download

Why does a CSS class beat this ID selector?

Frontend~5 minReviewed 30 Nov 2026Free · No sign-in

Question

A button has id="pay" and class="action". What color is its text? Explain the role of layer order and specificity.

"CSS"
@layer components, theme;
@layer components { #pay { color: red; } }
@layer theme { .action { color: blue; } }
Show hints (2)
  1. "COMPARE THE LAYERS"

    Both declarations are normal author rules. Check what decides which layer wins.

  2. "SAME LAYER ONLY"

    Compare the selectors by specificity after the earlier cascade steps tie.

Show checked explanation

The text is blue. For these normal declarations, theme comes later in the layer order and wins. The ID selector would beat the class if both rules were in the same layer.

The normal, important, and unlayered variants were checked in Chromium.

Walkthrough

The first line fixes the order of the two layers. Both declarations are normal author styles. The cascade picks the later layer, theme, before it compares selector specificity. Moving the #pay rule later in the file while leaving it in components keeps the result blue.

Common mistakes

  • Treating selector specificity as the first step of the cascade.
  • Moving the rule without changing its layer or priority.
  • Assuming that important declarations use the same layer order as normal declarations.

Follow-up questions

  • "What happens if both declarations become !important?" The earlier layer wins for important declarations, so the text becomes red.
  • "What if an unlayered normal rule sets the text to green?" The text becomes green. Unlayered normal author declarations outrank normal author declarations inside named layers.

What a strong answer covers

Name the winning layer and explain when selector specificity enters the comparison.

Open in interactive practice deck →

Original OfferVibe question. CSS cascade sorting order.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.