The cascade
Decide which style wins before anyone writes one. Declare the layer order once, cap selector specificity, and never let file order settle a conflict.
- Recommended 5
Recommended
Declare the layer order once
CSS-6Declare every cascade layer in one
@layerstatement at the top of the entry file. Put every style inside a named layer, so no style is left unlayered.Cap selector specificity and keep ids out of selectors
CSS-7Keep every selector at a specificity of
0,2,0or lower. That is at most two classes, attributes or pseudo-classes, with no ids. Use ids only for links, labels and scripts.Keep !important inside the utilities layer
CSS-9Write
!importantonly in the utilities layer. Every other layer has 0!importantdeclarations.Fence each component's styles in
CSS-10Make every selector in a component file start at the component's root. Use the module prefix on each class, or wrap the file in
@scopeon the root class.Import third-party CSS into its own layer
CSS-12Import every third-party stylesheet into a named layer placed before your own layers. Never edit a vendor file in place.
Read this with
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/html-css/the-cascade">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.