CSS Specificity Questions with Answers

5 practice questions on CSS Specificity (React & Web) with answers and step-by-step solutions. Try 5 samples below, then practise all 5 free in the AptiNest app.

Sample questions

Question 1Easy

Two rules have exactly the same specificity and set the same property. Which one applies?

  1. Neither; the property falls back to its default
  2. The one with the shorter selector text
  3. The one that appears later in the cascade
  4. 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

  1. Origin and importance are the same, and so is specificity.
  2. The cascade then uses source order, so the declaration that appears later wins.
  3. 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?

  1. #main
  2. .note
  3. div p
  4. 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

  1. The selector #main uses an ID, which has specificity (1,0,0).
  2. A class such as .note is (0,1,0), and an element such as p is (0,0,1).
  3. 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?

  1. Blue, because its selector contains more parts than the first
  2. Blue, because a :hover rule always overrides non-hover rules
  3. Red, because the ID gives (1,1,1), which beats (0,3,1)
  4. 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

  1. The selector #nav .item a has one ID, so its specificity is (1,1,1).
  2. .menu .item a:hover has two classes and a pseudo-class, plus one element: (0,3,1).
  3. Specificity is compared column by column, and the ID column is first.
  4. 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?

  1. Red, because ID selectors are the most specific
  2. Whichever of the two the browser happens to load last
  3. Green, because inline styles beat stylesheet rules
  4. 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

  1. An inline style attribute has higher priority than a rule in a stylesheet.
  2. The ID selector in the stylesheet does not matter against inline style.
  3. 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)
  2. (1,0,1)
  3. (0,1,1)
  4. (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

  1. :where() always has zero specificity, whatever it contains.
  2. The selector inside :where, #app .card, adds nothing.
  3. The p element adds (0,0,1).
  4. 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.

Practise all 5 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all React & Web topics · All topics