CSS Variables Explained

CSS, or Cascading Style Sheets, is a powerful tool for styling and formatting web pages. The use of variables is one of the most important concepts in CSS. Variables enable you to store a value in one location and use it in multiple locations throughout your CSS code. When you want to make changes to your design in the future, you only need to update the variable’s value rather than changing every instance of that value in your code.

css variables

Here’s how variables can be used in CSS:

Variable declaration
The first thing you should do is declare your variables. You can accomplish this by using the var() function, which allows you to give your variable a name and a value. As an example:

:root {
--primary-color: #007bff;
--secondary-color: #6c757d;

In this example, we declared two variables – primary-color and secondary-color – and assigned them the values #007bff and #6c757d. We’ve also declared these variables within the:root selector, so they’re accessible throughout the document.

Using CSS Variables
Once your variables have been declared, you can use them in your CSS code. To use a variable, simply call it with the var() function and pass in the variable’s name. As an example:

body {
background-color: var(--primary-color);
color: var(--secondary-color);

The primary-color variable is used to set the background colour of the body element, and the secondary-color variable is used to set the text colour. This makes it simple to change the website’s colour scheme in the future because you only need to update the value of the primary-color and secondary-color variables once.

Using variables in conjunction with calc()
Variables can also be used with the calc() function to perform mathematical operations on values. As an example:

:root {
--spacing: 20px;
.box {
margin: calc(var(--spacing) * 2);

We’ve declared a variable called spacing and assigned it a value of 20px in this example. We then use this variable in the margin property of the.box element, multiplying the value of the spacing variable by 2. By changing the value of the spacing variable, we can easily adjust the spacing between elements.

Variables that have precedence
You can override a variable’s value in a specific context by declaring a new value for the variable within that context. As an example:

.box {
--primary-color: #ff0000;
background-color: var(--primary-color);

In this example, we override the primary-color variable within the.box element, setting it to #ff0000 instead of the default value of #007bff. This enables us to change the colour scheme of a specific element without having to change the rest of the document.

Using variables in CSS to style and format web pages can greatly simplify the process. You can easily reuse values throughout your code and make global changes with minimal effort by declaring variables and using the var() function. Using variables in CSS can help streamline your development process and make your code more maintainable in the long run, whether you’re working on a small personal project or a large-scale website.

Hi there! Designing is my profession. It's my responsibility to guide you to the information you need while ensuring that you enjoy yourself. Let's add some enjoyment to the routine.

Similar Articles



Please enter your comment!
Please enter your name here

Most Popular

%d bloggers like this: