0
0
CSSmarkup~20 mins

Specificity rules in CSS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Specificity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
selector
intermediate
2:00remaining
Which CSS rule applies to the paragraph?
Given the following CSS and HTML, which rule will color the paragraph text green?
CSS
p { color: blue; }
#main p { color: green; }
.content p { color: red; }

<div id="main" class="content">
  <p>Hello world!</p>
</div>
Ap { color: blue; }
B#main p { color: green; }
C.content p { color: red; }
DAll rules apply equally, so the text is purple.
Attempts:
2 left
💡 Hint
Remember that IDs have higher specificity than classes or element selectors.
🧠 Conceptual
intermediate
2:00remaining
What is the specificity value of this selector?
Calculate the specificity of the selector ul#nav li.active a:hover.
A0,1,1,3
B0,0,3,3
C0,1,2,3
D0,1,2,2
Attempts:
2 left
💡 Hint
Count IDs, classes/pseudo-classes, and elements separately.
rendering
advanced
2:00remaining
Which color will the button have?
Consider this HTML and CSS. What color will the button text be when rendered?
CSS
<style>
button { color: black !important; }
.btn-primary { color: blue; }
#submit-btn { color: red; }
</style>
<button id="submit-btn" class="btn-primary">Submit</button>
ABlack
BDefault browser color
CRed
DBlue
Attempts:
2 left
💡 Hint
The !important rule overrides normal specificity.
accessibility
advanced
2:00remaining
How does specificity affect focus styles for accessibility?
You have these CSS rules for a link's focus style. Which rule will actually show a visible outline when the link is focused?
CSS
a:focus { outline: 2px solid blue; }
a.special:focus { outline: none; }
.special { outline: 3px solid red; }
AThe link will have no outline on focus.
BThe link will have a 3px red outline on focus.
CThe link will have a 2px blue outline on focus.
DThe link will have a default browser outline.
Attempts:
2 left
💡 Hint
Check which selector has higher specificity and applies on focus.
📝 Syntax
expert
2:00remaining
What error occurs with this CSS selector?
What happens if you write this CSS selector: div > > p?
CSS
div > > p { color: green; }
AThe selector matches <code>p</code> elements that are grandchildren of <code>div</code>.
BThe selector matches all <code>p</code> inside any <code>div</code>.
CThe selector matches only direct child <code>p</code> of <code>div</code>.
DThe CSS is invalid and will cause a syntax error; the rule is ignored.
Attempts:
2 left
💡 Hint
Check the syntax for combinators in CSS selectors.