Careeroza
Attribute Selectors
Selecting by Attribute
| Selector | Selects elements whose attribute... |
|---|---|
[target] | exists |
[type="email"] | equals a value |
[href^="https"] | starts with a value |
[href$=".pdf"] | ends with a value |
[class*="btn"] | contains a value |
Example
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
a[target="_blank"]::after { content: " ↗"; }
a[href$=".pdf"] { color: crimson; }
input[type="text"] { border: 2px solid #04AA6D; padding: 4px; }
</style>
</head>
<body>
<p><a href="https://example.com" target="_blank">External link</a></p>
<p><a href="report.pdf">Download report (PDF)</a></p>
<input type="text" placeholder="Text input">
<input type="number" placeholder="Number input">
</body>
</html>Result
Exercise
Exercise: Write a selector that makes every link starting with https:// green.
Show answer
a[href^="https://"] { color: green; }