Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

Cascade and Inheritance

At some point, you will find yourself in a situation where multiple CSS rules have selectors targeting the same element. In such cases, a dilemma arises: which CSS rule "wins" and ends up being the one applied to the element in the end.

This is controlled by a mechanism called the "Cascade," but it is also related to inheritance — elements will inherit rules from parents, but not from other elements.

In this chapter, we will clarify what the CSS cascade is, specificity and importance, and how CSS properties inherit different values.

The final style for an element can be specified in many different places, which can interact in complex ways. This complex interaction makes CSS very powerful, but sometimes confusing and hard to debug.

I will try to clarify some of this complexity, and if you don't understand immediately, that's normal, because I'm trying to help you understand one of the most challenging parts of CSS theory.

Cascade

CSS stands for "Cascading Style Sheets." This means that, at the most basic level, the order of CSS rules matters — although things are much more complex than that.

Multiple sets of rules can apply to a single element. To decide the order in which styles should be applied, the standard defines the cascade. The rule selection process is as follows:

Which selectors in the cascade "win" depends on three factors listed in order of weight, with the note that the first can override the last:

Stylesheet Origin and Importance Level

From the origin perspective, there are three types of stylesheets (in ascending order of priority):

Regarding importance, there are two levels:

Depending on origin and importance, the order of applying rules is (in ascending order of priority):

  1. Browser-level rules (user-agent stylesheets);
  2. Normal importance rules from user stylesheets;
  3. Normal importance rules from author stylesheets;
  4. Important rules from author stylesheets;
  5. Important rules from user stylesheets.

Importance

In CSS syntax, there is an element that can be used to ensure that a particular declaration will always win: !important.

The !important value should only be used when there is no other solution, because it can unnecessarily complicate stylesheets and make them very hard to modify and maintain.

🔧 Example of using "!important"

Specificity

Specificity is a way to measure the "weight" of a selector.

As mentioned above, if two sets of rules have the same origin and the same importance, ordering is done according to specificity.

Specificity can be calculated as follows:

The declaration with the highest score is the one that will be applied, because it has the highest specificity.

If there is still a tie between declarations, the difference will be made by the order in which they were declared.

To simplify the process of calculating a selector's specificity, you can use a four-value method: thousands, hundreds, tens, units (four unique digits in four columns).

To better understand, let's look at the table below:

Selector Thousands Hundreds Tens Units Total
h1 0 0 0 1 0001
h1 + p::first-letter 0 0 0 3 0003
li > a[href*="en-US"] > .warning 0 0 2 2 0022
#identifier 0 1 0 0 0100
No selector, inline CSS 1 0 0 0 1000
🔧 Specificity Example

Source Order

As mentioned before, if multiple competing selectors have the same importance and specificity, the third factor that comes into play to determine the winning rule is source order — meaning later rules override earlier rules.

One thing to keep in mind when thinking about cascade theory and rules overriding other rules is that all of this happens at the property level — properties override other properties, not rules completely replacing other rules.

🔧 Source Order Example

When multiple CSS rules target the same element, initially all rules are applied. Only after evaluating conflicts is it determined which rules win and will be applied finally.

🔧 Mixed Rules Example

Inheritance

CSS inheritance is the last piece we need to explore to understand what style is applied to an element. The idea is that some property values applied to an element will be inherited by its children, while others will not.

For example, it makes sense for font and color to be inherited, as the author can set a font for the entire site by applying it to the html element, and it will only be overridden where necessary.

It would be inefficient to have to set the font separately for each element on the site.

Another logical example: properties like margin, padding, border, and background-image are not inherited. If they were automatically inherited, they would need to be manually removed or modified for each child element.

The full list of inherited properties can be found in the CSS documentation.

Controlling Inheritance

CSS provides four values to control inheritance:

Among these, inherit is the most useful when we want an element to explicitly inherit a value from its parent.

🔧 Inheritance Example

Resetting All Property Values

Using the shorthand property all, any of the discussed inheritance values can be applied to almost all properties simultaneously. It is a convenient way to return to a "zero moment" before adding new styles.

  all: initial;
  all: inherit;
  all: unset;
  all: revert;

In the following exercise, try writing a single rule that overrides the color and background-color properties applied by default to links.

🔧 Exercise

🧠 Final Quiz – CSS Cascade and Inheritance

Top