In this short post, we’ll take a look at CSS variables and how to use them in your website design project, in its most basic form.
First of all, what are CSS variables?
If you’re new to all of this, don’t panic. I’ll try my best to simplify things. It can be quite daunting learning CSS for the first time, but it’s quite simple once you understand it.
CSS variables, also known as custom properties, or cascading variables in the web dev world, are a feature introduced in CSS3 that allows you to define reusable values that can be used throughout your CSS stylesheets. They are denoted by the var()
notation and are similar to variables in other programming languages.
What are the benefits of using CSS variables?
Using CSS variables offers several benefits in web development:
- Reusability and Consistency: CSS variables allow you to define values once and reuse them throughout your stylesheets. This encourages consistency in your design by ensuring that a particular value, such as a color or a font, is consistent across various elements and components of your site.
- Easy Global Styling: By defining variables at the root level (
:root
), CSS variables become globally available, making it convenient to apply consistent styles across your entire website. - Dynamic Styling: CSS variables can be dynamically changed using JavaScript. This enables you to create dynamic themes or provide user-customizable styles. By modifying the value of a CSS variable, you can instantly update multiple elements that use that variable, allowing for a seamless and interactive user experience.
- Easy Maintenance: CSS variables enhance code maintainability. When a common value needs to be updated, you only need to change the variable’s value once, and it will automatically update throughout your stylesheet. This reduces the chances of errors and saves time when making global design changes.
- Calculations and Responsive Design: CSS variables can be used in calculations, allowing for more flexible and responsive designs. For example, you can use a variable to define a base font size and then calculate different font sizes based on that variable. This simplifies responsive design since you can adjust a single variable rather than modifying multiple instances of a value. We’ll look at an example of this later on in this post.
- Fallback Values: CSS variables can have fallback values, which are useful for supporting older browsers or handling cases where a variable is not defined. Fallback values ensure that your styles remain consistent even if a variable is not supported or available, providing graceful degradation. Again, we’ll look at how to do this in an example later on in the post.
Overall, CSS variables enhance code organization, reusability, maintainability, and flexibility, leading to more efficient development and easier management of styles across your projects.
Now let’s dive a little bit deeper, but not too deep 🙂
How to define a CSS variable
OK, as I said, I want to keep this beginner friendly. So, to define a CSS variable, you simply use the --
prefix followed by a name of your choice. So here’s an example:
:root { --primary-color: #ff0000; }
In the example above, --primary-color
is a CSS variable with the value #ff0000
, which represents a red color.
So, how can you use this in your CSS?
It’s easy. You can use the defined variable anywhere in your CSS by referencing it with the var()
function. For instance:
h1 { color: var(--primary-color); }
In the case above, the color
property of the h1
element is set to the value of the --primary-color
variable, which is #ff0000
in our previous example.
Flexibility!
CSS variables provide flexibility and reusability as we reviewed at the start of this post. We also mentioned they can be changed dynamically using JavaScript, making it easier to create themes or adjust styles on the fly, regardless of whether you are coding up a website manually or using a powerful website builder like Bricks Builder with WPCodeBox. Additionally, CSS variables can have fallback values, which are used if the variable is not defined or not supported by the browser.
Here’s an example that demonstrates fallback values:
h1 { color: var(--primary-color, blue); }
In this case, if the --primary-color
variable is not defined, the color
property will default to blue
.
CSS variables to define values
CSS variables can also be used to calculate values, making them even more powerful. For example:
:root { --base-font-size: 16px; } p { font-size: calc(var(--base-font-size) * 1.2); }
In this case, the font-size
property of the p
element will be calculated as 20px (1.2 times the value of --base-font-size
).
Summary
CSS variables are widely supported by modern browsers and are a valuable tool for creating flexible and maintainable CSS stylesheets.
In our Bricks Builder course, we use a third-party plugin called WPCodeBox which allows us to create custom stylesheets where we can add custom variables to define colors and values. Bricks Builder alone doesn’t yet support CSS variables in the color picker or anywhere else. At least as far as I’m aware, but I could be wrong.

Hopefully, you’ve found this post useful. I’ve tried to be as accurate as possible in this post, but from time to time, errors are made. If there is something that is not accurate, please do inform me and I’ll be sure to correct it 🙂
Additional resources
If you wouldn’t like to learn more about CSS variables, check out this page on MDN web docs.