On this page10 sections
  1. Learning Objectives
  2. 1. Inheritance
  3. 2. inherit, initial and unset
  4. 3. Specificity: Who Wins?
  5. 4. !important
  6. 5. The Full Order
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 026: Inheritance, Specificity & !important

Category6: CSS in Depth
Lecture026 of 057
Estimated video length14 min
TargetCSS3 · any modern browser
LevelIntermediate

"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 !important somewhere overrode everything. Understand these and CSS stops feeling random.


Learning Objectives

  • Know which properties inherit, and control it with inherit, initial and unset
  • 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

html
<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

html
<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>
KeywordMeaning
inheritUse the parent's value
initialUse the property's built-in default
unsetinherit if the property normally inherits, otherwise initial
revertUse 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 partScore
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.

output
p                 → 0,0,0,1
.note             → 0,0,1,0
p.note            → 0,0,1,1
#main p.note      → 0,1,1,1

Example

html
<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

html
<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:

  1. Importance: !important beats normal.
  2. Cascade layer: Lecture 027.
  3. Specificity: higher wins.
  4. 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

  1. Does padding inherit? Does color?
  2. Make a link use its parent's text colour.
  3. What is the specificity of nav ul li a?
  4. Which wins: .btn.primary or #save?
  5. Two rules have equal specificity. Which applies?
  6. Why should you avoid !important?

Practice Answers

  1. padding does not; color does.
  2. a { color: inherit; }
  3. 0,0,0,4 (four elements).
  4. #save (0,1,0,0 beats 0,0,2,0).
  5. The one written later.
  6. 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, unset and revert to control it.
  • Specificity: inline > id > class/attribute/pseudo-class > element. Ties go to the later rule.
  • !important beats everything; use it rarely.
  • DevTools shows which rule won and which were crossed out.