Specificity Order: Inline, IDs, Classes, Elements
What you'll learn: You'll understand the hierarchy of CSS selector power—from strongest to weakest.
The Specificity Hierarchy
In the last lesson, you learned that CSS specificity uses a point system. But which selectors actually win when there's a conflict? Here's the pecking order, from most powerful to least:
- Inline styles (1,0,0,0) - The heavyweight champion
- IDs (0,1,0,0) - Very powerful
- Classes, attributes, and pseudo-classes (0,0,1,0) - Moderate power
- Elements and pseudo-elements (0,0,0,1) - Least powerful
How It Works in Practice
Think of specificity like military ranks. A general (inline style) always outranks a colonel (ID), who outranks a captain (class), who outranks a private (element selector). Even a hundred privates can't overrule one general.
Example conflict:
p { color: blue; } /* 0,0,0,1 - weakest */
.intro { color: green; } /* 0,0,1,0 - stronger */
#special { color: red; } /* 0,1,0,0 - even stronger */
<p style="color: purple;">Text</p> <!-- 1,0,0,0 - WINS! -->
The text will be purple because inline styles trump everything else in this hierarchy. If there were no inline style, the ID would win with red. Remove the ID, and the class makes it green. Remove the class, and the element selector shows blue.
Why This Matters
Understanding this order helps you predict which styles will actually appear on your page and avoid writing overly specific selectors just to override previous styles.
Key Takeaway: CSS specificity follows a strict hierarchy: inline styles beat IDs, IDs beat classes, and classes beat element selectors—no matter how many weaker selectors you combine.