[CSS Selector] E[foo]
Using 'selector[attribute]', you can apply styles only to elements that have a specific attribute. This is commonly called an attribute selector. The value of the attribute does not matter — simply having the attribute is enough. Note that this does not work in IE6 and below.
Sample Code
p[id] { color: red;} /* Applies to all p elements that have an id attribute. */
p[title] { color: blue;} /* Applies to all p elements that have a title attribute. */
[class] { color: orange;} /* Applies to all elements that have a class attribute. Same as specifying '*[class]'. */
Browser Preview
p[id] { color: red;} /* Applies to all p elements that have an id attribute. */
p[title] { color: blue;} /* Applies to all p elements that have a title attribute. */
[class] { color: orange;} /* Applies to all elements that have a class attribute. Same as specifying '*[class]'. */
<p id="hoge">This is a p element. The id is set to 'hoge'.</p> <p>This is a p element.</p> <p title="">This is a p element. The title attribute is set but has no value.</p> <p class="hoge1">This is a p element.</p>
Browser Compatibility
Desktop
2 and below ×Supported in all versions.
8 ○
7 ○
6 ×
8 and below ×Mobile
Android Browser
4.4+ ○
3 and below ×Same support as desktop.
Same support as desktop.
※ Version data is based on MDN.
If you find any errors or copyright issues, please contact us.