Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

Cascade and Specificity

3m:9s

The cascade is one of the fundamental design principles of CSS. There’s a big reason why CSS is called “Cascading Stylesheets”. But what does cascade actually mean?

CSS allows multiple style sheets to affect the presentation of the elements in your document. This results in numerous instances where different declarations are trying to set a value for the same property on a specific element. Such conflicts need to be resolved in a predictable manner.

All browsers follow the same algorithm (i.e. set of rules) to combine and resolve property values from all these different style sheets. By understanding how the cascade works, you will be able to troubleshoot styling issues more easily. In the simplest terms, order matters when it comes to CSS rules. Given the two rules with equal specificity, the rule that comes last is the one that gets used.

In the earlier chapter on HTML, we mentioned that the class and id attributes of a HTML element can be used as selectors. The element itself can also be a selector. This means that there could be multiple rules with different selectors that apply to the same element. Here is where the concept of CSS specificity comes into play.

Specificity

If you are familiar with the concept of number bases, then you will know that the numeral system we use on a day-to-day basis is a base-10 number system. CSS specificity is not really a numeral system per se, but you can think of it as a base-infinity number system.

CSS as a base-infinity number system

The selectors in the above diagram are, from left to right, in descending order of “strength”. In other words, even if you have a thousand element selectors, one class selector will trump it. And same goes for each category of selector as you move left.

This might be easier if you try to figure out the specificity of the selectors in the following three examples:

Examples of values of specificity

For the first example, we have one element selector, ul, so if we take the specificity values as a four digit system (like in the diagram above), it would have a specificity of 0, 0, 0, 1.

For the second example, we have two class selectors and one element selector, ending up with a specificity of 0, 0, 2, 1.

Finally, in the third example, we have one id selector, one class selector and one element selector, giving you a specificity of 0, 1, 1, 1.

We can conclude that the specificity of the third example’s selectors is the highest of the three. Specificity strength will trump position, i.e. even if you have a weaker selector that comes last, that rule will not apply.

<ul class="ingredients">
<li>4 large eggs</li>
<li>1/4 cup milk</li>
<li>2 tsp. butter</li>
</ul>
<ol class="instructions">
<li>BEAT eggs, milk, salt and pepper in medium bowl until blended.</li>
<li>
HEAT butter in large non-stick skillet over medium heat until hot. POUR IN
egg mixture. As eggs begin to set, GENTLY PULL the eggs across the pan with
a spatula, forming large soft curds.
</li>
<li>
CONTINUE cooking – pulling, lifting and folding eggs – until thickened and
no visible liquid egg remains. Do not stir constantly. REMOVE from heat.
SERVE immediately.
</li>
</ol>
.ingredients li {
color: green;
}
li {
color: purple;
}

The result of the above CSS will render the ingredients list green, and not purple, because the specificity of the first style rule is higher than the second, and hence “wins”. But keep in mind that getting into “specificity battles” is usually not a good idea because it makes your code hard to maintain.

Specificity and order of appearance are only part of the cascade though.