On this page10 sections
Lecture 026: Inheritance, Specificity & !important
| Category | 6: CSS in Depth |
| Lecture | 026 of 057 |
| Estimated video length | 14 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
"Why isn't my style working?" Usually the answer is one of three rules: a value was inherited from a parent, a more specific rule won, or an
!importantsomewhere overrode everything. Understand these and CSS stops feeling random.
Learning Objectives
- Know which properties inherit, and control it with
inherit,initialandunset - Calculate specificity and predict which rule wins
- Understand
!important, and why to avoid it
1. Inheritance
Some properties pass from a parent to everything inside it. Mostly these are text
properties: color, font-*, line-height, text-align, visibility.
Example
<style>
.card { color: #2b8a3e; font-family: Georgia, serif; border: 2px solid #2b8a3e; padding: 10px; }
</style>
<div class="card">
<h3>The heading inherits the colour and font</h3>
<p>So does this paragraph, and this <a href="#">link</a>?</p>
</div>Notice that:
- the border is not inherited, because box properties (
border,margin,padding,width,background) aren't; - the link keeps its own blue, because browsers give links a colour of their own.
2. inherit, initial and unset
Example
<style>
.card { color: #c2255c; }
.card a.same { color: inherit; } /* take the parent's value */
.card p.reset { color: initial; } /* back to the browser default (black) */
</style>
<div class="card">
<p>Pink text with a <a class="same" href="#">pink link</a>.</p>
<p class="reset">This paragraph has been reset to the default colour.</p>
</div>| Keyword | Meaning |
|---|---|
inherit | Use the parent's value |
initial | Use the property's built-in default |
unset | inherit if the property normally inherits, otherwise initial |
revert | Use the browser's own default style for this element |
3. Specificity: Who Wins?
When two rules target the same element, the browser scores each selector:
| Selector part | Score |
|---|---|
Inline style="…" | 1, 0, 0, 0 |
Each id (#nav) | 0, 1, 0, 0 |
Each class, attribute ([type]) or pseudo-class (:hover) | 0, 0, 1, 0 |
Each element (p) or pseudo-element (::before) | 0, 0, 0, 1 |
Compare from left to right; the first bigger number wins. A tie goes to the rule written last.
p → 0,0,0,1
.note → 0,0,1,0
p.note → 0,0,1,1
#main p.note → 0,1,1,1Example
<style>
#main p.note { color: green; } /* 0,1,1,1 */
p.note { color: orange; } /* 0,0,1,1 */
.note { color: blue; } /* 0,0,1,0 */
p { color: grey; } /* 0,0,0,1 */
</style>
<div id="main">
<p class="note">Green: the id makes it most specific.</p>
</div>
<p class="note">Orange: p.note beats .note.</p>
<p>Grey.</p>* and the combinators (>, +, space) add nothing. :where() always scores zero, and
:is() / :not() take the score of their most specific argument.
4. !important
!important on a declaration beats every normal declaration, whatever the specificity.
Example
<style>
#title { color: blue; }
h2 { color: crimson !important; }
</style>
<h2 id="title">Crimson, even though #title is more specific</h2>Why to avoid it: the only way to override an !important is another !important, and
stylesheets soon become an arms race. Fix the selector instead. Reasonable uses are
utility classes (.hidden { display: none !important; }) and overriding third-party CSS you
can't edit.
5. The Full Order
When rules conflict, the browser decides in this order:
- Importance:
!importantbeats normal. - Cascade layer: Lecture 027.
- Specificity: higher wins.
- Order: later wins.
Common Beginner Mistakes
1. Expecting borders or backgrounds to inherit. Only mostly-text properties do.
2. Adding !important to "make it work". Find the more specific rule instead (DevTools shows crossed-out declarations).
3. Styling with ids. Their high specificity makes later overrides painful. Prefer classes.
Practice Questions
- Does
paddinginherit? Doescolor? - Make a link use its parent's text colour.
- What is the specificity of
nav ul li a? - Which wins:
.btn.primaryor#save? - Two rules have equal specificity. Which applies?
- Why should you avoid
!important?
Practice Answers
paddingdoes not;colordoes.a { color: inherit; }- 0,0,0,4 (four elements).
#save(0,1,0,0 beats 0,0,2,0).- The one written later.
- It can only be overridden by another
!important, which makes CSS hard to maintain.
Key Takeaways
- Text properties inherit; box properties don't. Use
inherit,initial,unsetandrevertto control it. - Specificity: inline > id > class/attribute/pseudo-class > element. Ties go to the later rule.
!importantbeats everything; use it rarely.- DevTools shows which rule won and which were crossed out.