Keeping your CSS organized as a project grows can get tricky fast. The new @layer
rule in CSS helps you manage your stylesheets in clear, structured layers. It’s perfect for controlling how resets, base styles, utilities, and components stack in the cascade — all without weird specificity battles. In this MiniCoursey quick guide, you’ll learn how @layer
works, why it’s a game changer for maintainable CSS, and how to get started today.
What is CSS @layer?
@layer
lets you define named sections of your CSS that stack in a specific order. Think of it like SCSS layers or the BEM approach — but native to CSS and built into the cascade.
Basic Syntax
Use @layer
to declare a new layer and wrap styles inside it.
@layer reset {
*, *::before, *::after {
box-sizing: border-box;
}
}
@layer base {
body {
font-family: sans-serif;
}
}
@layer components {
.button {
padding: 1rem;
border: none;
}
}
Layers apply in the order they’re declared. Styles in later layers override earlier ones if there’s a conflict.
Why Use Layers?
- Keep resets, base styles, and components separate.
- Avoid specificity hacks to override third-party styles.
- Make your cascade clear and predictable.
💡 Pro Tip: Use
@import
with@layer
to organize external CSS files into the cascade too.
⚠️ Common Mistake: Remember, only browsers with Cascade Layers support will respect
@layer
— check support if you have legacy users.
FAQ
Q: Does @layer add specificity?
A: No — it only affects cascade order, not the specificity of individual selectors.
Q: Can I reorder layers?
A: Yes! Declare @layer
statements in the order you want them to apply.
Related Link
👉 Check out previous: CSS :is() and :where() — Write DRY Selectors
Where to Learn More
Explore MDN’s Cascade Layers guide and try layering your resets, utilities, and component styles for a cleaner, conflict-free CSS workflow.
Conclusion
Congrats — you now know how to use @layer
to organize your stylesheets! Take control of your cascade and keep your CSS clean and future-proof.
✨ Bookmark MiniCoursey for more quick & free mini courses!