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
Recall & Review
beginner
What does the CSS selector [attr^="value"] do?
It selects elements whose attribute attr value starts with the string value.
Click to reveal answer
beginner
What does the CSS selector [attr$="value"] do?
It selects elements whose attribute attr value ends with the string value.
Click to reveal answer
beginner
Give an example of a CSS rule using the starts-with selector to style links that start with "https".
[href^="https"] { color: green; }
This styles all links whose href attribute starts with "https" in green color.
Click to reveal answer
beginner
How can you select all images with file names ending in ".png" using CSS?
img[src$=".png"] { border: 2px solid blue; }
This adds a blue border to all images whose src attribute ends with ".png".
Click to reveal answer
intermediate
Why are starts-with and ends-with selectors useful in web design?
They let you style elements based on parts of attribute values, like URLs or file types, without adding extra classes or IDs. This keeps HTML clean and CSS flexible.
Click to reveal answer
Which CSS selector matches elements with an attribute data-id starting with "user"?
A[data-id^="user"]
B[data-id$="user"]
C[data-id*="user"]
D[data-id="user"]
✗ Incorrect
The ^ symbol means 'starts with', so [data-id^="user"] matches attributes starting with 'user'.
What does the selector a[href$=".pdf"] select?
ALinks with href exactly '.pdf'
BLinks with href starting with '.pdf'
CLinks with href containing '.pdf' anywhere
DLinks with href ending with '.pdf'
✗ Incorrect
The $ symbol means 'ends with', so it selects links whose href ends with '.pdf'.
Which selector would style all inputs with names ending in "_email"?
Ainput[name^="_email"]
Binput[name*="_email"]
Cinput[name$="_email"]
Dinput[name="_email"]
✗ Incorrect
The $ selector matches attributes ending with the given string.
If you want to select elements with an attribute containing a certain substring anywhere, which selector do you use?
A[attr$="value"]
B[attr*="value"]
C[attr^="value"]
D[attr="value"]
✗ Incorrect
The * selector matches attributes containing the substring anywhere.
Can starts-with and ends-with selectors be used on any attribute?
AYes, on any attribute
BNo, only on class attributes
CNo, only on id attributes
DNo, only on href attributes
✗ Incorrect
These selectors work on any attribute present on the element.
Explain how to use CSS attribute selectors to style elements based on the start or end of an attribute value.
Think about how you can target links or images by parts of their URLs.
You got /4 concepts.
Describe a real-life scenario where starts-with or ends-with selectors help keep your HTML clean and CSS efficient.
Consider how you might style different file types or URL patterns without changing HTML.
You got /4 concepts.
Practice
(1/5)
1. Which CSS selector targets all elements with an href attribute that starts with "https"?
easy
A. a[href*="https"]
B. a[href$="https"]
C. a[href^="https"]
D. a[href~="https"]
Solution
Step 1: Understand the starts-with selector syntax
The starts-with selector uses the caret symbol (^) inside square brackets to match attribute values that begin with a specific string.
Step 2: Match the selector to the question
The selector a[href^="https"] matches all anchor tags with href attributes starting with "https".
The selector targets inputs with name attributes starting with "user". The inputs have names "login", "signup", and "emailuser". None of these start with "user".
Step 2: Identify why no elements styled
Since none of the name attributes start with "user", no elements match the selector, so nothing gets styled.
Final Answer:
The attribute values do not start with "user" -> Option B
Quick Check:
Starts-with selector matches only beginning of attribute value [OK]
Hint: Check if attribute values actually start with the given string [OK]
Common Mistakes:
Assuming ends-with ($) is needed instead of starts-with (^)
Thinking IDs are required for attribute selectors
Believing selector syntax is wrong when it is correct
5. You want to style all img elements whose src attribute starts with "https://cdn." and ends with ".png". Which CSS selector correctly targets these images?
hard
A. img[src^="https://cdn."][src$=".png"]
B. img[src*="https://cdn."][src*=".png"]
C. img[src$="https://cdn."][src^=".png"]
D. img[src^=".png"][src$="https://cdn."]
Solution
Step 1: Understand combined attribute selectors
You can combine multiple attribute selectors to match elements that satisfy all conditions. Here, one selector checks if src starts with "https://cdn." and another checks if src ends with ".png".
Step 2: Match correct starts-with and ends-with syntax
Starts-with uses ^ and ends-with uses $. So the correct selector is img[src^="https://cdn."][src$=".png"].
Final Answer:
img[src^="https://cdn."][src$=".png"] -> Option A
Quick Check:
Combine ^ and $ selectors correctly [OK]
Hint: Combine ^ and $ selectors to match start and end [OK]