Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a workaround to make CSS classes with names that start with numbers valid? [duplicate]

Tags:

Is it referenced anywhere that CSS classes with names start with numbers doesn't work? for example I found that a class with background like:

.000000-8 {background:url(../../images/common/000000-0.8.png);} .8FFFFFF {background:url(../../images/common/FFFFFF-0.8.png);} 

doesn't work in most browsers I have and the CSS grammar shows that but my question is there a workaround to make CSS classes with names that start with numbers valid? .

like image 229
hsobhy Avatar asked Jan 20 '14 06:01

hsobhy


People also ask

Can CSS class names start with a number?

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.

Can class names start with a number?

It cannot start with a digit, starting with the digit is acceptable by HTML5 but not acceptable by CSS. Two hyphens followed by a number is valid.

Can a class and ID have the same name?

Yes, you can use same name for both id and class because both parameters have their own significance.

Can two classes have same name in CSS?

If you have two CSS files containing the same class name, then the properties of both will be applied in a sort of combination. If both class declarations share the same property, then the one for the file that was linked last is applied. Any properties that are declared in only one of the CSS files will be applied.


2 Answers

There are no CSS classes. The question logically splits to two questions: can you start a class name with a digit in HTML, and (assuming the answer is “yes”, as it is) if it does, how do you use the corresponding class selector in CSS?

Various HTML specifications impose various restrictions on class names, but in browser practice, and according to HTML5, there are no limitations, except that a class name cannot contain whitespace characters. So class=000000-8 is valid.

By CSS syntax rules, a CSS identifier cannot being with an unescaped digit. Therefore, a selector like .000000-8 is invalid. But the digit can be escaped, by CSS escaping rules: the selector

.\30 00000-8 

or, equivalently,

.\00003000000-8  

is valid and matches an element with class=000000-8.

Needless to say, class names starting with a digit are best avoided, but if you have to work with them (e.g., because some HTML documents have them and you cannot change the markup), this is the way.

like image 116
Jukka K. Korpela Avatar answered Oct 06 '22 00:10

Jukka K. Korpela


Yes, it's documented in CSS specification:

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers 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".

like image 27
MarcinJuraszek Avatar answered Oct 06 '22 01:10

MarcinJuraszek