CSS Variables
CSS variables allow you to attach a value to a variable and reuse that variable throughout the Style Panel inputs.
Last updated
Was this helpful?
CSS variables allow you to attach a value to a variable and reuse that variable throughout the Style Panel inputs.
Last updated
Was this helpful?
Why Use CSS variables?
Consistency – Define a value once, like a color, and access that variable in every style input field.
Speed – Design quickly by selecting predefined variables from autocomplete.
Experimentation – Arrow through autocomplete and get real-time rendering of that variable to see which value looks best in your design.
Parent-child interactions – Have full control over the styles of all child instances when interacting with the parent.
Let’s think of these two concepts as layers or building blocks.
CSS variables are the bottom layer. They comprise individual variable names and values often used for sizes, colors, and other styles with many input options.
For example, you can create one variable per color in your design system. Those colors are going to be used throughout your site and in Tokens.
The values you enter for each style should be defined as variables. This approach ensures consistent designs and allows you to update a value in one place, with the change automatically reflected wherever the variable is used.
With CSS variables, Tokens now often take a more semantic approach, such as calling them Card
, Team Member
, or Testimonial
. Without CSS variables, Tokens were a blend between semantic and utility, such as padding-medium
, font-size-small
, and Card
.
A CSS variable is defined in the Advanced section by using two dashes followed by the variable name, like this:
Then, the variable's value can be anything such as a color, gradient, duration, size, or number.
Once the variable is defined, you can use it on the instance it was defined on and any of its children.
The variables are available in the autocomplete, so you can access variables by typing them in.
The autocomplete search algorithm is very flexible, letting you search by any of the following:
--
var
gray
(variable name)
You can search any part of your variable, and autocomplete will show you the proper results.
CSS variables, by nature, are available in the current instance and any of the children.
Most variables should be defined on the Global Root, which is the highest level of the page and the same for every page. That way, you can define --my-color
, and it's available on every instance on every page.
Some variables are only needed on a specific section or page. You can define these variables on a common ancestor of where they need to be accessed, such as a Box/wrapper.
Here are some use cases for local variables:
Doing an A/B test without modifying the entire design system
Running a seasonal promotion and modifying colors for a section
With CSS variables, you can interact with the parent and modify the styles of any of the children.
Here are the high-level steps to accomplish this pattern:
Take note of the various properties you want to change such as color and background color.
Create a variable on the parent for each style property such as --child-color
and --child-bg
. Leave the values empty for now.
Add the variables to the various children's style inputs, such as setting background to --child-bg
(no style changes will happen yet because the variables don't have values).
Go back to the parent, where the variables are defined, and give them values for the default state.
Switch to the other state, such as hover, and change the variables' values.
Interact with the parent and see the child change!
You can define as many variables as you want and use them on any children where they are defined to create more complex interactions.
The concept of “reusability” is present in both CSS variables and , but they are different and complement each other exceptionally well.
are the next layer. They package up multiple styles. A Card
Token may include padding, color, and gap styles, for example.
Before you create custom variables, be sure to check out — the standard guideline for building with Webstudio. It contains a library of expertly crafted CSS variables.
This syntax is not unique to Webstudio. It’s the official syntax.
Changing a child's design when interacting with the parent (see for more info).