Cascade Layers (Experimental)
Learn how to control the priority of Crispy styles using CSS Cascade Layers.
When using Crispy components with CSS frameworks that use CSS Cascade Layers (like Tailwind CSS 4), the following configuration is required to override Crispy component styles with the framework's utility classes:
- Use the Crispy CSS version wrapped with
@layerdirective - Set appropriate priority between layers
CSS Cascade Layers is supported in Chrome 99+, Safari 15.4+, Firefox 97+. (Can I Use)
Overview
By default, Crispy CSS is provided without @layer. Unlayered styles defined without the @layer directive always have higher priority than layered styles. (MDN)
To solve this problem, Crispy provides CSS files separately wrapped with @layer.
Configuration
Customize layer order
Adjust the declaration order of seed-base and seed-components layers with other layers appropriately.
Tailwind CSS
When using Tailwind CSS 4 to override Crispy component styles with utility classes, the Tailwind utilities layer must have higher priority than the seed-components layer in the @layer declaration.
Bundler configuration
Add the seed-layered condition to your bundler's module resolution configuration. This ensures that Crispy React components automatically use the layered version when importing CSS internally.
vite.config.ts
Learn about the Vite conditions option.
Layer order issues with Chunk Splitting
When a bundler splits CSS into multiple chunks, the chunk containing @layer seed-components { ... } may load before other @layer declarations, resulting in unintended layer priority.
You can inline the @layer declaration in HTML to ensure it loads before all <link> tags.
Use the transformIndexHtml plugin hook to inject a <style> tag at the beginning of <head>.
Crispy layer structure
Crispy uses two layers.
| Name | Content | File | Notes |
|---|---|---|---|
seed-base | Design tokens, global styles, keyframes | base.layered.css | Requires manual import |
seed-components | Component styles | recipes/*.layered.css | Automatically imported with Crispy React components |
Related Documentation
Tailwind CSS
Learn how to use Crispy tokens as utility classes in Tailwind CSS.