Bird
Raised Fist0
CSSmarkup~10 mins

Specificity rules 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 - Specificity rules
Parse CSS selectors
Calculate specificity
Compare specificity values
Apply styles with highest specificity
Render final styles on elements
The browser reads CSS selectors, calculates their specificity scores, compares them, and applies the styles with the highest specificity to the matching elements.
Render Steps - 3 Steps
Code Added:div { color: orange; }
Before
[div]
 Hello World (default black color)
After
[div]
 Hello World (orange color)
The div element is styled with orange color from the type selector.
🔧 Browser Action:Matches div element, applies color style, triggers paint.
Code Sample
A div with class 'box' and id 'unique' has three conflicting color styles; the browser chooses the color based on specificity rules.
CSS
<div class="box" id="unique">
  Hello World
</div>
CSS
.box { color: red; }
#unique { color: green; }
div { color: orange; }
Render Quiz - 3 Questions
Test your understanding
After applying step 2, what color is the text inside the div?
AGreen
BRed
COrange
DBlue
Common Confusions - 3 Topics
Why does the ID selector override the class selector even if the class is declared later?
Because ID selectors have higher specificity than class selectors, the browser applies the ID style regardless of order. Specificity beats source order.
💡 Higher specificity selectors always win over lower ones, no matter where they appear.
Why doesn't the inline style get overridden by the ID selector?
Inline styles have the highest specificity (except !important), so they override ID selectors unless !important is used.
💡 Inline styles trump all selectors except !important rules.
What happens if two selectors have the same specificity?
The browser applies the style declared last in the CSS source, so later rules override earlier ones if specificity ties.
💡 When specificity ties, the last declared style wins.
Property Reference
Selector TypeSpecificity ValueExampleVisual EffectCommon Use
Type Selector0,0,0,1divLowest specificity, applies basic stylesStyling elements by tag name
Class Selector0,0,1,0.boxOverrides type selectors, styles groupsStyling groups of elements
ID Selector0,1,0,0#uniqueOverrides class and type selectors, very specificStyling unique elements
Inline Style1,0,0,0style="color: blue;"Overrides all selectors except !importantDirect element styling
Universal Selector0,0,0,0*Lowest specificity, rarely overridesGeneral resets or base styles
Concept Snapshot
Specificity determines which CSS rule applies when multiple match. ID selectors have higher specificity than class selectors. Class selectors override type selectors. Inline styles override all selectors except !important. If specificity ties, the last declared rule wins.

Practice

(1/5)
1. Which CSS selector has the highest specificity?
easy
A. An ID selector like #header
B. A class selector like .menu
C. An element selector like div
D. A universal selector like *

Solution

  1. Step 1: Understand selector types and specificity

    ID selectors have the highest specificity, followed by class selectors, then element selectors.
  2. Step 2: Compare given selectors

    #header is an ID selector, which beats class .menu and element div selectors.
  3. Final Answer:

    An ID selector like #header -> Option A
  4. Quick Check:

    ID selector > class selector > element selector [OK]
Hint: ID selectors always outrank classes and elements [OK]
Common Mistakes:
  • Thinking class selectors have higher specificity than IDs
  • Confusing element selectors with class selectors
  • Ignoring the universal selector has lowest specificity
2. Which of the following CSS selectors is written with correct syntax?
easy
A. .container > #main .item
B. #main .container > .item#
C. .container #main .item#
D. #main > .container .item#

Solution

  1. Step 1: Check each selector for valid CSS syntax

    Valid selectors use IDs with # before the name, classes with ., and combinators like > properly placed.
  2. Step 2: Identify invalid parts

    Options A, B, and D end with # which is invalid syntax. .container > #main .item is correctly formed.
  3. Final Answer:

    .container > #main .item -> Option A
  4. Quick Check:

    Valid CSS selector syntax = .container > #main .item [OK]
Hint: IDs start with # and never end with # [OK]
Common Mistakes:
  • Placing # at the end of selectors
  • Misusing combinators like >
  • Mixing class and ID syntax incorrectly
3. Given the CSS rules below, which color will the <p> element inside <div id="content"> have?
p { color: blue; }
.content p { color: green; }
#content p { color: red; }
medium
A. Blue
B. Red
C. Black (default)
D. Green

Solution

  1. Step 1: Calculate specificity of each rule

    p is element selector (lowest), .content p has a class and element, and #content p has an ID and element. ID selector has highest specificity.
  2. Step 2: Determine which rule applies

    The #content p rule overrides others because ID selectors beat class and element selectors.
  3. Final Answer:

    Red -> Option B
  4. Quick Check:

    ID selector rule wins = Red color [OK]
Hint: ID selectors override class and element selectors [OK]
Common Mistakes:
  • Choosing class selector color over ID selector
  • Ignoring specificity order
  • Assuming last rule always wins regardless of specificity
4. Why does the following CSS not apply the red color to the <h1> element?
h1 { color: blue; }
#title { color: red; }
.title { color: green; }

HTML:
<h1 class="title" id="header">Hello</h1>
medium
A. Because element selectors have higher specificity than ID selectors
B. Because class selectors always override ID selectors
C. Because the ID selector #title does not match the element's ID
D. Because the CSS syntax is incorrect

Solution

  1. Step 1: Match selectors to HTML element

    The element has id="header" and class="title". The selector #title targets an element with ID "title", which does not match.
  2. Step 2: Understand why red color is not applied

    Since #title does not match, its rule is ignored. The class selector .title applies green, which overrides the element selector blue.
  3. Final Answer:

    Because the ID selector #title does not match the element's ID -> Option C
  4. Quick Check:

    ID selector must match element's ID exactly [OK]
Hint: ID selectors must match element's actual ID attribute [OK]
Common Mistakes:
  • Assuming class overrides ID selectors
  • Confusing ID and class selectors
  • Ignoring selector matching rules
5. You have these CSS rules:
.btn { color: black; }
button { color: blue; }
#submit.btn { color: green; }

And this HTML:
<button id="submit" class="btn">Send</button>

What color will the button text be and why?
hard
A. Black, because the first rule always wins
B. Blue, because element selectors override class selectors
C. Black, because class selectors override element selectors
D. Green, because the combined ID and class selector has highest specificity

Solution

  1. Step 1: Calculate specificity of each rule

    .btn is class selector (specificity 0,1,0), button is element selector (0,0,1), and #submit.btn combines ID and class (1,1,0), highest specificity.
  2. Step 2: Determine which rule applies

    The #submit.btn selector wins because it has the highest specificity, so the color is green.
  3. Final Answer:

    Green, because the combined ID and class selector has highest specificity -> Option D
  4. Quick Check:

    ID + class selector beats class or element alone [OK]
Hint: Combine ID and class selectors for highest specificity [OK]
Common Mistakes:
  • Ignoring combined selector specificity
  • Thinking element selector beats class selector
  • Assuming first rule always applies