CSS Variables: A Beginner’s Guide to Effortless and Efficient Web Design

Guillaume Ferber
5 min readApr 7, 2023
Copyright UX Magazine

CSS variables, also known as custom properties, are a powerful tool for creating efficient and maintainable stylesheets. By allowing you to store and reuse values across your CSS code, CSS variables make it easy to keep your styles consistent, reduce code duplication, and simplify updates. In this article, we’ll take a closer look at how CSS variables work and how you can use them to improve your stylesheets.

Understanding CSS Variables

Photo by Alex Knight

CSS variables are a relatively new feature of CSS, introduced in the CSS3 specification. They allow you to store a value in a variable, which can then be used throughout your stylesheet. To define a custom property, you use the -- prefix, followed by the variable name and the value you want to store. Here's an example:

:root {
--main-color: #ff0000;
}

In this example, we’re defining a variable called --main-color with a value of #ff0000 (red). We're also using the :root pseudo-class to scope the variable to the root element of the document.

--

--