The Cascade in CSS

The Cascade in CSS

Element.How also offers premium tutorials! Check them here:

Table of Contents

Understanding how a browser renders your CSS styles requires a good grasp of Cascade in Cascading Style Sheets. If you've been reading user questions on the web relating to CSS, you've probably come across many developers wondering why the browser doesn't render some CSS styles.

More often than not, this hiccup has to do with the CSS cascade algorithm.

The cascading effect is an algorithm embedded in browsers. It decides the style to apply to every HTML element based on the provided CSS styles by the CSS engine, website visitors, and the web designer. The algorithm also resolves conflicts between styles targeting any given element.

For example, what happens when you have an element with an id and class and target it using different selectors? Consider the following illustration.

<p id="nice-id" class="cute-paragraph">Paragraph text here.</p>

You can target the element using different approaches, including the <p> tag, id, and class selectors like below.

<script> 
/* Please login to get the code 
 * The code will be for the The Cascade in CSS tutorial 
 * Found at this URL https://element.how/the-cascade-in-css/ 
 */ 
</script>

The above CSS statements all target the example paragraph. They target the element using its tag, id, and class, respectively. What happens in such a case? What happens when the order of the statements is altered? How about where the element also has inline CSS?

In the following Code Playground, we've changed the order of the style declarations in the different code snippets and given the second paragraph inline CSS.

.cascade-cd1 p {
   color:blue;
}

.cascade-cd1 #nice-id {
   color:red;
}

.cascade-cd1 .cute-paragraph {
   color:pink;
}
 
.cascade-cd1 #nice-id {
   color:red;
}

.cascade-cd1 .cute-paragraph {
   color:pink;
}

.cascade-cd1 p {
   color:blue;
}
  
.cascade-cd1 .cute-paragraph {
   color:pink;
}

.cascade-cd1 p {
   color:blue;
}

.cascade-cd1 #nice-id {
   color:red;
}

Paragraph text here.

Paragraph text here.

You can see that the color of the first and second paragraphs didn't change in all the above three CSS snippets.

Why? The cascading effect in CSS.

The cascading effect algorithm considers, among other things, the order and position of a style, the specificity of the selection, the origin of the styling, and the level of importance of that given style.

Let's have a look at these in detail.

Importance

Access tutorial

$99/one time Purchase access

Includes

  • Gain Access to This TutorialUnlock complete access to the current tutorial:
  • Future UpdatesYou will get access to all future updates to this tutorial.
  • Enjoy Unlimited UsageUse on as many of your own sites or your clients sites as you wish.

    Note that reselling or redistributing is not permitted.

Access everything

$299/one time Purchase All Access

Includes

  • Unlock every premium tutorial on Element.howGet access to the entire library of premium tutorials on Element.how
    Preview premium tutorials
  • Get access to the CSS course for Elementor usersAccess the complete 14 HTML chapters, 30 CSS chapters and 7 Elementor Projects.Learn more
  • Simple CSS Grid For ElementorAn Elementor Addon to Create Awesome Grid Layouts in a Single Click for Containers, Galleries and Loop Grid. Learn more
  • free extra: ShapeDividers.com Premium AccessLifetime Premium Access to ShapeDividers.comVisit ShapeDividers.com
  • 30 day money backNo questions asked money back. Not what you expected? Get a refund.
  • One-time payment of only $299No hidden fees or subscriptions.

    Sales taxes added where applicable.
  • Great supportGet help when you need it. Support includes getting things working as intended.

    Support excludes customization work.
  • Lifetime access to everything Element.howThe price reflects what is currently available on Element.how. All future updates are included, but none are promised. You pay for what is available now, and the rest is a sweet extra.

    I will say that it is definitely my intention to keep adding tutorials to Element.how.
* All prices are USD. Applicable taxes will be charged at checkout. Have a question? See the FAQ or email me.

Element.how also provides premium tutorials showing awesome advanced designs, check them out here.

Looking for something else? Search across 2737 Elements right here:

Checkout the Elementor Addon Finder directly

Leave a Reply