DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. CSS Specificity: The Complete Mental Model
XLinkedInReddit
MediumFrontend Engineering

CSS Specificity: The Complete Mental Model

D
DevPrep Team
February 10, 2026·2 min read·0
Table of Contents
  • The Specificity Hierarchy
  • Calculating Specificity
  • The :where() and :is() Difference
  • Cascade Layers
  • Common Mistakes
  • Modern Best Practices

CSS specificity determines which styles win when multiple rules target the same element. Master this and you'll never use !important again.

The Specificity Hierarchy

Inline styles     → (1,0,0,0)
IDs               → (0,1,0,0)
Classes/attributes → (0,0,1,0)
Elements/pseudo   → (0,0,0,1)
Universal (*)     → (0,0,0,0)

Calculating Specificity

/* (0,0,0,1) — one element */
p { color: red; }

/* (0,0,1,0) — one class */
.text { color: blue; }

/* (0,0,1,1) — one class + one element */
p.text { color: green; }

/* (0,1,0,0) — one ID */
#main { color: purple; }

/* (0,1,1,1) — one ID + one class + one element */
div#main.container { color: orange; }

/* Winner: orange (highest specificity) */

The :where() and :is() Difference

/* :is() takes the highest specificity of its arguments */
:is(#header, .nav) a { }  /* specificity of #header: (0,1,0,1) */

/* :where() has ZERO specificity — always */
:where(#header, .nav) a { }  /* specificity: (0,0,0,1) */

/* :where() is perfect for resets and defaults */
:where(h1, h2, h3) { margin: 0; }  /* easily overridable */

Cascade Layers

@layer reset, base, components, utilities;

@layer reset {
  * { margin: 0; padding: 0; }
}

@layer components {
  .btn { background: blue; } /* Always beats reset regardless of specificity */
}

@layer utilities {
  .bg-red { background: red; } /* Always beats components */
}

Common Mistakes

  • !important wars: If you need !important, your architecture is wrong. Use cascade layers instead.
  • Over-qualifying selectors: div.container > ul > li > a.link is fragile. Use .nav-link.
  • Relying on source order: Source order only matters when specificity is equal.

Modern Best Practices

  • Use cascade layers for architectural specificity control
  • Use :where() for low-specificity defaults
  • Prefer classes over IDs for styling
  • Use BEM or utility classes to keep specificity flat

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • The Specificity Hierarchy
  • Calculating Specificity
  • The :where() and :is() Difference
  • Cascade Layers
  • Common Mistakes
  • Modern Best Practices

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.