By · Updated

CSS Cascade Layers Generator

Layer Order, Starter Blocks or Imports to Paste, and a Live Precedence Demo

No ratings yet

OneTask is free, with no sign-up, and built by one person.
If this tool saved you time, a coffee helps keep new ones coming.

How the Cascade Layers Generator works

CSS cascade layers let you decide which group of styles wins before specificity is even compared. List your layers from lowest to highest priority and get the @layer order statement, a starter block for each layer or @import lines, and a demo that proves the order works.

Most CSS conflicts come from specificity. A component rule written as .card .badge beats a utility class like .badge-accent because it has two classes against one, so teams reach for !important or longer selectors to win. Cascade layers break that arms race. Styles in a later layer beat styles in an earlier layer regardless of how specific the selectors are, so the order you declare once at the top of the stylesheet becomes the main rule for who wins.

The @layer rule has been supported since 2022 in Chrome and Edge 99, Firefox 97 and Safari 15.4, so it is safe for almost every site today.

How to use the CSS cascade layers generator

  1. Type your layer names, lowest priority first, separated by commas.
  2. Choose whether to write the styles in layer blocks or import separate files into layers.
  3. Set an accent colour to see the demo update.
  4. Copy the code and put it at the very top of your main stylesheet.

What the output contains

Part Example Purpose
Order statement @layer reset, base, components, utilities; Fixes every layer's priority
Layer block @layer base { ... } Adds styles to a layer; blocks can appear many times
Layered import @import url("base.css") layer(base); Puts a whole file into a layer

Known names get starter rules. reset gets box sizing and margin resets, base gets body type and link colour, and theme or tokens gets a custom property. Any other name gets an empty block to fill in. The second-to-last and last layers get the demo badge rules, so you can see a simple class in the top layer beat a more specific selector in the layer below it.

How the cascade decides now

Browsers compare these in order and stop at the first difference:

  1. Origin and importance, such as user-agent styles against author styles, and !important.
  2. Context, such as Shadow DOM scoping.
  3. Layer order. Later layers beat earlier ones, and unlayered styles beat all layers.
  4. Specificity, which is now only compared within the same layer.
  5. Source order, the last rule wins.

Layer order sits above specificity, which is why the demo works.

Tips and pitfalls

Declare the order first. A layer's position is fixed the first time its name appears. If an @layer utilities { } block happens to be read before the order statement, utilities lands at the bottom. Putting the full order statement at the top of the first stylesheet avoids surprises.

Unlayered CSS wins. Any rule not inside a layer beats every layered rule. That is useful for quick overrides, but it also means an old unlayered stylesheet will override your carefully ordered layers. Move older CSS into a legacy layer to tame it.

!important reverses the order. Important declarations in an earlier layer beat important declarations in a later one. That lets a reset layer protect critical rules, such as accessibility settings, but it surprises people. Avoid !important inside layers unless you mean it.

Third-party CSS fits neatly. Import a framework into an early layer with @import url("framework.css") layer(framework); and your own components in a later layer will override it without fighting its selectors.

Imports must come first. @import rules must appear before any other rule except @charset and @layer statements. Put the order statement first and the imports straight after.

Nested layers. A name like components.cards creates a sub-layer, and the dotted name in the order statement controls its position inside its parent layer.

Reading the result

The code panel shows the full stylesheet skeleton in the order the browser will read it. The demo card lists your layers from lowest to highest priority, and the badge shows the colour from your last layer, even though the rule in the layer below it has a more specific selector.

FAQ

Do cascade layers replace specificity?

No, they sit above it. Layers decide between rules in different layers, and specificity still decides between rules inside the same layer. Most conflicts disappear because components and utilities no longer compete on selector weight.

Why do my unlayered styles override everything?

That is the defined behaviour. Unlayered styles form an implicit final layer that beats every named layer. Move them into a named layer, or rely on it deliberately for small page-level overrides.

Can I put a CSS framework into a layer?

Yes. Use @import url("framework.css") layer(framework); near the top of your stylesheet. With the framework layer listed early in the order statement, your own layers override it easily.

What happens in browsers that do not support @layer?

Very old browsers ignore the layer blocks entirely, so the styles inside them do not apply. All major browsers have supported layers since early 2022, so this affects very few visitors.

See also: all Layout & Spacing tools, Shadows & Motion, Shapes & SVG, UI Components, Forms & Inputs, Colour.

Related tools