W3schools: CSS Attribute Selectors
W3schools: CSS Attribute Selectors
asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
The following example selects all <a> elements with a target attribute:
Example
a[target] {
background-color: yellow;
}
Try it Yourself »
1 of 9 07/03/2017 9:53
CSS Attribute Selector https://www.w3schools.com/css/css_attribute_selectors.asp
The following example selects all <a> elements with a target="_blank" attribute:
Example
a[target="_blank"] {
background-color: yellow;
}
Try it Yourself »
The following example selects all elements with a title attribute that contains a space-
separated list of words, one of which is "flower":
Example
[title~="flower"] {
border: 5px solid yellow;
}
2 of 9 07/03/2017 9:53
CSS Attribute Selector https://www.w3schools.com/css/css_attribute_selectors.asp
Try it Yourself »
The example above will match elements with title="flower", title="summer flower", and
title="flower new", but not title="my-flower" or title="flowers".
The following example selects all elements with a class attribute value that begins with
"top":
Note: The value has to be a whole word, either alone, like class="top", or followed by a
hyphen( - ), like class="top-text"!
Example
[class|="top"] {
background: yellow;
}
Try it Yourself »
The following example selects all elements with a class attribute value that begins with
"top":
Example
[class^="top"] {
background: yellow;
}
3 of 9 07/03/2017 9:53
CSS Attribute Selector https://www.w3schools.com/css/css_attribute_selectors.asp
Try it Yourself »
The following example selects all elements with a class attribute value that ends with
"test":
Example
[class$="test"] {
background: yellow;
}
Try it Yourself »
The following example selects all elements with a class attribute value that contains "te":
Example
[class*="te"] {
background: yellow;
}
Try it Yourself »
Styling Forms
4 of 9 07/03/2017 9:53
CSS Attribute Selector https://www.w3schools.com/css/css_attribute_selectors.asp
The attribute selectors can be useful for styling forms without class or ID:
Example
input[type="text"] {
width: 150px;
display: block;
margin-bottom: 10px;
background-color: yellow;
}
input[type="button"] {
width: 120px;
margin-left: 35px;
display: block;
}
Try it Yourself »
Tip: Visit our CSS Forms Tutorial for more examples on how to style forms with CSS.
Exercise 6 »
For a complete reference of all the CSS selectors, please go to our CSS Selectors
Reference.
❮ Previous Next ❯
5 of 9 07/03/2017 9:53