Chapter 17: Selectors & Specificity

Selectors & Specificity.

This is the part that decides which style wins when multiple rules try to style the same element. If you don’t understand selectors + specificity well, you’ll spend hours wondering:

“Why is my red color not applying?” “Why does this button look different on another page?” “Why does adding one class suddenly break everything?”

Let’s explain it like we’re sitting together in VS Code, going line-by-line, with lots of examples you can copy-paste and play with right now.

1. What is a CSS Selector?

A selector is simply how CSS finds the HTML element(s) you want to style.

Examples you already know:

CSS

2. The Cascade – Why Multiple Rules Fight Each Other

CSS = Cascading Style Sheets → when several rules target the same element, the browser uses a strict set of rules to decide the winner.

The full order of priority (from strongest to weakest):

  1. Transition / animation declarations (almost never relevant for beginners)
  2. !important (very last resort – avoid it like over-spicy mirchi 😅)
  3. Inline styles (style=”color: red;”) – very high priority
  4. Specificity (the main thing we’re learning today)
  5. Source order – if specificity is exactly equal, the rule that appears later in the CSS file wins

3. Specificity – How the Browser Calculates “Who Wins”

Specificity is a four-part score: a-b-c-d

Part What adds to it Example that adds 1 point here Score contribution
a Inline style attribute <p style=”color:red;”> 1,0,0,0
b ID selectors (#id) #main-header 0,1,0,0
c Classes .class, attributes [type=”…”], pseudo-classes :hover, :nth-child() .card, [type=”email”], :hover 0,0,1,0
d Elements p, div, pseudo-elements ::before p, ::first-letter 0,0,0,1

Higher number in left-most digit wins. If tie → move to next digit. Still tie → later rule in file wins.

4. Real Examples – Specificity Battles

Copy-paste this HTML + CSS and open with Live Server. Change things and watch which color wins.

index.html

HTML

style.css – lots of competing rules on purpose

CSS

What wins right now? → hotpink (because of !important)

Remove !important → now black wins (highest specificity: 0,1,4,2)

Add this inline to the

in HTML:

→ lime wins (inline = 1,0,0,0)

5. Quick Specificity Score Calculator Table

Selector Example Specificity Score Winner against…
p 0,0,0,1 Almost nothing
.card 0,0,1,0 Beats element selectors
#header 0,1,0,0 Beats classes
.card.featured 0,0,2,0 Beats single class
#main .card p 0,1,1,1 Very strong
div#main.container > p.text.lead 0,1,3,2 Extremely specific
Inline style=”color: cyan;” 1,0,0,0 Beats almost all CSS rules
!important Overrides everything except another !important with higher specificity

6. Best Practices in 2026 (So You Don’t Fight Specificity Later)

  1. Use classes for almost everything — avoid IDs for styling (save IDs for JS or anchors)
  2. Keep specificity low & flat — prefer .card-title over #main .card h2
  3. Use BEM-like naming (optional but helps a lot):
    • .card
    • .card__title
    • .card–featured (modifier)
  4. Never use !important except for debugging or overriding third-party libraries
  5. Avoid deep nesting — .container .section .card p strong is a red flag
  6. Order matters — put more general rules first, specific later

7. Your 5-Minute Experiment

  1. Open the demo above
  2. Remove !important from the hotpink rule → see black win
  3. Add !important back to the black rule → see black win again
  4. Add style=”color: yellow;” to the <p> → yellow wins
  5. Remove inline style → play with deleting selectors one by one and see color changes

That hands-on feeling is how you really get specificity.

How does it feel now? Any rule confusing? Want to see more battles (like pseudo-classes vs classes)?

Next possible lessons:

  • “BEM naming convention for cleaner selectors”
  • “CSS variables + specificity”
  • “when to use :where() and :is() to control specificity”
  • “Flexbox – next core after selectors”

You’re tackling one of the hardest parts of CSS — proud of you! 🚀 Chai thanda ho gaya? Fresh kar le, phir code karte hain 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *