Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does an attribute CSS selector work both with and without apostrophes?

I know CSS Selectors should be generally of the following format:

tagName[attributeName='attributeValue']

For example Ask Question button on stackoverflow.com main page is sitting inside a div element matching the following CSS selector div[class='d-flex']
However I see with my Chrome DevTools that div[class=d-flex] locator is matching this element as well and it is found to be a correct locator!
So, my question is why the CSS selector expressions without apostrophes around the attribute values are still found as correct by the DevTools?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From the specification:

Attribute values must be <ident-token>s or <string-token>s

You never need to use apostrophes. There are useful only for values with spaces.

[data=ok] {
  height:100px;
  background:red;
}
[data="ok ok"] {
  height:100px;
  background:blue;
}
<div data=ok>

</div>

<div data="ok ok">

</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading