π What Are Cascade Layers?
CSS cascade layers (using @layer) allow you to control how different groups of styles are prioritized in the cascade βbefore specificity or source order are considered. This gives developers a powerful way to structure CSS clearly and prevent style conflicts. π―
π― Why Do We Need Cascade Layers?
They help solve common CSS problems:
- Avoiding style conflicts between components
- Keeping utility classes predictable (e.g., Tailwind)
- Ensuring project styles override framework defaults
- Making CSS more structured and easier to maintain
π§© Basic Syntax
Creating a Layer
@layer components {
.btn {
padding: 10px;
background: blue;
}
}This creates a layer named components and styles inside it are grouped together in the cascade.
π Layer Priority
Layers are evaluated in the order they are declared. Earlier layers = lower priority. Later layers = higher priority.
Layer Priority Example
@layer reset, base, components, utilities;This declaration sets the order once β even if the actual layers are defined later.
| Layer | Priority |
|---|---|
| reset | Lowest πͺ« |
| base | β¬οΈ |
| components | β¬οΈ |
| utilities | Highest π₯ |
βοΈ How Layers Affect the Cascade
Layers come before specificity and source order in the cascade. That means even a low-specificity rule in a higher layer can override a more specific rule in a lower layer! π€―
Example
@layer base {
p {
color: blue; /* Low layer */
}
}
@layer theme {
p {
color: red; /* High layer */
}
}π Final color: red (theme layer wins)
π§± Creating Multiple Layers
Multiple Layers
@layer reset {
* { margin: 0; padding: 0; }
}
@layer components {
.card { border: 1px solid #ccc; }
}
@layer utilities {
.text-center { text-align: center; }
}π‘ Importing Layers
You can import files into layers:
@import with @layer
@import url("reset.css") layer(reset);
@import url("buttons.css") layer(components);Note
π¨ Layering Framework CSS
Many libraries (Bootstrap, Tailwind, etc.) may conflict with your custom styles. Using layers gives you full control.
Layering Frameworks
@layer framework {
@import url("bootstrap.css");
}
@layer custom {
button {
background: purple;
}
}Even if Bootstrap uses stronger selectors, your custom layer wins because itβs higher priority.
π§ Layers + Specificity Example
Layer + Specificity Clash
@layer low {
#title {
color: blue;
}
}
@layer high {
h1 {
color: red;
}
}β Even though #title is more specific,h1 wins because it's in a higher layer.
π οΈ Best Practices
- Define layer order at the top using @layer a, b, c
- Use layers to organize CSS (reset β base β components β utilities)
- Put third-party CSS in low layers and your projectβs overrides in higher layers
- Use layers with frameworks to avoid !important wars π₯
π Real-World Layer Structure
Recommended Structure
@layer reset, base, theme, components, utilities;
/* reset */
@layer reset {
*, *::before, *::after {
margin: 0;
padding: 0;
}
}
/* base */
@layer base {
body {
font-family: sans-serif;
}
}
/* theme */
@layer theme {
:root {
--color-primary: #4f46e5;
}
}
/* components */
@layer components {
.btn {
padding: 8px 16px;
}
}
/* utilities */
@layer utilities {
.text-center { text-align: center; }
}