CSS Attribute Selector
CSS Attribute Selector
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R
CSS Lists
Tutorials References Exercises Bootcamp Upgrade Get Certified Create Website Sign Up Log in
CSS Tables
CSS Display
CSS Max-width
CSS Position
CSS Z-index
CSS Overflow
CSS Float
CSS Inline-block
CSS Align
CSS Attribute Selectors
CSS Combinators
CSS Pseudo-class
❮ Previous Next ❯
CSS Pseudo-element
CSS Opacity
CSS Navigation Bar
Style HTML Elements With Specific Attributes
CSS Dropdowns
CSS Image Gallery It is possible to style HTML elements that have specific attributes or attribute values.
CSS Image Sprites
COLOR PICKER
CSS Attr Selectors
CSS Forms
CSS Counters
CSS [attribute] Selector
CSS Website Layout The [attribute] selector is used to select elements with a specified attribute.
CSS Units
CSS Specificity The following example selects all <a> elements with a target attribute:
CSS !important
CSS Math Functions
Example Get certified
by completing
a[target] {
background-color: yellow;
a CSS
} course today!
school
Try it Yourself » w3 s
3
CE
02
TI 2
R
FI .
ED
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;
}
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".
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 starts with "top":
Example
[class^="top"] {
background: yellow;
}
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
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:
Set the background color to "red" for <a> elements that have a target attribute.
<style>
{
background-color: red;
}
</style>
<body>
<a href="https://w3schools.com">w3schools.com</a>
<a href="http://disney.com" target="_blank">Disney.com</a>
<a href="http://wikipedia.org" target="_top">wikipedia.org</a>
</body>
Submit Answer »
[attribute~=value] [title~="flower"] Selects all elements with a title attribute containing the word "flower"
[attribute|=value] [lang|="en"] Selects all elements with a lang attribute value starting with "en"
[attribute^=value] a[href^="https"] Selects every <a> element whose href attribute value begins with "https"
[attribute$=value] a[href$=".pdf"] Selects every <a> element whose href attribute value ends with ".pdf"
[attribute*=value] a[href*="w3schools"] Selects every <a> element whose href attribute value contains the
substring "w3schools"
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.