I’m reading the spec on attribute selectors, but I can’t find anything that says if whitespace is allowed. I’m guessing it’s allowed at the beginning, before and after the operator, and at the end. Is this correct?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The rules on whitespace in attribute selectors are stated in the grammar. Here’s the Selectors 3 production for attribute selectors (some tokens substituted with their string equivalents for illustration;
S*represents 0 or more whitespace characters):Of course, the grammar isn’t terribly useful to someone looking to understand how to write attribute selectors, as it’s intended for someone who’s implementing a selector engine.
Here’s a plain-English explanation:
Whitespace before the attribute selector
This isn’t covered in the above production, but the first obvious rule is that if you’re attaching an attribute selector to another simple selector or a pseudo-element, don’t use a space:
If you do, the space is treated as a descendant combinator instead, with the universal selector implied on the attribute selector and anything that may follow it. In other words, these selectors are equivalent to each other, but different from the above:
Whitespace inside the attribute selector
Whether you have any whitespace within the brackets and around the comparison operator doesn’t matter; I find that browsers seem to treat them as if they weren’t there (but I haven’t tested extensively). These are all valid according to the grammar and, as far as I’ve seen, work in all modern browsers:
Whitespace is not allowed between the
^(or other symbol) and=as these are treated as a single token, and tokens cannot be broken apart.If IE7 and IE8 implement the grammar correctly, they should be able to handle them all as well.
If a namespace prefix is used, whitespace is not allowed between the prefix and the attribute name.
These are incorrect:
These are correct:
Whitespace within the attribute value
But notice the quotes around the attribute values above; if you leave them out, and you try to select something whose attribute has spaces in its value you have a syntax error.
This is incorrect:
This is correct:
This is because an unquoted attribute value is treated as an identifier, which doesn’t include whitespace (for obvious reasons), whereas a quoted value is treated as a string. See this spec for more details.
To prevent such errors, I strongly recommend always quoting attribute values, whether in HTML, XHTML (required), XML (required), CSS or jQuery (once required).
Whitespace after the attribute value
As of Selectors 4 (following the original publication of this answer), attribute selectors can accept flags in the form of an identifier appearing after the attribute value. Two flags have been defined pertaining to character case, one for case-insensitive matching:
And one for case-sensitive matching (whose addition I had a part in, albeit by proxy of the WHATWG):
The grammar has been updated thus:
In plain English: if the attribute value is not quoted (i.e. it is an identifier), whitespace between it and
attrib_flagsis required; otherwise, if the attribute value is quoted then whitespace is optional, but strongly recommended for the sake of readability. Whitespace betweenattrib_flagsand the closing bracket is optional as always.