3

In the latest version of Selectors Level 4:

The class selector is given as a full stop (. U+002E) immediately followed by an identifier.

An ID selector consists of a “number sign” (U+0023, #) immediately followed by the ID value, which must be a CSS identifier.

In the identifier link above:

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 (_); cannot start with a digit, two hyphens, or a hyphen followed by a digit.

However, in that same spec, the Grammar part:

<class-selector> = '.' <ident-token>

<id-selector> = <hash-token>

As you can see from the token links above, the syntax diagram explicitly specifies that a CSS identifer may begin with two hyphens, and the grammar of an ID selector contradicts with the grammar of a hash-token.

Which definitions should I follow?

UPDATE:

I missed a line in the bottom of the Grammar section:

In <id-selector>, the <hash-token>’s value must be an identifier.

2

1 Answer 1

1

The hash-token diagram must be wrong, as you quoted it:

element names, classes, and IDs in selectors (...) cannot start with a digit, two hyphens, or a hyphen followed by a digit

It is very explicit

Sign up to request clarification or add additional context in comments.

4 Comments

Clearly, between these definitions of an ID selector, only one of them is correct, just not sure which one. I think it's also quite possible that the editors forgot to update some of the definitions (esp. links), considering it's still a working draft.
Definitely only one is correct, but as one of them is explicit while the other one is a generalization, I think the one I quoted is the good one
It turns out, (see my update) the diagram is fine, since it's not used to describe the grammar of an id selector, but a hash. And fwiw, according to the new grammar of a CSS identifier, using two hyphens at the beginning is allowed now.
The quote is from CSS21. CSS Syntax 3 changed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.