Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readCSS

Control specificity wars with @layer

Use CSS cascade layers to order normal declarations before specificity and see how important declarations reverse that order.

You wrote .link { color: blue } in your component CSS and the link stays red. A reset on the same page has #app .link { color: red }, and its higher specificity (1,1,0 against 0,1,0) wins whichever file loads last.

Cascade layers let you decide precedence within an origin before the browser compares specificity. For normal declarations, later layers have higher priority.

diagram

Layer order decides before specificity

The cascade from lowest to highest priority: reset, base, components, then unlayered styles. The components rule with less specificity beats the reset rule.

Declare the order first

Put one line at the top of your entry stylesheet, then wrap each stylesheet's rules in its layer.

@layer reset, base, components;

@layer reset {
  #app .link { color: red; }
}

@layer components {
  .link { color: blue; }
}

The first line lists the layers from lowest priority to highest. In Chromium the link computed to rgb(0, 0, 255): the blue rule won with less specificity.

Specificity still decides between rules in the same layer. I moved the components block above the reset block and got the same blue, because the first line fixes the order wherever the blocks sit.

Unlayered styles beat every layer

Within your stylesheets, normal unlayered declarations win over layered ones, regardless of selector specificity.

@layer components {
  .link { color: blue; }
}

a { color: orange; }

With the reset rule from before still in place, the link computed to rgb(255, 165, 0). The bare a selector has a specificity of 0,0,1 and still beat both layered rules.

This bites when you adopt layers in your own CSS and a vendor stylesheet arrives without them. The vendor rules sit outside every layer, so they beat all of yours. Import the vendor stylesheet into a layer you listed first with @import url("vendor.css") layer(reset);. Place the import before style rules.

Important reverses the order

For !important declarations the order flips, and the earliest layer wins.

@layer reset, base, components;

@layer reset {
  #app .strong { font-weight: 400 !important; }
}

@layer components {
  .strong { font-weight: 700 !important; }
}

The element computed to font-weight: 400, the value from reset. An !important color in reset also beat the same property marked !important outside any layer. The first layer has the highest priority among author stylesheet layers for important declarations. Inline styles, user styles and other origins have their own cascade precedence.

I keep !important out of components for that reason. Anything I mark important there loses to a reset rule I forgot about, and I can no longer fix it with a later layer.