Bird
Raised Fist0
CSSmarkup~20 mins

Attribute selectors in CSS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Attribute Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
selector
intermediate
2:00remaining
Which CSS rule selects all elements with a 'type' attribute exactly equal to 'text'?
Given the following HTML snippet:
<input type="text" name="username">
<input type="password" name="password">
<input type="text" name="email">

Which CSS selector will style only the inputs where type is exactly text?
Ainput[type="text"] { border: 2px solid blue; }
Binput[type*="text"] { border: 2px solid blue; }
Cinput[type^="text"] { border: 2px solid blue; }
Dinput[type$="text"] { border: 2px solid blue; }
Attempts:
2 left
💡 Hint
The selector should match elements whose attribute value is exactly the given string.
selector
intermediate
2:00remaining
Which selector matches elements with a 'data-role' attribute that starts with 'admin'?
Consider these HTML elements:
<div data-role="admin-user">User</div>
<div data-role="administrator">Admin</div>
<div data-role="guest">Guest</div>

Which CSS selector will style only the first two divs?
Adiv[data-role*="admin"] { background-color: yellow; }
Bdiv[data-role$="admin"] { background-color: yellow; }
Cdiv[data-role^="admin"] { background-color: yellow; }
Ddiv[data-role="admin"] { background-color: yellow; }
Attempts:
2 left
💡 Hint
Look for the selector that matches attribute values starting with a specific string.
selector
advanced
2:00remaining
What is the visual effect of this CSS rule?
a[href$='.pdf'] { color: red; }
Given this CSS:
a[href$='.pdf'] { color: red; }

And these links:
<a href="file.pdf">PDF File</a>
<a href="document.pdf?version=2">PDF Version 2</a>
<a href="image.png">Image</a>

Which links will appear red in the browser?
ANo links will be red because the selector is invalid.
BBoth links with href containing '.pdf' anywhere will be red.
CAll links will be red because they all have href attributes.
DOnly the link with href exactly ending with '.pdf' (file.pdf) will be red.
Attempts:
2 left
💡 Hint
The $= selector matches attribute values that end with the given string exactly.
accessibility
advanced
2:00remaining
Which attribute selector helps improve accessibility by styling only form inputs that have a 'required' attribute?
You want to visually highlight all form fields that are required by the user.
Which CSS selector correctly targets all inputs with the 'required' attribute, regardless of its value?
Ainput[required] { outline: 2px solid red; }
Binput[required="true"] { outline: 2px solid red; }
Cinput[required^="true"] { outline: 2px solid red; }
Dinput[required$="true"] { outline: 2px solid red; }
Attempts:
2 left
💡 Hint
The presence selector matches elements that have the attribute, no matter its value.
🧠 Conceptual
expert
2:00remaining
How many elements will be selected by this CSS?
button[aria-pressed="true"]
Given this HTML:
<button aria-pressed="true">On</button>
<button aria-pressed="false">Off</button>
<button aria-pressed>Unknown</button>
<button>No attribute</button>

How many buttons will the selector button[aria-pressed="true"] match?
A4
B1
C3
D2
Attempts:
2 left
💡 Hint
The selector matches elements with the attribute exactly equal to the string 'true'.

Practice

(1/5)
1. What does the CSS attribute selector [type="text"] select?
easy
A. All elements with a class named "text"
B. All elements with an attribute type equal to "text"
C. All elements with an ID named "text"
D. All elements that contain the word "text" anywhere

Solution

  1. Step 1: Understand attribute selector syntax

    The selector [type="text"] targets elements that have an attribute named type with the exact value "text".
  2. 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.
  3. Final Answer:

    All elements with an attribute type equal to "text" -> Option B
  4. Quick Check:

    Attribute selector = exact attribute match [OK]
Hint: Attribute selectors match exact attribute values [OK]
Common Mistakes:
  • Confusing attribute selector with class or ID selectors
  • Thinking it matches partial attribute values
  • Assuming it selects elements containing the word anywhere
2. Which of the following is the correct CSS syntax to select all input elements with a placeholder attribute?
easy
A. input[placeholder]
B. input.placeholder
C. input#placeholder
D. input(placeholder)

Solution

  1. Step 1: Identify attribute selector syntax

    To select elements with a specific attribute regardless of value, use [attribute]. So input[placeholder] selects all input elements with a placeholder attribute.
  2. Step 2: Eliminate incorrect syntax

    input.placeholder selects inputs with class "placeholder"; input#placeholder selects input with ID "placeholder"; input(placeholder) is invalid CSS syntax.
  3. Final Answer:

    input[placeholder] -> Option A
  4. Quick Check:

    Attribute presence selector = [attribute] [OK]
Hint: Use square brackets for attribute presence [OK]
Common Mistakes:
  • Using dot or hash instead of square brackets
  • Trying to use parentheses for attributes
  • Confusing attribute selectors with class or ID selectors
3. Given the CSS rule 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>
medium
A. Only Link 1
B. Link 2 and Link 4
C. Link 1 and Link 3
D. All links

Solution

  1. Step 1: Understand the attribute selector [href^="https"]

    The caret (^) means "starts with". This selector matches a elements whose href attribute starts with "https".
  2. 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.
  3. Final Answer:

    Link 1 and Link 3 -> Option C
  4. Quick Check:

    ^ means starts with = Link 1 & 3 green [OK]
Hint: ^ means attribute value starts with given string [OK]
Common Mistakes:
  • Confusing ^ with $ or * in attribute selectors
  • Assuming partial match anywhere instead of start
  • Ignoring exact string case sensitivity
4. The CSS below is intended to select all 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?
medium
A. Change *= to $= to match the end of the attribute
B. Change *= to ^= to match the start of the attribute
C. Add quotes around the selector like "img[alt*='.jpg']"
D. Use img[alt~='.jpg'] to match the word

Solution

  1. Step 1: Understand attribute selector operators

    *= means "contains" anywhere, $= means "ends with", ^= means "starts with".
  2. Step 2: Match attribute ending with ".jpg"

    Since we want to select elements whose alt attribute ends with ".jpg", we must use $= instead of *=.
  3. Final Answer:

    Change *= to $= to match the end of the attribute -> Option A
  4. Quick Check:

    $= means ends with [OK]
Hint: Use $= to select attributes ending with a value [OK]
Common Mistakes:
  • Using *= which matches anywhere, not just the end
  • Confusing ^= (start) with $= (end)
  • Adding quotes incorrectly around selectors
5. You want to style all 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?
hard
A. button[data-action$="save"], button[data-action^="save-"]
B. button[data-action^="save"]
C. button[data-action*="save"]
D. button[data-action="save"], button[data-action^="save-"]

Solution

  1. Step 1: Understand the requirement

    We want buttons where data-action is exactly "save" OR starts with "save-".
  2. 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.
  3. 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.
  4. Final Answer:

    button[data-action="save"], button[data-action^="save-"] -> Option D
  5. Quick Check:

    Exact match + prefix with dash = button[data-action="save"], button[data-action^="save-"] [OK]
Hint: Combine exact and prefix selectors with comma for precise matches [OK]
Common Mistakes:
  • Using only prefix selector which matches unwanted values
  • Using contains (*) selector which is too broad
  • Confusing $= (ends with) with ^= (starts with)