05. How Cascading Works

By now you may be wondering two things:

  1. What the heck does "cascading" mean in "Cascading Style Sheets"?
  2. What happens when two or more CSS properties overlap?

These two questions are actually very much related! In this lesson, we will cover these two questions.

The Cascading Conundrum

When there are multiple rules for a single elements, CSS follows a hierarchical rule for reconciliation.

Cascading Hierarchy

  1. Default Styles

    The lowest level. These play as a fallback in case no styles can be loaded on the browser. This is why some styles are different based on browser.

  2. User Styles

    These are the user's styles, which can be set within the browser properties.

  3. External Style Sheets

    Style sheets that were imported using the HTML <link> tag.

  4. Internal Style

    Style sheets within the document using the HTML <style> tags.

  5. Inline Styles

    Style embedded within its own HTML tag.

Even throughout this list, we are bound to find overlapping properties. Thus, there are three more rules that the browsers follows.

1) The Last Rule

If two or more rules apply to same element, the last one will take hold.

p { 
  color: blue; 
}
  
p { 
  color: purple;
}

The <p> tag will display a purple color.

2) Specificity

The selector that has higher specificity always takes hold.

By this, we mean the CSS selectors that directly choose an element precede over general properties that are inherited.

p {
  color: blue;
}
 
div #logo p {
  color: red;
}
The <p> tag embedded within a <div> with the ID "logo" tag will display a red color.

3) Inheritance

Some properties will be inherited from its parent elements.

Whether or not a property is inherited depends on the property itself. For example, the font-family property is inherited, while the background-color and border properties are not inherited. If you'd like a property to inherited its parent's property, simply write "inherit" as its property value.

p {
  color: inherit;
}

Text color in <p> will be whatever color its parent element is.

Copping-out after hours of debugging

To override any of these rules, use the !important symbol after any property value.

p {
  color: red !important;
}

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Build modern and responsive webpages

HTML & CSS

Build modern and responsive webpages Try Front-end

This book teaches HTML as if it's for anyone - hobbyists, students, and professionals - and it's full-color throughout. It utilizes information graphics and lifestyle photography to explain the topics in a simple way that is engaging. You can progress through the chapters from beginning to end or just dip into topics of particular interest at your leisure.

$ Check price
29.9929.99Amazon 4.5 logo(926+ reviews)

More Front-end resources

Ad