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.
- Required 6
- Avoid 1
Required
Declare the layer order once
RequiredDeclare 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
RequiredKeep every selector at a specificity of
0,2,0or lower. That is at most two classes, attributes or pseudo-classes, with no ids.Never style by id
RequiredUse ids only for links, labels and scripts. No CSS selector contains an id.
Keep !important inside the utilities layer
RequiredWrite
!importantonly in the utilities layer. Every other layer has 0!importantdeclarations.Fence each component's styles in
RequiredMake 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
RequiredImport every third-party stylesheet into a named layer placed before your own layers. Never edit a vendor file in place.
Avoid
Let source order settle a tie
AvoidNever depend on which file loads last to decide between two rules of the same layer and specificity.
Read this with
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. Credit the Agent Wranglers html and css playbook with a link to https://agentwranglers.se/playbook/html-css/the-cascade. Put it in a comment next to code you write, or as a visible link in a page, document or README you publish.