CSS · CSS Tutorial · Lesson 34
CSS Attribute Selectors
Select elements by attributes and values.
It is possible to style HTML elements that have specific attributes or attribute values.
| Selector | Selects |
|---|---|
| [target] | Elements with a target attribute |
| [target="_blank"] | Elements with target exactly "_blank" |
| [class~="flower"] | Attribute contains the word "flower" |
| [href^="https"] | Value starts with "https" |
| [href$=".pdf"] | Value ends with ".pdf" |
| [title*="code"] | Value contains "code" |
Example
<style>
a[target="_blank"] { background-color: yellow; }
a[href$=".pdf"]::after { content: " (PDF)"; color: red; }
input[type="text"] { border: 2px solid #04AA6D; padding: 6px; }
</style>
<a href="https://example.com" target="_blank">External link</a><br>
<a href="guide.pdf">Download guide</a><br><br>
<input type="text" placeholder="Text input"> <input type="password" placeholder="Password">
Keep reading “CSS Attribute Selectors”
Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.