In this video, you moved the stylesheet’s leftover unlayered rules into layers, so one order statement now decides between all of them. Here are the main points and the finished stylesheet.
Key Points
- For normal declarations, when a rule outside any layer and a rule in a layer set the same property, the one outside wins. So an old unlayered page rule, such as
.masthead { margin-bottom: 2rem; }, beats the margin of Teo’s masthead in thecomponentslayer, whatever the order statement says. The old rule doesn’t set padding or colors, so Teo’s still apply. - To bring leftover rules under the order statement, wrap them in
@layerblocks: the borrowed reset in@layer reset { }and the page styles in@layer base { }. No selector or value changes. - The order statement at the top,
@layer reset, base, vendor, components, utilities;, already placesresetandbasebelowvendorandcomponents, so it needs no change. - With every rule in a layer, the order statement decides between layers, and specificity only decides within one layer (for normal declarations). Teo’s
componentsmargin would still win if thebaseblock came after it, because the order in the file doesn’t decide. - In the Styles pane, each rule sits under a heading such as Layer components, Layer base or Layer reset. The pane lists layers from the latest to the earliest, so you read it from the layer that wins a clash down. A lower layer’s declarations still apply wherever no higher layer sets the same property, which is why the reset’s
box-sizing: border-boxstill applies.
The Final Code
styles.css, with every rule in a layer:
@layer reset, base, vendor, components, utilities;
@import url("gallery.css") layer(vendor);
/* Borrowed reset */
@layer reset {
*,
*::before,
*::after {
box-sizing: border-box;
}
img {
display: block;
max-width: 100%;
height: auto;
}
}
/* Page styles */
@layer base {
body {
margin: 0;
padding: 2rem;
font-family: Georgia, serif;
color: #2b2118;
background: #fbf7f0;
}
.masthead {
margin-bottom: 2rem;
}
}
/* Teo's components */
@layer components {
.cover-caption {
font-family: Georgia, serif;
font-style: italic;
text-transform: none;
color: #2b2118;
}
.masthead {
margin-bottom: 4rem;
padding: 1.5rem;
color: #fbf7f0;
background: #2b2118;
}
}
In the next lesson, Protect the Reset Layer, you’ll find out what !important does now that layers exist.