Attribute selectors let you style HTML elements based on their attributes. This helps you target elements more precisely without adding extra classes or IDs.
Attribute selectors in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
[attribute] { /* styles */ }
[attribute="value"] { /* styles */ }
[attribute~="value"] { /* styles */ }
[attribute|="value"] { /* styles */ }
[attribute^="value"] { /* styles */ }
[attribute$="value"] { /* styles */ }
[attribute*="value"] { /* styles */ }The simplest form [attribute] selects elements with that attribute, no matter its value.
Other forms let you match exact or partial values inside the attribute.
<a>) that have a target attribute, no matter its value.a[target] {
color: red;
}type attribute value.input[type="checkbox"] { accent-color: green; }
href attribute starts with "https".a[href^="https"] { font-weight: bold; }
alt attribute contains the word "logo" anywhere.img[alt*="logo"] { border: 2px solid blue; }
This page shows how attribute selectors style elements based on their attributes:
- Links with
target="_blank"become red and underlined. - Inputs with the
requiredattribute get an orange border. - Images whose alt text contains "logo" get a blue border and smaller size.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Attribute Selectors Example</title> <style> /* Style all links that open in new tab */ a[target="_blank"] { color: red; text-decoration: underline; } /* Style inputs that are required */ input[required] { border: 2px solid orange; padding: 0.3rem; } /* Style images with alt containing 'logo' */ img[alt*="logo"] { border: 3px solid blue; max-width: 6rem; display: block; margin-bottom: 1rem; } </style> </head> <body> <main> <h1>Attribute Selectors Demo</h1> <section> <h2>Links</h2> <a href="https://example.com" target="_blank">Open Example in new tab</a><br /> <a href="https://example.com">Open Example in same tab</a> </section> <section> <h2>Form Inputs</h2> <label> Name (required): <input type="text" required /> </label><br /> <label> Email (optional): <input type="email" /> </label> </section> <section> <h2>Images</h2> <img src="https://via.placeholder.com/100" alt="company logo" /> <img src="https://via.placeholder.com/100" alt="profile picture" /> </section> </main> </body> </html>
Attribute selectors are case-sensitive for attribute values in HTML.
They help keep your HTML clean by avoiding extra classes just for styling.
Use browser DevTools to inspect elements and test attribute selectors live.
Attribute selectors let you style elements based on their attributes and values.
They are useful for targeting elements without adding extra classes or IDs.
Common forms include matching presence, exact value, starts with, ends with, and contains.
Practice
[type="text"] select?Solution
Step 1: Understand attribute selector syntax
The selector[type="text"]targets elements that have an attribute namedtypewith the exact value "text".Step 2: Differentiate from class and ID selectors
Class selectors use a dot (.) and ID selectors use a hash (#), so this selector is not for class or ID.Final Answer:
All elements with an attributetypeequal to "text" -> Option BQuick Check:
Attribute selector = exact attribute match [OK]
- Confusing attribute selector with class or ID selectors
- Thinking it matches partial attribute values
- Assuming it selects elements containing the word anywhere
input elements with a placeholder attribute?Solution
Step 1: Identify attribute selector syntax
To select elements with a specific attribute regardless of value, use[attribute]. Soinput[placeholder]selects allinputelements with aplaceholderattribute.Step 2: Eliminate incorrect syntax
input.placeholderselects inputs with class "placeholder";input#placeholderselects input with ID "placeholder";input(placeholder)is invalid CSS syntax.Final Answer:
input[placeholder] -> Option AQuick Check:
Attribute presence selector = [attribute] [OK]
- Using dot or hash instead of square brackets
- Trying to use parentheses for attributes
- Confusing attribute selectors with class or ID selectors
a[href^="https"] { color: green; } and the HTML below, which links will appear green?<a href="https://example.com">Link 1</a>
<a href="http://example.com">Link 2</a>
<a href="https://secure.com">Link 3</a>
<a href="ftp://example.com">Link 4</a>Solution
Step 1: Understand the attribute selector
The caret (^) means "starts with". This selector matches[href^="https"]aelements whosehrefattribute starts with "https".Step 2: Check each link's href value
Link 1: "https://example.com" starts with "https" - matches.
Link 2: "http://example.com" starts with "http" - no.
Link 3: "https://secure.com" starts with "https" - matches.
Link 4: "ftp://example.com" starts with "ftp" - no.Final Answer:
Link 1 and Link 3 -> Option CQuick Check:
^ means starts with = Link 1 & 3 green [OK]
- Confusing ^ with $ or * in attribute selectors
- Assuming partial match anywhere instead of start
- Ignoring exact string case sensitivity
img elements with an alt attribute ending with ".jpg". Why does it not work?img[alt*=".jpg"] { border: 2px solid red; }What is the correct fix?
Solution
Step 1: Understand attribute selector operators
*=means "contains" anywhere,$=means "ends with",^=means "starts with".Step 2: Match attribute ending with ".jpg"
Since we want to select elements whosealtattribute ends with ".jpg", we must use$=instead of*=.Final Answer:
Change*=to$=to match the end of the attribute -> Option AQuick Check:
$= means ends with [OK]
- Using *= which matches anywhere, not just the end
- Confusing ^= (start) with $= (end)
- Adding quotes incorrectly around selectors
button elements that have a data-action attribute starting with "save" but only if the attribute value is exactly "save" or starts with "save-" (like "save-draft"). Which CSS selector correctly achieves this?Solution
Step 1: Understand the requirement
We want buttons wheredata-actionis exactly "save" OR starts with "save-".Step 2: Choose selectors for exact and prefix matches
[data-action="save"]matches exactly "save".[data-action^="save-"]matches values starting with "save-".
Combining with a comma selects both sets.Step 3: Analyze other options
[data-action^="save"]matches any value starting with "save", including "savegame" which is not desired.[data-action*="save"]matches anywhere "save" appears, too broad.[data-action$="save"]matches values ending with "save", not what we want.Final Answer:
button[data-action="save"], button[data-action^="save-"] -> Option DQuick Check:
Exact match + prefix with dash = button[data-action="save"], button[data-action^="save-"] [OK]
- Using only prefix selector which matches unwanted values
- Using contains (*) selector which is too broad
- Confusing $= (ends with) with ^= (starts with)
