CSS variables, also known as custom properties, allow us to store and reuse values within stylesheets. This makes the code more maintainable, cleaner, and more flexible.
It is best to declare variables in the <:root> selector so they are available throughout the entire document. They can be used in different styles with the <var()> function.
The following example shows how to declare and use CSS variables for setting background color and padding.
We can define multiple variables at once, for example colors for different buttons. This simplifies theme management and design consistency.
In the next example, we store two colors in variables and assign them to different buttons.
CSS variables provide a flexible solution for managing stylesheets. Here are some useful tips for their use:
✨ Ask Lara — your AI study partner
Unlock personalized learning support. Lara can explain lessons, summarize topics, and answer your study questions — available from the Go plan and above.
Lara helps you learn faster — exclusive to ReadyTools Go, Plus, and Max members.