CSS Attribute Selectors allow you to style HTML elements based on the presence or value of an attribute. You can target links, form fields, images and more using selectors like [href], [type="text"] or [src$=".png"].
This makes your CSS more flexible and reduces the need for extra classes in your HTML.
[attr] selects elements that have a given attribute.[attr="value"] selects elements whose attribute value matches exactly.[attr^="val"] matches attribute values that begin with a value.[attr$="val"] matches attribute values that end with a value.[attr*="val"] matches attribute values that contain a value anywhere.<a>), form inputs, and images.Common CSS attribute selector patterns:
/* 1. Any element that simply has the attribute */
a[target] {
color: red;
}
/* 2. Exact value match */
input[type="text"] {
background-color: lightyellow;
}
/* 3. Value starts with 'https' (secure links) */
a[href^="https"] {
font-weight: bold;
}
/* 4. Value ends with '.png' (PNG images) */
img[src$=".png"] {
border: 2px solid green;
}
/* 5. Class attribute contains 'btn-' anywhere */
button[class*="btn-"] {
padding: 8px 14px;
}
These selectors look at attribute values, not just at classes or IDs. That’s why they are perfect when the HTML is already using attributes like type, href, or src.
Quick reference for the main attribute selector variants:
| Selector | Description | Example |
|---|---|---|
[attr] |
Selects elements that have the attribute | input[required] |
[attr="value"] |
Matches an exact attribute value | input[type="text"] |
[attr^="val"] |
Value starts with the given text | a[href^="https"] |
[attr$="val"] |
Value ends with the given text | img[src$=".png"] |
[attr*="val"] |
Value contains the given text | button[class*="btn-"] |
Imagine a small “Sign up” section with two links and a few inputs. We will use attribute selectors to style them without adding extra classes.
<!-- Links in a small navigation bar -->
<a href="https://blog.example.com" target="_blank">Company Blog</a>
<a href="/pricing.html">Pricing</a>
<!-- Sign up form fields -->
<input type="text" placeholder="Full name" required>
<input type="email" placeholder="Work email">
<input type="password" placeholder="Create password" required>
Below is the same markup, rendered. Use the notes under each group to clearly see how attribute selectors would match these elements.
a[target] → matches Company Blog (it has target="_blank").a[href^="https"] → matches Company Blog (its href starts with https).a[href^="/"] → would match Pricing (internal link starting with /).
input[type="text"] → matches the first field (Full name).input[type="password"] → matches the third field (Create password).input[required] → matches the first and third fields (they both have required).In your own stylesheet, you could now add colors, borders, or spacing to each of these groups using only the attribute selectors shown — no extra HTML classes are needed.
type or href).[type="text"] is clearer than just [type] when you need only one kind of input.a[href^="https"].input[required] (for example with a light background color).input[type="password"] to make them stand out.href ends with ".pdf" and style it via a[href$=".pdf"].btn-primary and btn-secondary, then style them together using button[class*="btn-"].