What does the word specificity mean in CSS?

👨‍💻 Frontend Developer 🟡 Often Asked 🎚️ Medium
#CSS #HTML

Brief Answer

CSS specificity is a priority system that determines which style will be applied to an element:

  1. Inline styles — highest priority (1000) 🎯
  2. ID selectors — high priority (100) 🆔
  3. Classes, attributes, pseudo-classes — medium priority (10) 📝
  4. Tags and pseudo-elements — low priority (1) 🏷️
/* Specificity: 1 */
p { color: black; }
 
/* Specificity: 10 */
.text { color: blue; }
 
/* Specificity: 100 */
#title { color: red; }
 
/* Specificity: 1000 */
<p style="color: green;">Text</p>

Full Answer

CSS specificity is like a ranking system in the army. The higher the selector’s rank, the more power it has over element styles! 🎖️

How specificity works — counting system

CSS uses a four-digit counting system:

/* Format: [inline, id, class, element] */
 
/* [0, 0, 0, 1] = 1 */
p { color: black; }
 
/* [0, 0, 1, 0] = 10 */
.text { color: blue; }
 
/* [0, 1, 0, 0] = 100 */
#title { color: red; }
 
/* [1, 0, 0, 0] = 1000 */
style="color: green;"

Selector categories by priority

1. Inline styles — generals (1000)

<!-- Highest priority -->
<p style="color: red;">Red text</p>

2. ID selectors — colonels (100)

/* Specificity: 100 */
#header { background: blue; }
#nav { color: white; }

3. Classes, attributes, pseudo-classes — majors (10)

/* Specificity: 10 each */
.button { padding: 10px; }
[type="text"] { border: 1px solid; }
:hover { opacity: 0.8; }
:first-child { margin-top: 0; }

4. Tags and pseudo-elements — privates (1)

/* Specificity: 1 each */
div { display: block; }
p { margin: 16px; }
::before { content: ""; }
::after { display: block; }

Specificity calculation examples

/* [0, 0, 0, 1] = 1 */
h1 { color: black; }
 
/* [0, 0, 1, 1] = 11 */
h1.title { color: blue; }
 
/* [0, 1, 0, 1] = 101 */
#main h1 { color: red; }
 
/* [0, 1, 2, 1] = 121 */
#main .content h1.title { color: green; }
 
/* [0, 2, 1, 0] = 210 */
#header #nav .menu { color: purple; }

Specificity comparison — who wins?

/* Style battle! */
 
/* Specificity: 1 */
p { color: black; }
 
/* Specificity: 10 - WINS first */
.text { color: blue; }
 
/* Specificity: 11 - WINS second */
p.text { color: red; }
 
/* Specificity: 100 - WINS third */
#content { color: green; }
 
/* Specificity: 101 - WINS fourth */
#content p { color: yellow; }

Priority table

SelectorSpecificityExamplePriority
Inline1000style="color: red"Highest
ID100#headerHigh
Class10.buttonMedium
Attribute10[type="text"]Medium
Pseudo-class10:hoverMedium
Tag1divLow
Pseudo-element1::beforeLow

Special cases

!important — nuclear button

/* !important beats everything (except another !important) */
p { color: red !important; } /* Beats everything */
 
/* Even inline styles! */
<p style="color: blue;">Still red!</p>

Universal selector — zero

/* Specificity: 0 */
* { margin: 0; }
 
/* Specificity: 0 + 10 = 10 */
*.class { padding: 0; }

Combinators don’t count

/* Specificity: 1 + 1 = 2 (combinator > doesn't count) */
div > p { color: blue; }
 
/* Specificity: 10 + 1 = 11 */
.container p { color: red; }

Practical examples

Problem: style doesn’t apply

/* Doesn't work - specificity: 10 */
.button { background: blue; }
 
/* Works - specificity: 100 */
#sidebar .button { background: blue; }
 
/* Or use !important */
.button { background: blue !important; }

Proper style architecture

/* ✅ Good: low specificity */
.card { padding: 20px; }
.card-title { font-size: 24px; }
.card-content { color: #666; }
 
/* ❌ Bad: high specificity */
#main .sidebar .card .card-header .card-title { font-size: 24px; }

Tools for working with specificity

Specificity calculator

/* Use online calculators */
/* https://specificity.keegan.st/ */
/* https://polypane.app/css-specificity-calculator/ */

Browser DevTools

/* In DevTools you can see which styles are overridden */
/* Crossed-out styles = low specificity */

Specificity management strategies

1. BEM methodology

/* Low and predictable specificity */
.block { }
.block__element { }
.block__element--modifier { }

2. Avoid IDs in styles

/* ❌ Bad: too high specificity */
#header #nav .menu-item { }
 
/* ✅ Better: use classes */
.header .nav .menu-item { }

3. Use cascade properly

/* Base styles */
.button { padding: 10px; background: gray; }
 
/* Modifications */
.button--primary { background: blue; }
.button--large { padding: 15px; }

Common mistakes

Overusing !important

/* ❌ Bad: !important everywhere */
.text { color: red !important; }
.title { font-size: 24px !important; }
 
/* ✅ Better: proper specificity */
.content .text { color: red; }
.content .title { font-size: 24px; }

Too specific selectors

/* ❌ Bad: hard to&nbsp;override */
.page .content .sidebar .widget .title { }
 
/* ✅ Better: simple selectors */
.widget-title { }

Simple rules

  1. More specific selector — higher priority 📈
  2. ID > class > tag — hierarchy of power 👑
  3. !important — last resort, use carefully ⚠️
  4. Inline styles — highest priority 🎯
  5. Equal specificity — last one wins 🏁
  6. Keep specificity low — easier to maintain 🛠️

Understanding specificity will help you write predictable and easily maintainable styles! 💪


Want more articles for interview preparation? Subscribe to EasyAdvice, bookmark the site and improve every day 💪