Layer Setup
CSS @layer configuration for explicit cascade control. Define layer order once, then organize styles without specificity battles.
Overview
CSS Cascade Layers let you control which styles win when there are conflicts, regardless of specificity or source order. Vanilla Breeze uses six layers:
- tokens - Design tokens (custom properties)
- reset - Baseline normalization
- native-elements - Styling for standard HTML elements
- custom-elements - CSS-only custom element layouts
- web-components - JavaScript-enhanced components
- utils - Utility overrides (highest layer precedence)
Layers declared first have lowest precedence. Un-layered styles have the highest precedence.
Visual Diagram
Layer precedence flows from bottom (lowest) to top (highest):
Source Code
Copy this to your main CSS file to establish layer ordering.
Usage Examples
Adding Styles to a Layer
Wrap your CSS in a @layer block to place it in the correct layer:
Component Overrides
Add component-specific styles to the web-components layer:
Utility Classes
Utilities go in the highest-precedence layer to ensure they always apply:
Layer Purposes
tokens
Design tokens define your visual language - colors, spacing, typography, etc. They're declared as CSS custom properties on :root and have the lowest precedence since they're just variable definitions.
reset
Browser normalization and sensible defaults. Removes inconsistent default styles across browsers and establishes a predictable baseline.
native-elements
Styles for standard HTML elements like <button>, <input>, <table>, etc. These build on the reset with Vanilla Breeze styling conventions.
custom-elements
CSS-only custom elements like <layout-stack>, <layout-grid>, and <layout-card>. These provide layout primitives without JavaScript.
web-components
JavaScript-enhanced components like <accordion-wc>, <tabs-wc>, and <dropdown-wc>. These extend native HTML with interactive behavior.
utils
Utility classes for one-off overrides. Use sparingly - prefer component-level styling. Utilities have the highest layer precedence to ensure they always work.
Why Layers?
Without layers, CSS specificity wars lead to escalating selector complexity or !important abuse. Layers solve this by separating concerns:
- Utility classes always override component styles (no
!importantneeded) - Component styles always override base element styles
- You can import third-party CSS into lower layers
- Source order within a layer still matters, but layer order takes precedence
Related
Tokens Starter
Design tokens for the tokens layer
Elements
Native and custom element documentation
MDN: @layer
CSS Cascade Layers specification