Protect the Reset Layer

Every rule in The Lantern Press’s stylesheet now sits in a layer, and one order statement at the top decides between them. That order puts the borrowed reset at the bottom, so any later layer can undo it. Most of the time, that’s what you want. A component should be able to restyle an image or a heading without a fight.

However, a few rules in a reset should never be undone. In this lesson, you’ll protect one of them with !important, and you’ll be able to predict which !important declaration wins when declarations sit in different layers or outside any layer. Start with something on the page that should be invisible and isn’t.

Keep a Hidden Note Hidden

Ama has written the call for poems for the next issue, and it can’t go live until March. She’s put it in the page already, with the hidden attribute, so all she has to do on launch day is delete one word. Add her note to index.html, inside <main> and above the covers section:

Language: HTML
<aside class="issue-note" hidden>
  <h2>Call for Poems</h2>
  <p>Send us your poems for the Spring 2027 issue from March 1.</p>
</aside>

Save, and the page doesn’t change, because hidden keeps the note out of sight. Teo is styling it ahead of time, as a bordered box with the heading beside the text. Add their rule at the top of the components block in styles.css:

Language: CSS
  .issue-note {
    display: flex;
    align-items: baseline;
    gap: 1rem;
    margin-bottom: 2rem;
    padding: 1rem 1.5rem;
    border: 2px solid #9b2226;
  }

Save again, and the call for poems appears above the covers, months early:

The page shows a box with a red border above the Covers heading, reading Call for Poems beside the line about the spring issue.
The Hidden Call for Poems Showing on the Page

The hidden attribute doesn’t hide anything by itself. It works through a rule in the browser’s built-in stylesheet that sets display: none on elements with hidden. Your own styles beat the browser’s defaults, whatever layer they’re in, so Teo’s display: flex wins. Any component that sets display can unhide an element this way.

The fix belongs in the reset, whose job is to correct browser defaults. A plain display: none in the reset layer won’t do, though, because components is a later layer and Teo’s rule would still win.

The reset needs !important. An important declaration beats a normal one, whatever layer each sits in, so it beats Teo’s plain display: flex. Add this rule inside the @layer reset block:

Language: CSS
  [hidden]:not([hidden="until-found" i]) {
    display: none !important;
  }

The :not() part is a safeguard you can copy as it is. It leaves out hidden="until-found", a value that lets the browser reveal content when a reader searches the page. Forcing that content to display: none would prevent the browser from revealing it. How that value works is outside this lesson.

The i before the closing bracket makes matching the value case-insensitive, so hidden="Until-Found" is left out too. Save, and the note disappears. On launch day, Ama deletes hidden from the <aside>, the selector no longer matches, and Teo’s flex layout takes over.

Try to Outrank the Reset

Teo still wants to see the note while they finish styling it. Their reflex is the one that worked on the gallery’s captions: add !important. Change the first declaration of Teo’s .issue-note rule:

Language: CSS
    display: flex !important;

Now both declarations are !important. For normal declarations, a later layer beats an earlier one, and components comes after reset in the order statement. Before you save, predict: Does the call for poems appear?

The note stays hidden. Right-click the covers heading, choose Inspect, and select the <aside> above it in the Elements panel. Then read the Styles pane:

The Styles pane shows display flex important struck through under Layer components, and display none important live under Layer reset.
The Reset's Important Declaration Beating the Component's

Teo’s display: flex !important is struck through under Layer components, and the reset’s display: none !important is the one that applies, even though reset is listed further down.

If you expected the note to appear, you applied the order you’ve used so far, and it doesn’t hold here. For !important declarations, the cascade runs the layer order backward: the earliest layer’s !important declaration wins. The reset comes first in the order statement, so its display: none !important beats the one in components.

For !important declarations, the earliest layer wins. That backward order also applies to styles outside any layer: an important declaration there loses to one in any layer. With the order statement @layer reset, base, vendor, components, utilities;, important declarations rank from strongest to weakest like this:

  1. reset
  2. base
  3. vendor
  4. components
  5. utilities
  6. Outside any layer

The order is reversed on purpose. The early layers hold the code you build on, such as a reset or a third-party library. The reversal lets those layers mark the few declarations that must hold, and no rule in a later layer or outside any layer can undo them, not even with !important. The reset’s rule keeps [hidden] hidden this way, whatever a component sets.

The one exception is an !important in an inline style attribute in the HTML, which beats every layer’s important declarations.

To try every combination, use the playground below. It’s a stripped-down stylesheet with only two layers, where the reset layer, the components layer, and a rule outside any layer each set the color of a poem title. Toggle !important on each declaration, and switch the order statement, including a version that sits at the bottom of the file:

Interactive diagram — enable JavaScript to view.

Among the important declarations, whichever layer comes first in the layer order wins, and the declaration outside any layer wins only when neither layer uses !important. The statement at the bottom changes nothing, because each layer’s place was already fixed where its name first appeared.

To preview the note, Teo removes hidden from the HTML while they work, because the attribute is the switch. Delete the !important from .issue-note, then delete hidden from the <aside> and save to see the styled note.

When you’re done looking, put hidden back, save again, and check that the note disappears, since it mustn’t go live before March. Keep !important in the low layers, for the few things that must never change, and leave it out of components.

If the backward order still feels slippery, the button below opens a chat with the RealJS mentor, with a question already typed that you can edit before you send it:

Work Through Three More Cases

The same rule decides any mix of layers. The poem cards on Ama’s next page use a .poem-title class. Say three people style it in the stylesheet you’ve built, which starts with @layer reset, base, vendor, components, utilities;:

Language: CSS
@layer base {
  .poem-title {
    color: #9b2226 !important;
  }
}

@layer components {
  .poem-title {
    color: #2b2118 !important;
  }
}

.poem-title {
  color: #595959 !important;
}

Here’s how each case plays out, and why:

  1. As written, each poem title is brick red, #9b2226. Among important declarations, base is the earliest layer, so it beats components, and the unlayered declaration loses to both.
  2. With all three !important flags deleted, the title turns gray, #595959. Normal declarations follow the order you met first: a rule outside any layer beats every layer.
  3. With the [hidden] rule moved, !important and all, from reset into utilities, restoring !important to Teo’s display: flex in components would unhide the note. For important declarations, components comes before utilities, so the component wins. The protection works only because the rule sits in the earliest layer.

To check that you can explain the rule, not only apply it, set up a case in the playground above and note which color wins. Use the prefilled question below to ask the mentor for feedback. Before you send it, add the toggles you chose, the winning color, and your explanation of why it wins:

The reset is now protected, and the components layer has started to fill up with captions, a masthead, and a note. In the next lesson, Nest Layers and Roll Back With revert-layer, you’ll organize that growing layer from the inside.

Become a Member to join the conversation.