CSS custom properties
Definition
CSS variables make recurring design values centrally nameable and reusable in the stylesheet.
CSS Custom Properties store named values that are used with var() in CSS rules. Their names begin with two hyphens. They participate in cascade and inheritance and can have different values depending on element, state, or design theme.
Simply explained
You give a color a name and use that name in all appropriate places.
Why it matters in practice
Central values facilitate consistent colors, spacing, and variants without having to change the same specifications repeatedly and individually.
Example from business
Possible example: A website uses --accent-color for buttons and adjusts the value for a dark color scheme.
Limits and pitfalls
- Scope, inheritance, and invalid values can produce unexpected results. CSS variables are not the same as preprocessor variables.
Origin & history
Custom Properties are part of CSS in the browser and connect reusable values with its cascade model.
Use in mid-sized companies
- An organization names design values according to their purpose and tests variants and fallback values directly in the affected components.
Frequently asked questions
Can JavaScript change the values?
Yes. Custom Properties are changeable at runtime; this allows, for example, implementing interactive design themes.
Sources
- Using CSS custom properties. · MDN contributors (o. J.): Using CSS custom properties.
Editorially reviewed on: