The cascade problem
Imagine you're styling a button. You write a CSS rule that makes it blue. Then later, you add another rule that makes it red. Which color does the button get?
The browser doesn't just pick randomly โ it follows a set of rules called the cascade. These rules determine which styles win when multiple CSS rules target the same element.
Cascade order: Where CSS comes from
Not all CSS is created equal. The browser applies styles in this order (lowest to highest priority):
- Browser defaults โ The browser's built-in styles (e.g., links are blue and underlined)
- Author styles โ Your CSS in
<style>tags or external .css files - Inline styles โ CSS written directly in the
styleattribute - !important โ Overrides everything (use sparingly!)
Within author styles, there's another layer: specificity. That's where most of the action happens.
Specificity: How the browser scores selectors
Specificity is a number that represents how "specific" a CSS selector is. The more specific, the higher the priority. The browser scores selectors like this:
| Selector Type | Points | Example |
|---|---|---|
| Inline style (style attribute) | 1000 | <div style="color: blue"> |
| ID selector | 100 | #header |
| Class, attribute, or pseudo-class selector | 10 | .btn, [type="text"], :hover |
| Element or pseudo-element selector | 1 | div, p, ::before |
a-b-c-d where a=inline, b=IDs, c=classes, d=elements. Examples: 0-0-0-1 for div, 0-1-0-0 for #header, 0-0-2-1 for .card.active p.
Specificity scoring examples
A higher specificity always wins, even if a rule appears later in the stylesheet. For example:
Even though the second rule appears later, the first rule wins because 0-1-0-0 > 0-0-0-1.
Try it yourself: Specificity battle
In the editor below, you'll see two CSS rules targeting the same element. The more specific rule wins โ see which one takes effect! Try adding more specific selectors to the loser rule to make it win.
Cascade order when specificity is equal
If two selectors have the same specificity, which rule wins? The one that appears last in the stylesheet.
This is why the order of rules in your stylesheet matters when specificity is equal. Cascade down the file โ later rules override earlier ones.
!important โ The nuclear option
!important is a declaration that forces a rule to have the highest priority, overriding even inline styles. It looks like this:
Why you should avoid !important:
- It starts a specificity war. If someone else uses !important, you're stuck in an arms race.
- It makes debugging harder. You can't figure out why a style isn't applying.
- It violates CSS principles. The cascade is supposed to be predictable and overridable.
- There's always a better solution. Increase specificity, restructure your HTML, or reorganize your CSS.
!important, stop. Reconsider your CSS architecture. Nine times out of ten, the real problem is poor specificity planning, not a need for !important.
Inheritance: Which properties pass to children?
Not all CSS properties are inherited. Some properties automatically pass from parent to child elements. Others don't.
Inherited properties (most text properties):
color,font-size,font-weight,line-height,font-familytext-align,text-transform,letter-spacingopacity,visibility
NOT inherited (most layout properties):
width,height,padding,margin,borderbackground,display,positionbox-shadow,transform,filter
Control inheritance: inherit, initial, unset, revert
CSS gives you four keywords to control whether a property is inherited:
| Keyword | What it does | When to use |
|---|---|---|
inherit |
Forces a property to take its parent's value | You want a child to match the parent (e.g., margin: inherit) |
initial |
Sets property to CSS specification default | You want the standard CSS default |
unset |
Removes all styles for that property | Generic reset (works differently for inherited vs non-inherited) |
revert |
Reverts to browser default (newest standard) | You want to undo custom styling but keep browser defaults |
Debug specificity conflicts with DevTools
The browser's Developer Tools show you exactly which CSS rules apply to an element and why.
- Open your browser's DevTools (F12 or Cmd+Opt+I on Mac)
- Right-click an element and select "Inspect" or "Inspect Element"
- In the Styles panel, you'll see:
- Green checkmarks โ Applied styles
- Crossed-out rules โ Overridden by a more specific rule
- The filename โ Where each rule comes from
- Scroll down to see the specificity score for each rule (some tools show it explicitly)
DevTools is your secret weapon for hunting down CSS bugs. If a style isn't applying, DevTools will show you why โ there's probably a more specific rule overriding it.
Task: Below is a CSS stylesheet with 5 specificity conflicts causing wrong colors. Your job: identify each conflict and fix it by adjusting selector specificity. You cannot use !important.
The Problems:
In the Try It Yourself editor below, the left side shows the broken CSS and the right side shows the preview. Fix the CSS so all colors and styles match the intended design:
- .btn should be green (#22c55e)
- .alert.error should be dark red (#7f1d1d)
- #success should be dark green (#065f46)
- .card.highlight should have padding: 20px
- .badge-active should be gold (#d97706)
Hint: Review the specificity rules above. Remember: ID > class > element. To make .btn beat button, try adding an ID or using a more specific class. For equal specificity, the rule that appears LAST wins. Reorder your CSS rules if needed.
Practical CSS specificity tips
- Use classes, not IDs for styling. IDs have specificity 100, which makes them hard to override. Reserve IDs for JavaScript.
- Avoid deeply nested selectors.
.header .nav .menu .item ais too specific. Keep selectors shallow:.nav-link. - Use the cascade to your advantage. More specific rules override general ones โ structure your CSS from general to specific.
- Never use !important in author CSS. Save it only for utility frameworks or third-party overrides.
- Use a naming convention. BEM (Block Element Modifier) makes specificity predictable:
.card__title--active. - Test with DevTools. If a style isn't applying, inspect the element and see what's overriding it.
Key takeaways
- Cascade order (from lowest to highest): browser defaults โ author CSS โ inline styles โ !important
- Specificity scoring: inline (1000) > ID (100) > class/attribute/pseudo-class (10) > element (1)
- When specificity ties, the rule that appears last in the stylesheet wins.
- Some properties are inherited (color, font-size, text-align), others aren't (margin, padding, width).
- Use inherit, initial, unset, revert to control inheritance explicitly.
- DevTools is your debugging friend. Inspect elements to see which rules apply and why.
- Avoid !important. It's almost never the right solution. Fix specificity instead.
Test your knowledge
You've mastered CSS specificity.
Build pixel-perfect designs next.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ