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 is CSS specificity?
CSS specificity is a set of rules browsers use to decide which CSS rule applies when multiple rules target the same element. It works like a score system based on selectors used.
Click to reveal answer
beginner
How do inline styles affect CSS specificity?
Inline styles have the highest specificity and override most other CSS rules except for !important declarations.
Click to reveal answer
intermediate
What is the specificity order from lowest to highest?
Element selectors (e.g., div) < Class selectors (e.g., .class) < ID selectors (e.g., #id) < Inline styles.
Click to reveal answer
intermediate
How can you debug specificity issues in the browser?
Use browser DevTools to inspect the element, check the Styles pane to see which rules apply and which are overridden, and look at the specificity scores or crossed-out styles.
Click to reveal answer
beginner
What does the !important declaration do in CSS?
The !important declaration forces a CSS rule to override other rules regardless of specificity, but it should be used sparingly to avoid confusion.
Click to reveal answer
Which selector has the highest specificity?
AID selector (#id)
BClass selector (.class)
CElement selector (div)
DUniversal selector (*)
✗ Incorrect
ID selectors have higher specificity than class, element, or universal selectors.
What happens if two CSS rules have the same specificity?
ABoth rules apply simultaneously
BThe rule that appears first in the CSS file applies
CThe rule that appears last in the CSS file applies
DThe browser randomly picks one
✗ Incorrect
When specificity is equal, the later rule in the CSS file overrides earlier ones.
How can you check which CSS rule is applied to an element?
AUse a text editor
BGuess based on the CSS file order
CCheck the HTML source code only
DUse browser DevTools and inspect the element
✗ Incorrect
Browser DevTools show applied and overridden CSS rules for any element.
Which of these increases specificity the most?
AAdding more element selectors
BAdding an ID selector
CAdding more class selectors
DAdding a pseudo-class
✗ Incorrect
ID selectors add more specificity than classes, elements, or pseudo-classes.
What is the effect of !important in CSS?
AIt makes a rule override others regardless of specificity
BIt lowers the specificity of a rule
CIt disables the rule
DIt only works on inline styles
✗ Incorrect
!important forces a CSS rule to override others, ignoring normal specificity rules.
Explain how CSS specificity works and why it matters when styling a webpage.
Think about how multiple rules can target the same element.
You got /3 concepts.
Describe steps you would take to debug a CSS specificity issue using browser tools.
Use the Styles pane in DevTools to see which rules win.
You got /4 concepts.
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
Step 1: Understand selector types and their specificity
ID selectors have higher specificity than class or element selectors.
Step 2: Compare the given selectors
#header is an ID selector, which beats class .menu and element div.
Final Answer:
An ID selector like #header -> Option A
Quick Check:
ID selector > class selector > element selector [OK]
Hint: ID selectors always outrank class and element selectors [OK]
Common Mistakes:
Thinking class selectors are stronger than ID selectors
Confusing element selectors with class selectors
Ignoring the universal selector's low specificity
2. Which of these CSS rules is correctly written to increase specificity by using multiple classes?
easy
A. .btn, .primary { color: blue; }
B. #btn.primary { color: blue; }
C. .btn .primary { color: blue; }
D. .btn.primary { color: blue; }
Solution
Step 1: Understand how multiple classes increase specificity
Writing selectors like .btn.primary targets elements with both classes, increasing specificity.
Step 2: Analyze each option
.btn.primary { color: blue; } combines two classes without space, increasing specificity. .btn .primary { color: blue; } has a space, meaning descendant selector, which is less specific.
Final Answer:
.btn.primary { color: blue; } -> Option D
Quick Check:
Multiple classes without space increase specificity [OK]
Hint: Combine classes without spaces to increase specificity [OK]
Common Mistakes:
Using spaces between classes which creates descendant selectors
Mixing ID and class selectors incorrectly
Using commas which separate selectors instead of combining
3. Given the CSS rules below, what color will the <p class='text'> element display?
p { color: red; }
.text { color: green; }
#main p { color: blue; }
Assume the paragraph is inside an element with id='main'.
medium
A. Blue
B. Green
C. Black (default)
D. Red
Solution
Step 1: Identify selectors affecting the paragraph
The paragraph matches p, .text, and #main p selectors.
Step 2: Compare specificity of each selector
p has lowest specificity, .text is higher, and #main p has highest specificity because of the ID.
Final Answer:
Blue -> Option A
Quick Check:
ID selector beats class and element selectors [OK]
Hint: ID selectors override class and element selectors [OK]
Common Mistakes:
Choosing class color over ID selector color
Ignoring the element's parent ID context
Assuming order of rules always wins over specificity
The button has class='btn primary' and id='submit'. Which background color will it show and why?
medium
A. Red, because multiple classes increase specificity
B. Yellow, because class selectors are enough
C. Green, because ID selectors have highest specificity
D. No background color due to conflict
Solution
Step 1: Identify selectors and their specificity
#submit is an ID selector, highest specificity. .btn.primary combines two classes, less specific than ID.
Step 2: Determine which rule wins
The ID selector #submit overrides class selectors, so background is green.
Final Answer:
Green, because ID selectors have highest specificity -> Option C
Quick Check:
ID selector beats multiple class selectors [OK]
Hint: ID selectors always override class selectors, no matter how many classes [OK]
Common Mistakes:
Thinking multiple classes beat an ID selector
Ignoring the ID selector's power
Assuming order of rules decides the winner
5. You want to override a third-party CSS rule .card { border: 1px solid black; } but cannot change their CSS file. Which selector below will reliably override their border style without using !important?
hard
A. div.card
B. #main .card
C. .card.primary
D. .card, .primary
Solution
Step 1: Understand the original selector specificity
The original selector .card is a single class selector.
Step 2: Choose a selector with higher specificity
#main .card combines an ID and a class, which has higher specificity than a single class.
Step 3: Verify other options
div.card combines element and class, less specific than ID. .card.primary combines two classes, still less than ID. .card, .primary is two separate selectors, no increased specificity.
Final Answer:
#main .card -> Option B
Quick Check:
ID + class selector beats single class selector [OK]
Hint: Add an ID selector before class to increase specificity [OK]
Common Mistakes:
Using multiple classes but no ID, which may not override
Adding element selectors only, which have low specificity
Using commas which separate selectors instead of combining