Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Variables in CSS?

In this blog post, we will discuss variables in CSS, a powerful feature that is commonly used in modern web development. We will start with an introduction to variables, explain their usefulness, and provide examples to help understand the concept better. This post is aimed at readers who are new to programming or have limited experience with CSS. We will avoid using jargons as much as possible, and if we do use any, we will make sure to explain them in simple terms.

What are Variables?

In programming, a variable is a container that stores a value. We can think of a variable as a label or a name attached to a piece of data. Variables allow us to refer to the data using the variable's name, making it easier to manipulate and work with the data. In the context of CSS, variables are called "Custom Properties."

CSS, short for Cascading Style Sheets, is a style sheet language used to describe the look and formatting of a document written in HTML. CSS allows us to apply styles (such as colors, fonts, and spacing) to specific elements of a webpage.

Why do we need Variables in CSS?

Before diving into the details of CSS variables, let's take a moment to understand why we would want to use them in the first place.

Imagine you have a webpage with multiple elements that share the same style attributes, like color or font size. Without variables, you would need to apply the same style to each element individually. This approach can quickly become tedious and difficult to maintain, especially when there are many elements with shared styles.

By using variables, you can store a style attribute's value (like a color or font size) in one place and then refer to it throughout your stylesheet. This approach makes it easier to update the style in one place, and the change will automatically apply to all the elements that use that variable.

CSS variables also make your stylesheets more readable and easier to understand since you can give meaningful names to your variables, indicating their purpose.

How to Declare a Variable in CSS

In CSS, you can declare a variable using a custom property. A custom property starts with two dashes (--) followed by the variable name, and it is assigned a value using a colon (:).

Here's an example of declaring a variable called --primary-color and assigning it the value blue:

:root {
  --primary-color: blue;
}

In this example, we have defined the variable --primary-color inside the :root selector. The :root selector refers to the highest-level element in the document (usually the <html> element), making the variable globally available throughout the entire stylesheet.

How to Use a Variable in CSS

To use a variable in your CSS, you need to use the var() function. The var() function takes the variable name as its argument and returns the value stored in the variable. You can then use this value in your style declarations.

Here's an example of using the --primary-color variable to set the background color of a button:

button {
  background-color: var(--primary-color);
}

In this example, we use the var(--primary-color) function to retrieve the value stored in the --primary-color variable (in this case, blue) and use it as the background color for the button element.

Updating the Value of a Variable

One of the advantages of using variables is the ability to update their values easily. To update the value of a variable, you can simply change its value in the custom property declaration.

For example, let's say we want to change the primary color of our webpage from blue to red. We can update the value of the --primary-color variable like this:

:root {
  --primary-color: red;
}

Now, all elements that use the --primary-color variable will automatically have their styles updated to reflect the new color.

Using Variables with Fallback Values

Sometimes, you may want to use a variable only if it has been defined. If the variable has not been defined, you might want to use a default value instead. The var() function allows you to provide a fallback value as a second argument, which is used if the variable is not defined or has an invalid value.

Here's an example of using a fallback value with the var() function:

button {
  background-color: var(--primary-color, green);
}

In this example, if the --primary-color variable is not defined or has an invalid value, the background color of the button will be set to green.

A Practical Example

Now that we understand how to declare, use, and update variables in CSS, let's look at a practical example.

Let's say we're building a webpage with a header, some content, and a footer. We want the header and footer to have the same background color, and we want to use a specific font for all the text on the page.

We can use CSS variables to achieve this:

/* Declare the variables */
:root {
  --header-footer-bg-color: darkblue;
  --main-font: Arial, sans-serif;
}

/* Apply the variables */
header, footer {
  background-color: var(--header-footer-bg-color);
}

body {
  font-family: var(--main-font);
}

In this example, we've defined two variables: --header-footer-bg-color for the background color of the header and footer, and --main-font for the font family. We then apply these variables to the appropriate elements in our stylesheet.

By using variables, we've made it easier to update the styles for our webpage. If we decide to change the background color of the header and footer, we only need to update the value of the --header-footer-bg-color variable, and the change will automatically apply to both elements.

Conclusion

CSS variables (custom properties) are a powerful feature that can help make your stylesheets more maintainable, readable, and easier to work with. By using variables to store values, you can reduce repetition, make updates more manageable, and provide meaningful names for your style attributes.

In this post, we've learned how to declare, use, and update variables in CSS, as well as how to use fallback values with the var() function. We've also explored a practical example to demonstrate the benefits of using CSS variables.

As you continue learning about web development and working with CSS, consider incorporating variables into your projects to improve your workflow and make your stylesheets more efficient.