There are exactly three ways to put the CSS on a page, and they differ in where the rules live, how far they reach, and how the browser resolves the conflicts between them. Every cascade question that you will ever have comes down to these three.
The previous lesson in this module covered "How Do Browser DevTools Expose Any Page's HTML and CSS?", and this lesson is the last one in the How the Web Works module.
What are the three ways?
There is an external stylesheet, there is an embedded style block, and there is an inline style attribute. All three of them can set the same property on the same element, and the browser always has the final say.
<!-- 1. external stylesheet: the normal way -->
<link rel="stylesheet" href="/css/site.css">
<!-- 2. embedded block: for one page only -->
<style>
.card { padding: 1rem; border: 1px solid #ddd; }
</style>
<!-- 3. inline attribute: for one element only -->
<p style="color: #800d4d;">This paragraph is styled individually.</p>The external stylesheet is what almost every project uses.
The embedded block is for the single page that genuinely needs its own rules, and the inline attribute is for a value that is set by the code or is truly one-off, such as a progress bar width or a background colour that the user chose.
Why does the external stylesheet usually win?
It is not because it is more important. It is because of the order in which they are considered.
The cascade resolves the conflicting declarations in this sequence. The later rules beat the earlier rules when the specificity ties. A more specific selector beats a less specific one. And a declaration with the !important beats everything except another !important.
/* the page's stylesheet */
.card {
color: #222;
}
/* a rule inside the same page's <style> block, later in the document */
.card { color: #800d4d; }
/* an inline attribute on the element */
<p class="card" style="color: green;">...</p>Here the green inline value wins, because an inline style has a higher specificity than any selector.
That is exactly why inline styles are a trap. They cannot be overridden by your stylesheet, so a single inline value silently defeats an entire rule.
How does the specificity work?
The specificity is a score that is built from three parts. There is how many IDs, there is how many classes and attribute selectors, and there is how many element names.
The score is compared part by part, like a version number.
p { color: #222; } /* 0-0-1 element only */
.card { color: #333; } /* 0-1-0 one class */
.card p { color: #444; } /* 0-1-1 class + element */
#featured { color: #555; } /* 1-0-0 an id */
p.banner { color: #666; } /* 0-1-1 class + element */You read the scores as "the IDs, then the classes, then the elements".
The #featured beats the .card p, because the 1 beats the 0 in the first position. No amount of classes can outrank an ID.
Two rules with the same score are decided by the order. The one that comes later wins.
0-0-0 (none) the universal selector * only
0-0-1 p, div element names
0-1-0 .card class or attribute selector
0-1-1 .card p class plus element
1-0-0 #featured id
1-1-0 #featured .cardThere are two modern selectors that exist to make this manageable.
The :where() always has a specificity of zero, so it is ideal for a reset or for a default that you intend to override. The :is() takes the specificity of its most specific argument, so it behaves like a grouped selector.
:where(a, button) { color: inherit; } /* 0-0-0 — easy to override */
:is(.btn, .link) { padding: 0.5rem 1rem; } /* 0-1-0 — behaves like a class */What does the cascade look like in practice?
Here is a complete example with all three attachment methods in play, and the result that each one produces.
/* site.css — the external stylesheet */
.card {
color: #222;
background: #f6f6f6;
padding: 1rem;
}<link rel="stylesheet" href="/css/site.css">
<style>
.card { color: #800d4d; } /* later in the document, same specificity: wins */
</style>
<div class="card">
<p style="color: green;">This paragraph is green, whatever the stylesheet says.</p>
</div>The div uses the embedded rule for its colour, and it uses the stylesheet for everything else.
The paragraph inside it is green, because the inline attribute outranks the class selector. That is a small demonstration of how a one-line style attribute can defeat a whole stylesheet.
Which should I use?
- External stylesheet: the default for every project. One place to change the design, cached by the browser, shared by every page.
- Embedded block: the one page's own rules, or a critical block of the styles that you want inlined for the speed.
- Inline attribute: a value that comes from the data, or a genuinely one-off adjustment. Never as a shortcut for the styling.
Whatever you choose, you write the selectors at the lowest specificity that does the job.
A page that is full of !important declarations and long class chains is a page that nobody can restyle later, and that includes you.
How do you keep the CSS from leaking between the pages?
A class name is global. Once you write the .card in a stylesheet, every element with that class on every page of the site inherits your rules, whether you meant it or not. On a small site that is manageable. On a large one it becomes the main source of the strange bugs.
There are four practical defences, and they work together rather than instead of each other.
You name things specifically. The .product-card is far safer than the .card, and the .nav-link than the .link.
You scope with a parent class. The .sidebar .card only applies inside the sidebar.
You scope by page. You put a class on the body, class="pricing-page", and you write the .pricing-page .card.
You use the cascade layers. A layer that is named base can be overridden by a layer that is named components, no matter which rule is more specific.
/* base layer holds the defaults */
@layer base {
.card {
padding: 1rem;
border: 1px solid #d8e0e8;
}
}
/* components layer wins over base even at equal specificity */
@layer components {
.card {
padding: 1.5rem;
}
}The layers are the newest of the four, and they are the most powerful, because they replace the specificity argument with an explicit ordering.
A rule in a later layer beats a rule in an earlier layer, no matter how specific either one is. And a rule with no layer at all beats every layered rule, which is why you declare the layer order at the very top of your first stylesheet.
Quick reference
- There are the three ways: the external stylesheet, the embedded
<style>, and the inline attribute. - The inline styles beat the stylesheet rules. That is the usual cause of "why is this colour wrong".
- The specificity compares the IDs, then the classes and the attributes, and then the element names.
- The equal specificity means that the later rule wins.
- The
:where()has a specificity of zero, and the:is()takes the highest of its arguments. - You use the lowest specificity that works.
Try it yourself: you write one stylesheet and one page that apply the same colour to the same element in all three ways, and then you predict the result before you reload. You change the order of the rules and you reload again. The winner changes, and the specificity does not. Finally, you add an !important to the stylesheet rule and you watch the inline value lose.