Discover how a few simple symbols can save you hours of tedious CSS work!
Why Starts-with and ends-with selectors in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a long list of product codes on a webpage. You want to highlight all products whose codes start with "A" or end with "Z" by changing their color.
If you try to do this manually, you must write separate CSS rules for each exact code or add special classes to each item. This is slow, error-prone, and hard to maintain when new products are added.
Starts-with and ends-with selectors let you target elements based on the beginning or ending of attribute values. This means you can write one rule to style all matching items automatically.
li[data-code='A123'] { color: red; } li[data-code='A456'] { color: red; } li[data-code='XYZ'] { color: blue; }
li[data-code^='A'] { color: red; } li[data-code$='Z'] { color: blue; }
You can style groups of elements dynamically based on attribute patterns without extra HTML changes.
On an online store, highlight all items starting with "NEW" to show new arrivals, or all items ending with "-sale" to mark discounted products.
Manual styling for many similar items is slow and error-prone.
Starts-with (^=) and ends-with ($=) selectors target attribute values by their start or end.
This makes styling dynamic groups easy and maintainable.
Practice
href attribute that starts with "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 selectora[href^="https"]matches all anchor tags with href attributes starting with "https".Final Answer:
a[href^="https"] -> Option CQuick Check:
Starts-with selector = ^ = a[href^="https"] [OK]
- Confusing starts-with (^) with ends-with ($)
- Using *= which means contains, not starts-with
- Using ~ which matches whole words in space-separated values
Solution
Step 1: Identify the ends-with selector syntax
The ends-with selector uses the dollar sign ($) inside square brackets to match attribute values that end with a specific string.Step 2: Apply correct syntax for class attribute
To select elements whose class attribute ends with "-btn", use[class$="-btn"]. The dot (.) is for class names, not attribute selectors.Final Answer:
[class$="-btn"] -> Option AQuick Check:
Ends-with selector = $ inside [] = [class$="-btn"] [OK]
- Using dot (.) with attribute selectors incorrectly
- Confusing starts-with (^) with ends-with ($)
- Missing square brackets for attribute selectors
<a href="https://example.com">Link1</a> <a href="http://example.com">Link2</a> <a href="https://secure.com">Link3</a>
Which links will be styled by the CSS selector
a[href^="https"] { color: red; }?Solution
Step 1: Identify which href values start with "https"
Link1 has href="https://example.com" and Link3 has href="https://secure.com". Both start with "https". Link2 starts with "http" (no s).Step 2: Apply the starts-with selector effect
The selectora[href^="https"]styles only elements whose href attribute starts with "https". So Link1 and Link3 get styled.Final Answer:
Link1 and Link3 -> Option DQuick Check:
Starts-with "https" matches Link1 & Link3 [OK]
- Including links starting with "http" without s
- Confusing contains (*) with starts-with (^)
- Assuming all links get styled
input[name^="user"] { background-color: yellow; }HTML:
<input name="login"> <input name="signup"> <input name="emailuser">
What is the likely problem?
Solution
Step 1: Check attribute values against selector
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 BQuick Check:
Starts-with selector matches only beginning of attribute value [OK]
- 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
img elements whose src attribute starts with "https://cdn." and ends with ".png". Which CSS selector correctly targets these images?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 isimg[src^="https://cdn."][src$=".png"].Final Answer:
img[src^="https://cdn."][src$=".png"] -> Option AQuick Check:
Combine ^ and $ selectors correctly [OK]
- Using * (contains) instead of ^ or $
- Swapping ^ and $ symbols
- Incorrect order or syntax of attribute selectors
