CSS Custom Properties (variables) let you store and reuse values throughout your stylesheet. They make CSS more maintainable and flexible.
Defining & Using Variables
- Define in
:rootfor global scope:--primary: #2563eb; - Use with
var():color: var(--primary); - Provide fallback:
var(--primary, #2563eb) - Scope variables to selectors for component-specific values
CSS Specificity Recap
- Inline styles: (1,0,0,0) — highest
- ID selectors: (0,1,0,0)
- Class/attribute/pseudo-class: (0,0,1,0)
- Element/pseudo-element: (0,0,0,1)
- Universal: (0,0,0,0) — lowest
Organization Best Practices
- Use a CSS methodology (BEM, SMACSS)
- Group related properties
- Use variables for repeated values
- Keep specificity low
- Use meaningful class names