Variables may be thought of as a means to store and reuse values. The core advantage of using variables is the reusability factor – enabling developers to reuse the same value & when meaningfully named, variables can enhance code readability.
We can use these variables inside our CSS as well, termed CSS Variables – these are simple variables that developers declare to reuse particular CSS properties throughout a page, either globally or locally. The syntax is easy enough: specify the property, such as - heading-size: 30px, then use it using the var() method.
Many web developers even use SASS, a CSS pre-processor, that served as an extension of CSS & provides more advanced properties. We can specify variables in SASS, along with a variety of additional features not available in regular CSS. The approach is the same, but we save a variable value using the $ sign rather than two hyphens.
However, since SASS is a preprocessor, the SASS variable turns into normal CSS when compiled. Because of that, SASS variables do not exist in the generated code and hence, you cannot access them in your browser. This makes using variables inside native CSS better for development when compared to SASS.
On the other hand, thanks to JavaScript, we can access CSS variables inside the browser and modify their values.
To properly understand the benefits of CSS variables, let's first look at what it was like before they were used.
To be more specific, we can declare CSS properties without declaring CSS variables.
For example in index.html:-
In the same way, we have to define inside about.html or project.html files.
Duplicating the same code on different pages is bad practice, and it could lead to unexpected errors as well.
All of these issues can be resolved by using CSS variables.
Modern apps tend to have several pages & screens and are complex to build, therefore it becomes necessary to create designs and prototypes before starting the actual development. These designs need to have uniform colors, fonts, and styling to convey a brand identity & theme.
It becomes increasingly challenging when we have to define several of these properties. So, we can simply prevent errors by establishing a variable with a distinct name dependent on the page.
To begin with, it makes no sense to use CSS variables for each of your CSS properties -- you only need to convert those properties you plan to reuse in your app into variables.
So, when specifically? Assume you are specifying the same background or text color for a number of pages or sections. If you use the same CSS attributes, such as background color for several parts, adopting CSS variables in this situation is a wise decision. This helps establish the same design language across multiple pages.
We already discussed that CSS variables are custom CSS properties with unique names and values.
It has the following syntax: var(--name, value)
Now let's discuss the two methods by which we may create CSS variables:
Let us explain with the simplest example possible.
Within the body tag, we have specified two tags.
Now let’s define the CSS variables globally.
Now let’s set the CSS variables to the specific HTML tags.
Here’s what the complete code will look like:
You can update your CSS variables dynamically using JavaScript as well, such as changing the button color or showing red borders around an input field to signify some error.
Let’s take an example of a simple website page where the text size increases when a button is pressed.
HTML
Here we are defining a paragraph tag. Following that, we defined two buttons: one to get the font size and the other to change it.
CSS
Now, let’s define a global variable in the root selector. After that, we are setting the font size to the p tag.
Now it’s time to change the font size with the help of javascript.
JavaScript
Here, we define a variable named getProperties, which will hold all of the variables stored in the root selector.
Later, we defined two functions: one to display the current font size and another to set a new font size.
That’s it -- the font size of the paragraph tag will increase from 30px to 60px.
With an example, we learned about CSS variables and how to modify CSS variables with JavaScript.
It’s now time to put it into your project.
All the top frameworks support CSS variables out of the box. And it’s as simple as what we’ve seen in this post.
CSS variables allow builders to reuse their properties and create uniform, scalable design systems for their projects.
However, going from your design file to a high-quality code that uses CSS variables can not only be time-consuming but also a bit challenging for developers since they may not fully understand which properties should be converted to variables.
Locofy.ai refers to your Figma styles to generate CSS variables. Moreover, it detects commonly reused variables, for example, fonts, and padding, & converts them to CSS variables as well.
If you want to learn more about how to convert your Figma or Adobe XD designs to React, React Native, HTML/CSS, Nextjs, and more, check out our docs. Or explore our solution pages for an in-depth look at how Locofy helps you build apps, websites, portfolio pages and responsive prototypes effortlessly using our low-code platform with seamless AI code generation.