Bird
Raised Fist0
CSSmarkup~8 mins

Starts-with and ends-with selectors in CSS - Browser Rendering Trace

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
Render Flow - Starts-with and ends-with selectors
[Parse CSS selectors] -> [Match elements with attribute selectors] -> [Check attribute values] -> [Apply styles to matched elements] -> [Recalculate layout if needed] -> [Paint styles]
The browser reads the CSS selectors, finds elements whose attributes start or end with specific strings, applies the styles to those elements, then repaints the page.
Render Steps - 2 Steps
Code Added:li[data-name^="ap"] { color: green; font-weight: bold; }
Before
[Apple]
[Apricot]
[Banana]
[Grape]
[Pineapple]
After
[Apple] (green, bold)
[Apricot] (green, bold)
[Banana]
[Grape]
[Pineapple]
Items whose data-name starts with 'ap' are styled green and bold, so Apple and Apricot change visually.
🔧 Browser Action:Matches elements with attribute starting with 'ap', applies styles, triggers repaint.
Code Sample
List items starting with 'ap' in their data-name turn green and bold; items ending with 'ple' get a yellow background.
CSS
<ul>
  <li data-name="apple">Apple</li>
  <li data-name="apricot">Apricot</li>
  <li data-name="banana">Banana</li>
  <li data-name="grape">Grape</li>
  <li data-name="pineapple">Pineapple</li>
</ul>
CSS
li[data-name^="ap"] {
  color: green;
  font-weight: bold;
}

li[data-name$="ple"] {
  background-color: yellow;
}
Render Quiz - 3 Questions
Test your understanding
After applying step 1, which list items are green and bold?
ABanana and Grape
BApple and Apricot
CPineapple only
DAll items
Common Confusions - 2 Topics
Why doesn't the style apply if the attribute value contains the string but not at start or end?
The ^ and $ selectors only match the start or end of the attribute value exactly, not anywhere inside. For example, 'grape' contains 'ap' but not at the start, so it won't match [data-name^="ap"] (see step 1).
💡 Starts-with (^) matches only beginning; ends-with ($) matches only end.
Can these selectors match partial words inside the attribute?
No, they only check the start or end of the whole attribute value. To match anywhere inside, use *= selector (not covered here).
💡 Use *= for contains, ^ for starts-with, $ for ends-with.
Property Reference
SelectorMeaningVisual EffectCommon Use
[attr^="value"]Selects elements with attribute starting with 'value'Applies styles to elements whose attribute begins with given stringHighlight items by prefix
[attr$="value"]Selects elements with attribute ending with 'value'Applies styles to elements whose attribute ends with given stringHighlight items by suffix
Concept Snapshot
Starts-with selector: [attr^="value"] matches elements whose attribute begins with 'value'. Ends-with selector: [attr$="value"] matches elements whose attribute ends with 'value'. They apply styles only if the attribute value exactly starts or ends with the string. Commonly used to style elements by attribute prefixes or suffixes. They trigger style recalculation and repaint when applied.

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

  1. 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.
  2. Step 2: Match the selector to the question

    The selector a[href^="https"] matches all anchor tags with href attributes starting with "https".
  3. Final Answer:

    a[href^="https"] -> Option C
  4. Quick Check:

    Starts-with selector = ^ = a[href^="https"] [OK]
Hint: Starts-with selector uses ^ inside attribute brackets [OK]
Common Mistakes:
  • Confusing starts-with (^) with ends-with ($)
  • Using *= which means contains, not starts-with
  • Using ~ which matches whole words in space-separated values
2. Which of the following is the correct CSS syntax to select elements with a class attribute ending with "-btn"?
easy
A. [class$="-btn"]
B. .class^="-btn"
C. .class$="-btn"
D. [class^="-btn"]

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    [class$="-btn"] -> Option A
  4. Quick Check:

    Ends-with selector = $ inside [] = [class$="-btn"] [OK]
Hint: Ends-with selector uses $ inside attribute brackets [OK]
Common Mistakes:
  • Using dot (.) with attribute selectors incorrectly
  • Confusing starts-with (^) with ends-with ($)
  • Missing square brackets for attribute selectors
3. Given this HTML:
<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; }?
medium
A. All three links
B. Only Link1
C. Link2 and Link3
D. Link1 and Link3

Solution

  1. 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).
  2. Step 2: Apply the starts-with selector effect

    The selector a[href^="https"] styles only elements whose href attribute starts with "https". So Link1 and Link3 get styled.
  3. Final Answer:

    Link1 and Link3 -> Option D
  4. Quick Check:

    Starts-with "https" matches Link1 & Link3 [OK]
Hint: Starts-with selector matches only beginning of attribute value [OK]
Common Mistakes:
  • Including links starting with "http" without s
  • Confusing contains (*) with starts-with (^)
  • Assuming all links get styled
4. You wrote this CSS but it doesn't style any elements:
input[name^="user"] { background-color: yellow; }

HTML:
<input name="login">
<input name="signup">
<input name="emailuser">

What is the likely problem?
medium
A. The selector should use $ instead of ^
B. The attribute values do not start with "user"
C. The input elements need IDs for this selector
D. The selector syntax is invalid

Solution

  1. 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".
  2. 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.
  3. Final Answer:

    The attribute values do not start with "user" -> Option B
  4. 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

  1. 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".
  2. 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"].
  3. Final Answer:

    img[src^="https://cdn."][src$=".png"] -> Option A
  4. Quick Check:

    Combine ^ and $ selectors correctly [OK]
Hint: Combine ^ and $ selectors to match start and end [OK]
Common Mistakes:
  • Using * (contains) instead of ^ or $
  • Swapping ^ and $ symbols
  • Incorrect order or syntax of attribute selectors