Technically speaking, it is not that HTML classes cannot start with a digit. Rather, it is that 10seconds
is not a valid CSS identifier, which means that the CSS processor will choke on a rule such as
.10seconds { }
and ignore it. The solution, if you really want to use the 10seconds
class, is to escape the leading digit:
.\31 0seconds { }
The \31
here is a the digit 1
, and the escape sequence "eats" the following space.
Another alternative is to write the rule using an attribute selector:
[class~="10seconds"] { color: #00b200; }
See this question (second answer):
If you want to use a class name which is not a valid CSS identifier in querySelector
, you may use the CSS.escape utility if it is available on your platform:
document.querySelector('.' + CSS.escape('10seconds'))
...most answers here are wrong. It turns out that any character except NUL is allowed as CSS class name in CSS.
Of course, as other answers suggest, you may prefer to simply use valid CSS identifiers as class names, such as seconds10
, and avoid all this trouble.