Loading video player…

Move Every Rule Into a Layer

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 the components layer, 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 @layer blocks: 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 places reset and base below vendor and components, 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 components margin would still win if the base block 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-box still applies.

The Final Code

styles.css, with every rule in a layer:

Language: CSS
@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.

00:00 Your stylesheet for The Lantern Press now opens with an order statement. The gallery sits in the vendor layer, and Teo’s captions sit in the components layer.

00:10 The borrowed reset and the page styles are still outside any layer. In this video, you’ll move them into layers of their own. To see why they need to move, look at the masthead Teo designed for the top of the page, a dark band with four rems of space below it.

00:26 Add their rule to the components layer, where a new component belongs. Save, and the dark band appears. However, the gap below it is half the size Teo asked for.

00:41 Inspect the masthead, and look at the Styles pane. At the top is the old page rule for the masthead, with a margin of two rems. It has no layer heading above it, and neither does the reset’s rule below it.

00:55 Further down, under Layer components, Teo’s margin of four rems is struck through. Margin is the only property both rules set, so Teo’s padding and colors still apply.

01:07 As a quick recap: for normal declarations, when a rule outside any layer and a rule in a layer set the same property, the one outside wins. The order statement can’t help, because the old rule isn’t in any layer it names.

01:23 The fix is to give every leftover rule a layer. Wrap the borrowed reset in an @layer reset block. Then wrap the page styles, the body rule and the old masthead rule, in a block for the base layer.

01:39 Every selector and value stays as it was. The rules only move inside the blocks. You don’t need a new order either. The statement at the top already names reset first and base second.

01:53 Now the two masthead rules sit in different layers: two rems in base, and four rems in components. Your turn: pause the video and predict which margin the masthead gets when you save.

02:11 Save, and the gap below the band doubles. Teo’s four rems win.

02:18 The layer order decides this, whatever the order in the file. Components would still win if you moved the base block below it, because the statement names components after base.

02:29 Inspect the masthead again. This time, every rule for it sits under a layer heading. Layer components comes first, with Teo’s margin of four rems. Under Layer base, the old margin of two rems is struck through.

02:45 Scroll down, and Layer reset holds the box-sizing rule. The pane lists the layers from the latest to the earliest. So you read it from the top down, from the layer that wins a clash to the one that loses it.

02:59 The reset still applies, because no higher layer sets box-sizing.

03:05 Here’s the rule: for normal declarations, once every rule is in a layer, the order statement decides between layers. Specificity only decides within one layer.

03:17 Because of that, the stylesheet can grow. A new component goes in the components layer and overrides the page styles, the gallery and the reset wherever they clash, with one plain class.

03:29 Every rule of the issue page now has a layer. However, the reset is now the lowest layer, so any later layer can undo it. In the next lesson, Protect the Reset Layer, you’ll find out what !important does now that layers exist.

Become a Member to join the conversation.