Question 1Easy
Two rules have exactly the same specificity and set the same property. Which one applies?
- Neither; the property falls back to its default
- The one with the shorter selector text
- The one that appears later in the cascade
- The first one that appears in the stylesheet
Answer: C. The one that appears later in the cascade
Explanation
When origin, importance and specificity are equal, the later declaration wins.
Step-by-step solution
- Origin and importance are the same, and so is specificity.
- The cascade then uses source order, so the declaration that appears later wins.
- So the later rule applies, which is option C.
Why the other options are wrong
- A. The property does not fall back to its default; one of the two values is used.
- B. The length of the selector text is not used in the cascade.
- D. The first rule loses, because the later one wins at equal specificity.
Tip: When specificity is equal, the rule that appears later in the stylesheet wins.
Question 2Easy
Which selector has the highest specificity?
- #main
- .note
- div p
- p
Answer: A. #main
Explanation
Specificity is compared as (IDs, classes/attributes/pseudo-classes, elements). An ID selector beats any number of classes and elements.
Step-by-step solution
- The selector #main uses an ID, which has specificity (1,0,0).
- A class such as .note is (0,1,0), and an element such as p is (0,0,1).
- An ID beats any number of classes or elements, so #main wins, which is option A.
Why the other options are wrong
- B. A class has lower specificity than an ID.
- C. Div p has two elements, which is (0,0,2), far below an ID.
- D. An element selector has the lowest specificity, (0,0,1).
Tip: Specificity order is inline style, then IDs, then classes, attributes and pseudo-classes, then elements.
Question 3Medium
Two rules set color on the same element: #nav .item a { color: red; } and .menu .item a:hover { color: blue; }. The link is hovered. Which color wins?
- Blue, because its selector contains more parts than the first
- Blue, because a :hover rule always overrides non-hover rules
- Red, because the ID gives (1,1,1), which beats (0,3,1)
- Red, because it appears first in the stylesheet
Answer: C. Red, because the ID gives (1,1,1), which beats (0,3,1)
Explanation
The first selector has one ID. The second has none (two classes plus one pseudo-class gives (0,3,1)). The ID column is compared first, so red wins.
Step-by-step solution
- The selector #nav .item a has one ID, so its specificity is (1,1,1).
- .menu .item a:hover has two classes and a pseudo-class, plus one element: (0,3,1).
- Specificity is compared column by column, and the ID column is first.
- So the first rule wins and the link is red, which is option C.
Why the other options are wrong
- A. Having more selector parts does not win; specificity counts IDs first.
- B. A :hover rule has no special priority over other rules.
- D. The stylesheet order does not matter here, because specificity decides first.
Tip: Compare specificity column by column: IDs, then classes, then elements.
Question 4Medium
An element has style="color: green" inline, and a stylesheet rule #box { color: red; }. Which color is used?
- Red, because ID selectors are the most specific
- Whichever of the two the browser happens to load last
- Green, because inline styles beat stylesheet rules
- The browser's default color, because of the conflict
Answer: C. Green, because inline styles beat stylesheet rules
Explanation
Inline style declarations take precedence over normal declarations from stylesheets. Only an !important declaration in the stylesheet could override them.
Step-by-step solution
- An inline style attribute has higher priority than a rule in a stylesheet.
- The ID selector in the stylesheet does not matter against inline style.
- So the inline color green is used, which is option C.
Why the other options are wrong
- A. The ID is more specific than a class, but inline style beats any selector.
- B. The load order does not matter here; inline style wins.
- D. There is no default color conflict; the inline style is applied.
Tip: Inline styles beat stylesheet rules unless the stylesheet uses !important.
Question 5Hard
What is the specificity of :where(#app .card) p?
- (1,1,1)
- (1,0,1)
- (0,1,1)
- (0,0,1)
Answer: D. (0,0,1)
Explanation
:where() always contributes zero specificity, whatever its arguments, so only the p counts. :is() would instead take the specificity of its most specific argument.
Step-by-step solution
- :where() always has zero specificity, whatever it contains.
- The selector inside :where, #app .card, adds nothing.
- The p element adds (0,0,1).
- So the specificity is (0,0,1), which is option D.
Why the other options are wrong
- A. The ID and class inside :where do not count, so the ID part is not 1.
- B. The class inside :where does not count, so the class part is 0.
- C. The ID and class inside :where do not count, so the middle value is 0 and the last is 1.
Tip: :where() adds zero specificity; :is() takes the value of its most specific argument.