What characters/symbols are allowed within CSS class names? Things such as:
~ ! @ $ % ^ & * ( ) _ + - = , . / ' ; : " ? > < [ ] \ { } | ` #
I know a lot of these are invalid, but which are valid?
What characters/symbols are allowed within CSS class names? Things such as:
~ ! @ $ % ^ & * ( ) _ + - = , . / ' ; : " ? > < [ ] \ { } | ` #
I know a lot of these are invalid, but which are valid?
You can check directly at the CSS grammar.
Basically, a name must start with an underscore (_
), a dash (-
), or a letter(a
–z
), followed immediately 1 by a letter or underscore, and then any number of dashes, underscores, letters, or numbers2:
-?[_a-zA-Z]+[_a-zA-Z0-9-]*
Identifiers beginning with a dash or underscore are typically reserved for browser-specific extensions, as in -moz-opacity
.
1 Note that, according to the grammar I linked, a rule starting with TWO dashes, e.g. --indent1
, is invalid. However, I'm pretty sure I've seen this in practice.
2 It's all made a bit more complicated by the inclusion of escaped unicode characters (that no one really uses).
Read the W3C spec. (this is CSS 2.1, find the appropriate version for your assumption of browsers)
edit: relevant paragraph follows:
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, 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".
edit 2: as @mipadi points out in Triptych's answer, there's this caveat, also in the same webpage:
In CSS, identifiers may begin with '-' (dash) or '_' (underscore). Keywords and property names beginning with '-' or '_' are reserved for vendor-specific extensions. Such vendor-specific extensions should have one of the following formats:
'-' + vendor identifier + '-' + meaningful name '_' + vendor identifier + '-' + meaningful name
Example(s):
For example, if XYZ organization added a property to describe the color of the border on the East side of the display, they might call it -xyz-border-east-color.
Other known examples:
-moz-box-sizing -moz-border-radius -wap-accesskey
An initial dash or underscore is guaranteed never to be used in a property or keyword by any current or future level of CSS. Thus typical CSS implementations may not recognize such properties and may ignore them according to the rules for handling parsing errors. However, because the initial dash or underscore is part of the grammar, CSS 2.1 implementers should always be able to use a CSS-conforming parser, whether or not they support any vendor-specific extensions.
Authors should avoid vendor-specific extensions
The complete regular expression is:
-?(?:[_a-z]|[\200-\377]|\\[0-9a-f]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-f])(?:[_a-z0-9-]|[\200-\377]|\\[0-9a-f]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-f])*
So all of your listed character except “-
” and “_
” are not allowed if used directly. But you can encode them using a backslash foo\~bar
or using the unicode notation foo\7E bar
.
My understanding is that the underscore is technically valid. Check out:
https://developer.mozilla.org/en/underscores_in_class_and_id_names
"...errata to the specification published in early 2001 made underscores legal for the first time."
The article linked above says never use them, then gives a list of browsers that don't support them, all of which are, in terms of numbers of users at least, long-redundant.