🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Guides › Cascading_variables › Using_custom_properties
Using CSS custom properties (variables) - CSS | MDN
Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that represent specific values to be reused throughout a document. They are set using the @property at-rule or by custom property syntax (e.g., --primary-color: blue;). Custom ...
🌐
W3Schools
w3schools.com › css › css3_variables.asp
CSS Variables - The var() function
The var() function is used to insert the value of a CSS variable.
Discussions

How to declare variables in css - Stack Overflow
Can anyone tell me why it is declared var(--color), what is the difference in declaring only the variable? ... Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific values to be reused throughout a document. More on stackoverflow.com
🌐 stackoverflow.com
What are the must have CSS Variables?
State variables. Stuff like: a, button, input { --hov: 0 } :is(s, button, input):is(:hover, :focus) { --hov: 1 } Then --hov can be used to adjust any property (background, color, box-shadow, transform). Or for theme switching: body { --dark: 0; /* setting the theme to auto */ @media (prefers-color-scheme: dark) { &:has([data-mode='auto'][aria-pressed='true']) { --dark: 1 } } /* setting the theme to dark */ &:has([data-mode='dark'][aria-pressed='true']) { --dark: 1 } } Then --dark can be used to switch background, color (and more) using color-mix(). --perc: calc(var(--dark)*100%); background: color-mix(in srgb, #111 var(--perc), #eee); color: color-mix(in srgb, #eee var(--perc), #111); Or for switching between wide narrow states using media/ container queries. Above a certain wiewport/ container width, --wide is 1, below it's 0. Then --wide can get used to set grid-template, grid-area values and more. More on reddit.com
🌐 r/css
13
11
April 7, 2025
How do you guys name your css variables ?
There are 2 hard problems in programming. Naming things, cache invalidation, and off by one errors. More on reddit.com
🌐 r/webdev
152
91
March 6, 2024
Am I the only one who thinks that the use of custom-properties worsens the readability of css code?
One example where custom properties really shine is if your web app follows a design system. You want to have single source of truth for all of the design tokens (colors, spacings, fonts, etc.), so that when something inevetably changes, you don't have to change that in 100 places but just 1. How would you handle that without custom properties? Custom properties (also known as css variables) are used when you want to share a value between multiple properties. I personally find them completely readable and very useful if used correctly. More on reddit.com
🌐 r/css
53
0
September 6, 2024
🌐
MadCap Software
help.madcapsoftware.com › contributor9r3 › Content › Contributor › Styles › CSS-Variables › CSS-Variables.htm
CSS Variables
A CSS variable lets you place the value for a style in one place and reuse it throughout a stylesheet. As with other kinds of single-sourcing, this can help with speed, ease of use, and consistency. Whenever you want to change the value, you only need to do so in one place and the new value ...
🌐
Codú
codu.co › niall › getting-started-with-css-variables-ejqv3eor
Getting Started with CSS Variables | by Niall Maher | Codú
October 6, 2023 - Did you know that instead of having static values, you can use variables to make your styles dynamic? Enter CSS Variables or, as they're formally known, CSS Custom Properties.
🌐
Savvy
savvy.co.il › blog › css & design › css variables: what they are and how to use them
CSS Variables: What They Are and How to Use Them | Savvy
February 23, 2026 - CSS Variables allow a value to be stored in one place and referenced or “called” from different places. Moreover, they have semantic value as --main-text-color is much easier to understand than #ffe01b, especially if the color is used in ...
🌐
Epic React
epicreact.dev › css-variables
Use CSS Variables instead of React Context | Epic React by Kent C. Dodds
July 9, 2024 - Turning to the CSS Variables approach, you'll notice the only component that re-rendered was our ThemeToggler component responsible for updating the body. And yet the user experience works perfectly! This is the magic behind CSS variables. With the ThemeProvider approach, we have to update the styles of every component, and then the browser paints those updates.
🌐
Ionic Framework
ionicframework.com › docs › theming › css-variables
CSS Variables | CSS Custom Properties for Variables & Components
Ionic components are built with CSS Variables for easy customization of an application. CSS variables allow a value to be stored in one place, then referenced in multiple other places. They also make it possible to change CSS dynamically at runtime (which previously required a CSS preprocessor).
Find elsewhere
🌐
Bootstrap
getbootstrap.com › docs › 5.3 › customize › css-variables
CSS variables · Bootstrap v5.3
Bootstrap includes many CSS custom properties (variables) in its compiled CSS for real-time customization without the need to recompile Sass.
🌐
MadeByMike
madebymike.com.au › writing › using-css-variables
Using CSS Variables Correctly - MadeByMike
June 18, 2017 - Variables have the potential to change how we organise and structure CSS, especially in relation to responsive design. The main advantage is we now have the ability to fully separate logic from design.
🌐
ThatSoftwareDude.com
thatsoftwaredude.com › content › 13923 › working-with-css-variables
Working with CSS Variables to Enhance Reusability and Flexibility - ThatSoftwareDude.com
September 13, 2024 - CSS Variables, also known as Custom Properties, have become a game-changer in the world of web development. They provide the ability to defi...
🌐
Medium
medium.com › @aicontentpace › css-variables-in-depth-guide-31b814d71ec6
CSS Variables In-depth Guide | by Defita F. C. P | Medium
July 23, 2024 - CSS variables, also known as CSS custom properties, are entities defined by CSS authors that contain specific values to be reused throughout a web document. They are a powerful tool that can simplify the management of styles and facilitate ...
🌐
Increment
increment.com › frontend › a-users-guide-to-css-variables
A user’s guide to CSS variables – Increment: Frontend
May 21, 2020 - CSS variables are custom properties that cascade normally and even inherit. They start with a reserved -- prefix, and there are no real rules about their value. (Anything goes, even white space.)
🌐
Medium
medium.com › @brcsndr › you-dont-know-css-variables-theming-d7a5c142d6ef
You don’t know CSS: Variables + Theming | by Bryce Snyder | Medium
November 21, 2023 - Theming with CSS variables involves using custom properties to store and reuse design values. You can easily change the look of a website by adjusting variables, providing benefits like consistency, real-time updates, and ease of maintenance.
Top answer
1 of 2
4

When you use the variable without var you set the value to the variable

:root {
  --color: red; /* set a value "red" to the "color" variable */
}

If you want then to get a value from the variable, you need to use var:

.card {
  color: var(--color);  /* get a color from the "--color" variable */
}

From the MDN Docs:

Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific values to be reused throughout a document. They are set using custom property notation (e.g., --main-color: black;) and are accessed using the var() function (e.g., color: var(--main-color);).

An example:

:root {
  --color: red; /* set default value for "color" variable */
}


#primary-cards {
  --color: #0F0; /* set custom value for "color" variable in primary cards */
}

.card {
  color: var(--color);  /* use the color variable */
}
<section id="primary-cards">
  <div class='card'>
    My card content
  </div>
</section>

2 of 2
0

<!DOCTYPE html>
<html>
<head>
<style>
  :root{
       /* --color: rbg)1,2,3,4);  error code, or --color variable doesn't exist */
      --blue: blue;
      
      --display-color: red;
      --NoFallBack-to-blue: blue;
      
      }
      
   .red{
      color: var(--display-color, var(--NoFallBack-to-blue))
      }
      
   .blue{
      color: var(--color, var(--blue))
      }
     
</style>
</head>
<body>

<div class="red">
  <p> color will not fallback to blue </p>
</div>


<div class="blue">
  <p> color will fallback to blue </p>
</div>


</body>
</html>

My bad, I didn't read the question, I only read the css code.

You can create a block of css root and assign a value to the declared variable.

For example:

:root {

   --color: rgb(53, 52, 52); 


  /* for fall back */
  --blue: #0000FF;
}

Inside a css selector, let's call the variable

.className{
   
    color: var(--color);
}

lets say --color doesn't exist or forgot to assign a value to the variable.

:root {

       --color: ; 
  
    
      /* for fall back */
      --blue: #0000FF;
    }

It is a fallback when first value doesn't work out then the program will fallback to next value which is --blue. The var(--blue) will display on the screen

.className{

    color: --var(--color, var(--blue))
}
🌐
Chrome Developers
developer.chrome.com › docs › css and ui › css variables - why should you care?
CSS Variables - Why should you care? | CSS and UI | Chrome for Developers
January 31, 2016 - CSS variables, more accurately known as CSS custom properties, are landing in Chrome 49. They can be useful for reducing repetition in CSS, and also for powerful runtime effects like theme switching and potentially extending/polyfilling future CSS features.
🌐
Mimo
mimo.org › glossary › css › variable
CSS Variable: Syntax, Usage, and Examples
CSS variables, also known as custom properties, store reusable values directly in stylesheets.
🌐
ITER Academy
iter-academy.com › home › html/css › an introduction to css variables (custom properties)
An Introduction to CSS Variables (Custom Properties) • ITER Academy
February 10, 2025 - CSS Variables enable you to store values (like colors, sizes, and other style properties) in a variable that can be reused throughout your CSS. These variables are defined with a -- prefix and can be accessed globally or scoped to a specific element.
🌐
Medium
medium.com › swlh › css-variables-618b156777ce
CSS Variables. Move over Sass, we have #CSS variables… | by Samantha Ming | The Startup | Medium
February 19, 2020 - Global scope will allow you to access the variable everywhere. Local scope will just be within your specific selector 👍 · :root { /* 1. Defining in Global Scope */ --color: red; /* 2. Available in all selector*/ color: var(--color); /* red */ }.local { /* 1. Defining in Local Scope */ --color: blue; /* 2. Scoped to the .local class only */ color: var(--color); /* blue */ } It’s been awhile since I had story time, so let’s me share the journey of a discontent css developer who finally stopped being so angry 😂
🌐
DEV Community
dev.to › delia_code › mastering-css-variables-a-beginners-guide-to-custom-properties-1gdk
Mastering CSS Variables: A Beginner's Guide to Custom Properties - DEV Community
June 19, 2024 - Overuse: It's tempting to turn every value into a variable. However, use them judiciously for values that genuinely need reuse or real-time manipulation. CSS Variables open up a world of possibilities for writing DRY, maintainable, and dynamic CSS. They can help you manage large stylesheets with ease, create themed designs without a hassle, and even let you tweak styles on the fly with JavaScript.
🌐
Maxime Heckel
blog.maximeheckel.com › posts › the-power-of-composition-with-css-variables
The Power of Composition with CSS Variables - The Blog of Maxime Heckel
August 2, 2022 - I stumbled upon this pattern of composing CSS partial values for my HSLA colors, assign them to variables. Thanks to this, I was able to build my fairly complex theme and a full color palette that made sense for my websites by only using CSS. No ThemeProvider.