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;
}

Build modern and responsive webpages

High Performance Web Sites

Build modern and responsive webpages Try Front-end

Want your web site to display more quickly? This book presents 14 specific rules that will cut 25% to 50% off response time when users request a page. Author Steve Souders, in his job as Chief Performance Yahoo!, collected these best practices while optimizing some of the most-visited pages on the Web.

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

More Front-end resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Ad