Skip to content

Quiz: Specificity Battle

beginner7 min read

Test Your Cascade Intuition

Time to see if you really get the cascade. Each question presents conflicting CSS rules targeting the same element. Your job: determine which rule wins and why. Don't guess -- trace the cascade algorithm step by step: origin, specificity (ID, CLASS, ELEMENT), then source order.

If you get fewer than 4 out of 6, go back to the selectors and specificity lesson. If you get all 6? You understand the cascade better than most professional developers.


Question 1: Class vs Element Count

/* Rule A */
div.container div.wrapper div.card p span {
  color: red;
}

/* Rule B */
.card .text {
  color: blue;
}

The element is a <span class="text"> inside .card inside .wrapper inside .container.

Quiz
Which rule wins for the span?

Question 2: The :where() Surprise

/* Rule A */
:where(#sidebar) .link {
  color: blue;
}

/* Rule B */
a {
  color: red;
}

The element is <a class="link"> inside #sidebar.

Quiz
Which color wins?

Question 3: !important vs Inline Style

This one catches a lot of people.

/* In stylesheet */
.banner {
  background: red !important;
}
<div class="banner" style="background: blue;">
Quiz
What background color does the banner have?

Question 4: Specificity Stacking

/* Rule A */
#nav .menu li a { color: green; }

/* Rule B */
#nav a.active { color: orange; }

/* Rule C */
a.menu-link.active { color: purple; }

The element is <a class="menu-link active"> inside <li> inside .menu inside #nav.

Quiz
Which color wins?

Question 5: The :is() Specificity Trap

This is the sneakiest one on this quiz.

/* Rule A */
:is(.card, #featured) p { color: red; }

/* Rule B */
.card p { color: blue; }

The element is <p> inside <div class="card"> (the element does NOT have id="featured").

Quiz
Which color wins for p inside .card?

Question 6: Cascade Layer vs Specificity

Okay, last one. And it tests whether you understand the newest addition to the cascade.

@layer base, components;

@layer base {
  #main .card .title {
    color: red; /* High specificity: (1, 2, 0) */
  }
}

@layer components {
  .title {
    color: blue; /* Low specificity: (0, 1, 0) */
  }
}
Quiz
Which color wins for an element with class='title' inside .card inside #main?

Scoring Guide

ScoreAssessment
6/6You can trace the cascade algorithm cold. You understand layers, :where(), :is(), and !important interactions.
4-5Strong foundation with some gaps. Review the cases you missed — especially :is() specificity inflation and cascade layers.
2-3You know the basics but the edge cases trip you up. Revisit the selectors and cascade lessons.
0-1Start with the "How CSS Works" lesson and work through the cascade algorithm step by step.