0

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?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • apostrophes were never mandatory, they are optional. You need them for values that contain spaces – Temani Afif Jan 26 '22 at 14:16
  • Does this answer your question? [Do values in CSS attribute selector values need to be quoted?](https://stackoverflow.com/questions/3851091/do-values-in-css-attribute-selector-values-need-to-be-quoted) or better yet [CSS attribute selectors: The rules on quotes (", ' or none?)](https://stackoverflow.com/q/5578845/215552) – Heretic Monkey Jan 26 '22 at 14:22

1 Answers1

2

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>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415