How can you use CSS variables to make everything more dynamic?

Perhaps you've already had that moment when you wanted to change the main colour of one of your projects? Or perhaps you've already experienced those moments when you go back to retrieve the colours of your site's theme? It's all very time-consuming and impractical. Thanks to CSS variables, this will be a thing of the past! Let's find out how.
•
Updated on August 14, 2026
•
5 min read
Believemy logo

You may have already experienced that moment when you wanted to change the primary color of one of your projects? Or maybe you've had those moments when you had to go back to retrieve the theme colors of your site? All of this is time-consuming and very inconvenient. Thanks to CSS variables, that will be a thing of the past! Let's discover how we're going to do it.

 

Let's Start with an Example

Nothing beats starting with a concrete example to learn something new. What we suggest is starting with a site that has two divisions: one presents a welcome title, and the other displays text with contact methods.

The example is very simple, but our goal is not to build something advanced: we just want to learn how to use CSS variables.

Here is the code:

HTML
<div class="bienvenue">
  <h1>This site uses CSS <span>variables</span></h1>
</div>

<div class="contact">
  <p>Contact us on our social networks:</p>
</div>
CSS
body {
  font-family: arial;
  background-color: #121416;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

h1 {
  color: yellow;
}

.bienvenue {
  background-color: green;
  padding: 15px;
  border-radius: 9px;
}

.contact {
  background-color: yellow;
  margin-top: 10px;
  border-radius: 9px;
  padding: 15px;
}

 

Let's Add Our Variables

In our basic example, we can already notice a problem: we are repeating many values.

  • The color yellow which could be our primary color;
  • The color green which could be our secondary color;
  • The internal margins - padding - which are reused identically;
  • The rounded corners - border-radius - with the same angle.

All of this works fine, but it's not viable for large projects. Remember our founding principle as a web developer:

Don't Repeat Yourself.

Our goal would therefore be to isolate these values so that we can reuse them everywhere. In this way, we will create style sheets:

  1. Consistent: with the same values;
  2. Simple: without having to search for the values we used;
  3. Readable: thanks to the variable names.

To do this, we will need to use :root. This special selector allows us to store variables that will be available throughout our style sheet.

CSS
:root {
    /* Our variables here */
}

We will also need to create variables. To create a variable, simply use two hyphens -- followed by the name of the variable, a name that you can invent and that should describe what the variable contains, then accompany it with a colon : and the value with a semicolon ; to finish.

CSS
:root {
    --myFirstVariable: yellow;
}

But wait, you're using a bit of a special syntax, aren't you?

Exactly. To create a variable, you can name it whatever you want and write it in whatever way you prefer, but there is a convention that exists (a convention is a rule that it's good to follow but if not followed, it won't penalize your project or prevent it from functioning correctly): camel case.

Illustration

This rule states that a variable should start with a lowercase letter, and each subsequent word should start with an uppercase letter. Here are some examples:

CSS
:root {
    --myFirstVariable: yellow; /* correct */

    --MyFirstVariable: yellow; /* incorrect */
    --myfirstvariable: yellow; /* incorrect */
    --MYFIRSTVARIABLE: yellow; /* incorrect */
    --myFiRsTVaRiAbLe: yellow; /* incorrect */
}

Well, we know how to write a variable and we've learned what camel case means. However, we still don't know how to use it.

To use a variable, you simply need to call it.

And to call it, we must inform the browser that we want to use a variable by specifying var() instead of the value. Here's an example.

CSS
:root {
    --primaryColor: yellow;
}

h1 {
    color: var(--primaryColor);
}

We start by creating a variable --primaryColor and we use its value by calling it with var(--primaryColor). Simple, right? Here's what our previous example would look like with variables.

CSS
:root {
  --primaryColor: yellow;
  --secondaryColor: green;
  --padding: 15px;
  --borderRadius: 9px;
}

body {
  font-family: arial;
  background-color: #121416;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

h1 {
  color: var(--primaryColor);
}

.bienvenue {
  background-color: var(--secondaryColor, purple);
  padding: var(--padding);
  border-radius: var(--borderRadius);
}

.contact {
  background-color: var(--primaryColor);
  margin-top: 10px;
  border-radius: var(--borderRadius);
  padding: var(--padding);
}

You now have a consistent, simple, and most importantly, very readable code.

 

Fallback Values

There's even more, with variables: fallback values.

We are never truly safe from a variable that might no longer exist or a typo. To avoid these issues, we can specify a value to use if the variable does not exist.

Here's how to use it:

CSS
.contact {
  /* --- */
  padding: var(--padding, 5px);
}

If we have a --padding variable, all is well, but if we remove it, then the padding will use the fallback value.

Thus, we will have 5px.

 

Conclusion

You now master everything you need to know to use CSS variables! If you want to learn more about HTML and CSS, we have a comprehensive course dedicated to these two programming languages. Also, feel free to share this article with your network on your social media. Thank you!

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Creating a random colour generator with JavaScript

Want to practise your JavaScript skills? Try building a random colour generator! In this article, we'll look at how to do it.

December 9, 2024

Syntax highlighting with HTML and CSS

If you have a blog or a portfolio on which you want to present code, whether HTML / JavaScript or CSS code extracts, you have undoubtedly come up against a problem: how do you activate syntax highlighting to make it look as if you had put the code in a code editor? In this article, we'll look at how to do it.

December 9, 2024

Creating a sinking button with CSS

The buttons on a site are the main triggers for action: styling them and making them dynamic is very important in your development process! That's the aim of this article.

December 9, 2024

Link CSS to HTML: 3 Methods Explained Simply

Learn the 3 methods to link CSS to HTML: external stylesheet, internal style, and inline. Code examples, best practices, and common mistakes to avoid.

March 22, 2026

Create an animated gradient background on hover avec CSS

Whether it's to highlight a part of your website or a quote as in this example, generating and adding a gradient to the background can give your website a very modern touch. Find out in this article how to do it easily!

December 9, 2024

Tailwind CSS 4.2: All New Features and Improvements

Discover everything new in Tailwind CSS 4.2: new color palettes, logical properties, webpack plugin and more. The complete guide to mastering this update.

May 5, 2026

Setting up SCSS themes and contexts with NextJs or React

You can't modify a scss variable, because scss converts its code to css, applying the variable values directly. This makes it difficult to set up a light/dark theme on your React or Next project and scss. Fortunately, there are solutions, which we'll be exploring in this article.

December 11, 2024

All about the new :is() and :where() selectors in CSS

Let's take a look at the latest selectors offered by CSS: where and is! These selectors will allow us to reduce our code and select our elements more flexibly.

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.