Layer the Vendor Gallery

Ama and Teo publish The Lantern Press, a small quarterly poetry zine, and its website has grown one rule at a time, the way most sites do. A borrowed reset, a third-party cover gallery, Teo’s page styles, and a few fixes now share one stylesheet, and they don’t always agree.

In this course, you’ll turn that stylesheet into one that scales, with rules in cascade layers, values from design tokens, and spacing that still works when the zine publishes its first right-to-left poem in Arabic. In this lesson, you’ll make Teo’s caption style beat the gallery’s styles without a longer selector or !important. Start where Teo did, with captions that refuse to change.

Before you can fix a style that won’t apply, you need to see which rule is beating it and why. You’ll work on your own copy of the zine’s site throughout the course. It’s in the code download, linked on the course page and in the overview lesson. Open its folder in your editor and start the live-preview feature or extension, which reloads the page in Chrome or Edge each time you save. Then open DevTools beside index.html.

Each lesson fixes one kind of mess in these files, and you make every change yourself. Where a lesson relies on something you already know, such as specificity or @layer, it offers a one-line recap.

The page shows the zine’s past covers in a grid. Teo didn’t write that grid. It comes from Covergrid, a gallery stylesheet by another developer, which the page links before Teo’s own stylesheet:

Language: HTML
<link rel="stylesheet" href="gallery.css">
<link rel="stylesheet" href="styles.css">

Each cover is a <figure> with Covergrid’s classes, cg-item and, for the current issue, cg-featured. Teo added a class of their own, cover-caption, to every caption:

Language: HTML
<figure class="cg-item cg-featured">
  <img src="covers/winter-2026.svg"
       alt="A lit paper lantern on a deep blue ground."
       width="240" height="320">
  <figcaption class="cover-caption">Winter 2026: Small Hours</figcaption>
</figure>

Covergrid styles its captions as small, gray, uppercase sans-serif text, and the current issue’s caption in brick red. These two rules in gallery.css do it:

Language: CSS
.cg-gallery .cg-item figcaption {
  margin-top: 0.5rem;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 0.75rem;
  text-transform: uppercase;
  color: #595959;
}

.cg-gallery .cg-item.cg-featured figcaption {
  color: #9b2226;
  letter-spacing: 0.1em;
}

Teo wants the captions to match the zine’s poems instead, in italic serif text and the same dark brown as the body text. That’s the last rule in styles.css:

Language: CSS
.cover-caption {
  font-family: Georgia, serif;
  font-style: italic;
  text-transform: none;
  color: #2b2118;
}

Look at the page, though, and the captions are still uppercase sans serif. Right-click the caption under the autumn cover, choose Inspect, and read the Styles pane:

The Styles pane lists the gallery's caption rule above Teo's .cover-caption rule, whose font-family, text-transform and color are struck through.
The Gallery's Caption Rule Beating Teo's Class

Covergrid’s rule sits above Teo’s, and Teo’s font-family, text-transform, and color are struck through. Only font-style: italic gets through, because the gallery never sets it. The gallery wins on specificity.

Its selector has two classes and an element, while Teo’s has one class. Specificity is compared before source order, so linking styles.css second doesn’t help. Teo can’t edit gallery.css either, because the next Covergrid release would overwrite those edits.

The first fix most people try is a selector that’s more specific than the gallery’s. It’s a detour, but it’s worth seeing where it leads. Change the first line of Teo’s rule in styles.css so it has three classes:

Language: CSS
.cg-gallery .cg-item .cover-caption {

Save, and two of the three captions switch to Teo’s style. The current issue’s caption turns serif but stays brick red. Inspect it:

The Styles pane lists the gallery's featured caption rule first, and the color in Teo's longer selector is struck through.
The Featured Caption Still Losing on Color

Covergrid’s featured rule has three classes and an element, one element more than Teo’s new selector, so it wins on color. The natural next move is to stop counting and add !important after Teo’s color. Try it, and all three captions turn brown.

It works, but each fix made the stylesheet harder to change. Teo’s selector now depends on Covergrid’s class names, so a gallery update that renames them breaks it. And once a declaration has !important, the only way to override it, on a poet’s page or anywhere else, is another !important.

Undo both changes so the rule starts with .cover-caption again and has no !important. There’s a way to make Teo’s single class win without fighting over specificity at all.

The gallery wins because it competes with Teo’s styles on equal terms. Both stylesheets are linked, and neither is in a layer. As a quick recap, normal declarations are the ones without !important. For these declarations, rules in a cascade layer are compared layer by layer before specificity is checked, and rules outside any layer beat every layer. Declarations with !important follow a different order, which a later lesson covers.

You can’t wrap Covergrid’s file in an @layer block without editing it, but you can put the whole file into a layer as you load it.

An @import rule loads another stylesheet from inside a stylesheet, as if its rules were written at that point. Adding layer() after the URL puts every imported rule into the named layer. Add this line at the very top of styles.css:

Language: CSS
@import url("gallery.css") layer(vendor);

Then delete the <link> for gallery.css from index.html. If you leave it, the browser loads the gallery twice: once in the vendor layer and once linked, outside any layer, where it beats Teo’s rule as before.

The @import line has to come before every other rule in the file. Put it below even one rule, and the browser ignores it without an error, so the gallery loses all its styles, grid included. There’s also a cost you won’t notice on a small site like this one. The browser finds gallery.css only after it has downloaded styles.css.

Save, and every caption switches to Teo’s italic, serif, and brown style. For now, Teo’s rule wins because it’s outside any layer. However, the plan for this stylesheet is a layer for every rule, so move the caption style into a layer for components:

Language: CSS
@layer components {
  .cover-caption {
    font-family: Georgia, serif;
    font-style: italic;
    text-transform: none;
    color: #2b2118;
  }
}

The captions keep Teo’s style. The vendor layer appears first, on the @import line, and components appears later, so components is the later layer and wins.

If your captions didn’t change, or the gallery lost its grid, check where your @import line sits. The button below opens a chat with the RealJS mentor, with a question already typed that you can edit before you send it. Paste the top of your styles.css after the question so the mentor can see it:

Declare the Layer Order Once

The order you have now comes from where each layer’s name happens to appear, which works until you add more layers. The next lesson fills the reset and base layers, and both belong below the vendor gallery, so a component can override the gallery and the gallery can override the reset. A utilities layer belongs at the very top, for small single-purpose classes that should beat any component.

You can’t get that order from where the names appear, though, because @import has to stay at the top of the file. Any @layer reset block you write would appear after it, and reset would land above vendor.

The one rule allowed above an @import is an @layer statement. It’s the keyword and a list of layer names, ending with a semicolon instead of a block. Make it the first line of styles.css:

Language: CSS
@layer reset, base, vendor, components, utilities;

@import url("gallery.css") layer(vendor);

A layer’s place is fixed where its name first appears, so this statement sets the order for the whole stylesheet, from reset at the bottom to utilities at the top. The reset, base, and utilities layers are empty for now, and that’s fine. Note that an @layer statement at the end of the file can’t reorder layers that have already appeared, so writing @layer components, vendor; there changes nothing.

Save, and inspect the current issue’s caption again:

The Styles pane shows Teo's .cover-caption rule under Layer components, and the gallery's featured rule in the vendor layer below it with its color struck through.
Teo's Class Winning From the Components Layer

The Styles pane now groups the rules by layer, under headings such as Layer components and Layer vendor. Teo’s one-class rule in components comes before the vendor group, and Covergrid’s featured rule, with three classes and an element, has its color struck through.

This is the rule from the recap earlier. For normal declarations like these, layers are compared before specificity, so any rule in a later layer beats any rule in an earlier one, however specific.

To make sure you could explain it to Teo, put it in your own words and ask the mentor to check your explanation:

The borrowed reset’s box-sizing rule shows above both groups because it’s still outside any layer, like the page styles. The reset and page styles don’t touch the captions, so they cause no trouble yet, but they’re the next thing to sort out.

Check Your Understanding

Try these before you open the answers:

  1. Teo finds a lightbox stylesheet, lightbox.css, and wants it in the vendor layer with the gallery. What line do they add, and where?
  2. Ama adds the order statement @layer reset, base, vendor, components, utilities; at the bottom of styles.css instead of the top. Does the order still put reset below vendor?
  3. After you added the @import line, why did the <link> for gallery.css have to go?
  1. Add @import url("lightbox.css") layer(vendor); next to the gallery’s @import line: below the @layer statement and above every other rule. Two imports can share a layer.
  2. No. The @import line already put vendor first, so reset and base first appear later, above it. Only a statement at the top, where the names first appear, sets the order.
  3. The linked copy isn’t in any layer, and for normal declarations like Covergrid’s, rules outside any layer beat every layer, so the gallery’s captions would win again.

In the next lesson, Move Every Rule Into a Layer, you’ll fill the reset and base layers with the rules that are still outside any layer and read the result in the Styles pane.

Become a Member to join the conversation.