CSS 2.1 defines identifiers as
In CSS, identifiers 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.
Therefore, --
should be an invalid identifier, and thus #--
shouldn't select the element with id="--"
:
body { color: black }
#-- { color: red }
<p id="--">I should be black.</p>
Attribute values must be identifiers or strings.
But --
seems to work as an identifier for attribute values, at least on Firefox:
body { color: black }
[id=--] { color: red }
<p id="--">I am red on Firefox.</p>
Moreover, CSS.escape
has been modified to allow --
:
Minor changes has been made in Firefox 32, to match the spec and the evolution of the CSS syntax. The identifier now can begin with
--
and the second dash must not be escaped.
According to the Wayback Machine, the change happened between 19 and 30 Apr 2014:
6 Feb 2014 editor's draft, stored on 19 Apr 2014.
If the character is the second character and is "-" (U+002D) and the first character is "-" as well, then the escaped character.
30 Apr 2014 editor's draft, stored on 4 May 2014.
If the character [...] is "-" (U+002D) [...], then the character itself.
So, has some new CSS3 module changed the definition of identifiers, so that they can sometimes begin with --
, or what exactly is happening here?
Standard
Effectively, a change in CSS Syntax Module now allows identifiers to start with two hyphens:
This change appeared in the 21 Mar 2014 Editor's Draft (changelog), and is still not present in the current Candidate Recomendation, which is the 20 Feb 2014 CR.
It's also described in Changes:
Reason
In www-style, the ...let's change the syntax thread proposed to change the syntax of CSS Variables:
_property
is a common IE6 hack.--
.Zack Weinberg warned:
There was a long discussion of what should be done.
--
prefix to indicate custom properties and other custom things.So the following day he commited the change of CSS Syntax to github (he is an editor of the spec).
Implementations
Firefox
Firefox allows identifiers to start with
--
since Nightly 31 2014-04-03 (pushlog). The behavior was changed in bug 985838:The change landed on Firefox 31.0. Since then,
[id=--]
works.However,
#--
still does not work even on latest Nightly 41. I filed bug 1175192 in order to fix that.Chrome
Chromium built a new CSS parser:
It was shipped in this commit, which was part of this commitlist, which was rolled in this commit. So it was finally implemented in build 44.0.2370.0 325166 (pushlog since 325152).
Since then, Chromium allows both
[id=--]
and#--
.