15

Can XHTML and HTML class attributes value start with a number?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
HELP
  • 14,237
  • 22
  • 66
  • 100

3 Answers3

14

No. They have to be SGML names. They "must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

However, class names that start with a number are supported by IE.

EDIT: meder pointed out that you can use Unicode characters and they seem to work in all browsers. I don't know if it complies with the specifications, but it does seem to validate. See http://css-tricks.com/unicode-class-names/ and http://snook.ca/archives/html_and_css/unicode_for_css_class_names

Adam
  • 43,763
  • 16
  • 104
  • 144
6

No, they cannot. They must begin with a letter. Some browsers may erroneously support them, though.

EDIT: You can start off with unicode escape points and specify the code for a number.

EDIT #2: Test case http://work.arounds.org/sandbox/66/run

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • You can specify numbers using hex code points, `.\34 ` is the UTF-8 code for number 4 per http://www.yoursiteisvalid.com/validnews/css-class-not-working-when-name-begins-with-numbers-1000.html/comment-page-1 – meder omuraliev Nov 03 '10 at 16:23
2

From the CSS spec:

"In CSS3, identifiers (including element names, classes, and IDs in selectors (see [SELECT] [or is this still true])) can contain only the characters [A-Za-z0-9] and ISO 10646 characters 161 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit or a hyphen followed by a digit. They can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F". (See [UNICODE310] and [ISO10646].)"

Community
  • 1
  • 1
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • 2018: this link is broken / incorrect. Quoted text appears nowhere at the linked page, and the # ref is broken. I suspect the text was taken from a draft that embedded CSS2 and was later updated, so that the correct link today (becaues I needed to verify ths myself in updating an old parser!) is: https://www.w3.org/TR/CSS2/syndata.html#characters – Adam Nov 04 '18 at 23:55