LESSON 3.02

How CSS Cascades

Understanding which styles win and why

Discover how the cascade, specificity, and inheritance rules determine which CSS styles take effect when multiple rules target the same element.

๐Ÿ’ก The Concept

When a browser loads a web page, it may find dozens of CSS rules all targeting the same element. Which one wins? That question is answered by the cascade: the system CSS uses to decide which styles get applied. Understanding the cascade is one of the most useful things you can learn, because it explains why your styles sometimes work and sometimes do not.

What Does “Cascading” Mean?

The word “cascading” is the key to understanding how CSS works, and it is also where beginners often get confused. Think of it like a waterfall.

A cascading waterfall

A cascade is a small waterfall flowing down a rocky slope. The water does not follow a single path โ€” it branches out and finds many routes to the bottom. CSS works the same way. When a browser renders a web page, it cascades through all the style rules it can find โ€” from multiple stylesheets, inline styles, and even default browser styles โ€” and follows a specific set of rules to decide which styles actually get displayed.

In a simple project, you might have just one stylesheet. But a real-world business website can easily have ten or more stylesheets all applying styles to the same page. The browser needs to sort through all of them and determine which rules win. Understanding this cascade is essential to writing CSS that behaves the way you expect.

How the Browser Decides: Specificity

When two CSS rules target the same element, the browser uses a concept called specificity to decide which one wins. Think of it as a score: the more precisely a rule targets a specific element, the higher its priority.

Here is the basic order from lowest to highest priority:

  • Browser default styles โ€” every browser has built-in styles that apply before any of your CSS
  • External or internal stylesheet rules โ€” your CSS file or a <style> block in the HTML
  • Inline styles โ€” written directly on an HTML tag using the style attribute (highest priority)

So an inline style always beats a stylesheet rule targeting the same property. You will see this in action in the exercise below.

๐ŸŽฌ Video Demonstration

In this video I demonstrate how the browser looks at conflicting CSS and decides what to display:

Exercise: Try It Yourself

Step 1. Open JSFiddle.net.

Step 2. In the HTML panel, paste this code:

<div style="background-color: gray">
  <h1 style="background-color: green">Which style wins?</h1>
</div>

Step 3. Click Run. The <div> says the background should be gray. The <h1> inside says the background should be green. Which one wins for the heading?

Notice that text color inherits from the parent while background color does not. This is another key part of how the cascade works.

The more specific CSS wins!

The more specific CSS wins! Since the style attribute on the <h1> is more specific to that element than the style on the parent <div>, the heading gets a green background. The gray background still applies to the rest of the <div>.

When you are working on small projects, CSS conflicts are rare. But on larger websites with multiple stylesheets, managing the cascade and making your CSS win becomes both the challenge and the fun of the job. If you want to go deeper, the Mozilla article on Cascading is an excellent resource.

โ“ Frequently Asked Questions

Do I really need to understand this if I am just making a simple website?

For a small project with one stylesheet you will rarely hit conflicts. But when you do hit one, knowing the cascade is the only way to understand why your style is not working. It also builds the foundation for everything more advanced. Developers who skip this concept end up frustrated later when their CSS behaves in ways they cannot explain.

Why isn’t my CSS rule working?

The most common reason is that another rule is winning the cascade. When a style does not apply, check two things first: is a more specific rule overriding it, and could browser default styles be interfering? Opening Chrome DevTools and clicking on the element will show you exactly which rules are active and which are being overridden. Crossed-out rules in the Styles panel mean something else won.

What wins if two rules are equally specific?

Whichever rule appears last in the stylesheet wins. This is called source order, and it is the tiebreaker when specificity is equal. It is also why the order you write your CSS rules in actually matters.

Where do browser default styles come from?

Every browser has its own built-in stylesheet called the user-agent stylesheet. It applies before any of your CSS runs. That is why headings are bold, links are blue and underlined, and paragraphs have spacing even when you have not written a single line of CSS. Your styles override the browser defaults, which is why writing your own rules takes priority.

How do I override browser default styles?

Simply write your own CSS rules targeting the same elements. Your stylesheet rules take priority over browser defaults. For example, if you want links to be black instead of blue, just write a { color: black; }. Many developers start a project with a CSS reset or normalize stylesheet: a set of rules that zeroes out browser defaults so every browser starts from the same baseline. This prevents inconsistencies between Chrome, Firefox, Safari, and other browsers.

What is !important and should I use it?

!important is a CSS declaration that forces a rule to win over all others, regardless of specificity or source order. You add it at the end of a property value, like this: color: red !important;. It sounds useful, but most experienced developers avoid it. Once you start using !important to force styles, you often end up needing more !important declarations to override the first one, creating a tangled mess that is very hard to debug. Reserve it for rare situations where you have no other option.

What is inheritance in CSS?

Inheritance is how some CSS properties automatically pass down from a parent element to its children. Text-related properties like color, font-family, and font-size inherit by default. So if you set color: blue on a <div>, all the text inside that div will be blue unless something overrides it. Layout properties like background-color, border, and padding do not inherit. You can see this in the exercise: the text color inherited from the parent div, but the background color did not.

โ˜… Key Takeaways

CSS does not apply styles randomly. This lesson explained the cascade: the set of rules that determines which style wins when there are conflicts. Next, you will learn three different ways to add CSS to a web page. Each method has its own purpose, and knowing all three gives you flexibility as a developer.

Google Ads

The costs of this website are partially offset by revenue from Google Ads.

Tools of the Trade

HTML/CSS Lessons