tags:

views:

10203

answers:

5

What characters/symbols are allowed within CSS class names? Things such as:

~ ! @ $ % ^ & * ( ) _ + - = , . / ' ; : " ? > < [ ] \ { } | ` #

I know a lot of these are invalid, but which are valid?

+42  A: 

You can check directly at the CSS grammar.

Basically, a name must start with an underscore (_), a dash (-), or a letter(az), 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).

Triptych
According to the blog entry in my answer, underscore as the first character won't work in IE6.
Paolo Bergantino
NB: The W3C says that the use of a leading '-' or '_' should be reserved for vendor-specific CSS extensions (e.g., -moz* classes implemented by Mozilla browsers).
mipadi
I'm expanding as I go along folks. thanks :)
Triptych
@Paolo, IE6 doesn't determine what is "valid," which is what the original question asked for.
strager
The \-escapes are commonly used, but generally mostly for the purposes of CSS hacks, isolating browsers that don't support them.
bobince
@strager: fair enough. still, you probably shouldn't alienate IE6 for something as silly as wanting to start your class name with an underscore.
Paolo Bergantino
@Paolo - if you stick to the guideline of only using underscores for browser-specific extensions, then there is no harm in IE6 ignoring them
Triptych
@strager. x doesn't work in IE6 is always a valid comment, mostly because 60% of the people browsing the internet still uses it. So it may not be valid/invalid but it is most-commonly-used.
Pim Jager
You can use escape notation to represent different characters. This can be useful, say, for targeting the underscore as the first character and having IE6 recognize it, as such: \\_
American Yak
+11  A: 

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

Jason S
+3  A: 

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.

Gumbo
+1  A: 

For HTML5/CSS3 classes and IDs can start with numbers.

Marius
+1  A: 

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.

mofaha