@layer
Widely availableSince 202297% of users
A way to organize your CSS into 'layers' of priority. It allows you to ensure that certain styles (like a theme) always override others (like a reset), regardless of selector specificity.
What's modern
From web-features, MDN's browser data and our snippets, checked dailyModernIn every major browser since March 2022. Widely available since September 2024.
The old way and the modern waySee the snippet: Organize CSS without specificity wars →
Old way 33 lines
/* Base styles */
body {
margin: 0;
font-family: sans-serif;
}
/* Components - start simple */
.card {
padding: 1rem;
background: white;
border: 1px solid #ccc;
}
.card h2 {
margin-top: 0;
color: #333;
}
.card.highlight {
background: yellow;
}
.card.highlight h2 {
color: red;
}
/* Utilities - need more specificity or !important to override components */
.mt-4 {
margin-top: 1rem !important;
}
.mt-2 {
margin-top: 0.5rem !important;
}
/* If utilities still don't win, add more specificity */
.sidebar .card .mt-4 {
margin-top: 0.5rem !important;
}
/* specificity wars: stacking selectors, fighting !important */Modern 31 lines
@layer reset, base, components, utilities;
@layer reset {
* { margin: 0; padding: 0; }
}
@layer base {
body {
font-family: sans-serif;
}
}
@layer components {
.card {
padding: 1rem;
background: white;
border: 1px solid #ccc;
}
.card h2 {
margin-top: 0;
color: #333;
}
.card.highlight {
background: yellow;
}
.card.highlight h2 {
color: red;
}
}
@layer utilities {
.mt-4 { margin-top: 1rem; }
.mt-2 { margin-top: 0.5rem; }
}
/* layers handle priority, no !important needed */See how modern your own CSS is.Check your CSS →
Quick example
@layer reset, base, theme;
@layer theme {
/* These styles have higher priority because 'theme' is defined last */
button { background: blue; }
}
@layer reset {
button { background: gray; }
}@layer browser support
BaselineWidely availableThis feature is well established and works across many devices and browser versions. It has been available across browsers since 2022.Safe to use without fallbacks.
Usage97%of people can use this3% use a browser without it.
Browsers
99+
97+
15.4+
99+Mobile
99+Android
97+Android
15.4+iOS