Results 1 to 8 of 8

Thread: Escaping Initial Digit on Class

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Default Escaping Initial Digit on Class

    Hello Folks,

    The pertinent part of my css page was...

    [CODE].18usc3607 { background-image: url("../images/federal0.jpg");
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: top left; }[\CODE]

    The css validator didn't like that. Something about a requirement that classnames beginning with a digit be escaped. OK, so I changed it to...

    Code:
    .\18usc3607 { background-image: url("../images/federal0.jpg");
           background-repeat: no-repeat;
           background-attachment: fixed;
           background-position: top left; }
    That made the css validator happy. Except that looking closer at the validator return, what actually resulted was...

    Code:
    .\000018usc3607 {
    background-image : url("../images/federal0.jpg");
    background-repeat : no-repeat;
    background-attachment : fixed;
    background-position : top left;
    }
    Is there a way to avoid those unwanted leading zeros?

    A.

    P.S. The Dynamic Drive editor that processes the bracketCODEclosebraket also doesn't seem to like code that begins with a digit.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,939
    Thanks
    16
    Thanked 303 Times in 302 Posts
    Blog Entries
    11

    Default

    I can't find the official documentation for it but its because you shouldn't start ids and classes with numbers - I think IE (or earlier versions of it) will recognise ones with numbers at the starts, but not other browsers. You can use numbers in the middle or at the end, just not right at the beginning, so to be safe, stick to a-z/A-Z characters as the first character of any class or id.
    Last edited by Beverleyh; 03-30-2013 at 10:38 PM.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    This is the rule:
    Code:
    -?[_a-zA-Z][_a-zA-Z0-9-]*
    basically, a class name must:

    -- start with a letter (a-z or A-Z) or underscore, or
    start with a dash, followed by a letter or underscore, or
    an escaped character (what you're doing);

    -- be followed by zero or more underscores, letters, numbers (0-9), or dashes.

    In practice, starting with a dash is almost never done, except by browser-specific extensions and internal stylesheets. As Beverleyh points out, I would not expect consistent/reliable browser support.

    ditto for starting with an "escaped" number, except that it is far easier for things to get messed up. (Your situation, with unexplained leading zeros, is a good example of that.)
    I would highly recommend starting with an underscore, followed by your numeric class name.

  4. The Following User Says Thank You to traq For This Useful Post:

    marain (04-01-2013)

  5. #4
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Default

    So be it.

    The class name was generated in the first place by PHP code:

    Code:
    <?php
    $class = substr($_SERVER['PHP_SELF'], 1, (strlen - 4));
    echo '<body class="' . $class . '">';
    ?>
    So you're saying that what I need to do is change the file name. Hey, if I gotta, I gotta. It would not have been my first choice.

    In any case, thanks for reviewing this.

    Best,

    A.

  6. #5
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,939
    Thanks
    16
    Thanked 303 Times in 302 Posts
    Blog Entries
    11

    Default

    So you're saying that what I need to do is change the file name.
    Not necessarily. If it makes things easier for you, how about just editing the code to add a prefix?
    Code:
    <?php
    $class = substr($_SERVER['PHP_SELF'], 1, (strlen - 4));
    echo '<body class="_' . $class . '">';
    ?>
    You might not be able to see at a first glance but look closely and you'll see that I've put an underscore prefix in red, right before the auto-generated class. That should keep things neat and allow you to use the existing file name. Of course, if this is a system that you're using across multiple files/classes, you might need to tweak more selectors in your CSS as they'll now all have an underscore prefix.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  7. The Following User Says Thank You to Beverleyh For This Useful Post:

    marain (03-31-2013)

  8. #6
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Default

    Thank you.

    A.

  9. #7
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Default

    Trak, your recommendation works like a charm.

  10. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    You're welcome! though to be fair, it was Beverleyh's recommendation first

Similar Threads

  1. Replies: 20
    Last Post: 08-27-2012, 10:47 AM
  2. Replies: 10
    Last Post: 08-28-2011, 07:09 PM
  3. Resolved escaping quotes before reinsertion into database
    By james438 in forum PHP
    Replies: 8
    Last Post: 03-13-2010, 03:58 AM
  4. Universal Countdown : 2-digit minimum?
    By jyeager in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 01-05-2010, 04:13 PM
  5. Replies: 0
    Last Post: 04-22-2007, 11:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •