Chapter 4: Cascade, Specificity, and Inheritance

Why your styles don’t apply, what wins in conflicts, and how to fix specificity wars


This chapter is where I’ll explain one of the most misunderstood aspects of CSS.

Ever wondered:

alt text

Let’s finally understand:


Section 4.1: The Cascade

Cascade = the browser’s decision making process.

When multiple CSS rules match the same element, the browser uses three layers of rules to pick the winner:

1. Origin

Where the CSS comes from:

2. Importance

3. Specificity

How uniquely the rule targets the element

4. Source Order

If specificity is equal, the last rule wins

Example:

/* Rule 1 */
p {
  color: red;
}

/* Rule 2 */
p {
  color: green;
}

Rule 2 wins because it’s later in the file.

But if you add this:

p {
  color: red !important;
}

Now Rule 1 wins, even though it’s earlier.


Section 4.2: Specificity Cheatsheet (With Real Examples)

CSS assigns weight to selectors using a 4-part value:
(a, b, c, d) = inline, ID, class, tag

Selector Specificity
* (0,0,0,0)
div (0,0,0,1)
ul li a (0,0,0,3)
.nav-link (0,0,1,0)
#header (0,1,0,0)
style="..." (1,0,0,0)

Rule of Thumb:


Real World Example:

/* Specificity: (0,0,1,1) */
.card .title {
  color: red;
}

/* Specificity: (0,1,0,0) */
#main {
  color: blue;
}

The second rule wins (ID > class)


Simplifying Specificity with :where()

:where() has zero specificity. It’s great for defaults.

:where(h1, h2, h3) {
  margin: 0;
}

Use it in component libraries or resets to avoid fighting specificity.


Section 4.3: How Inheritance Works (And How to Reset It)

What is Inheritance?

Some CSS properties automatically pass down from parent to child, others don’t.

Inherited by default:

Not inherited by default:


Overriding inheritance:

span {
  color: inherit;      /* takes from parent */
  font-size: initial;  /* resets to browser default */
  line-height: unset;  /* removes inherited or default */
}

Why Some Styles Don’t Apply

You wrote:

button {
  font-family: 'Inter';
}

But the browser shows Times New Roman?

Check if:

Fix:

button, input, select, textarea {
  font-family: inherit;
}

Summary


Learn More


Coming Up Next

In Chapter 5, we’ll cover positioning, z-index, and stacking contexts, the real reason your tooltip disappears behind the modal.

Let’s finally make sense of CSS layering.