Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - CSS Variables


CSS Variables

CSS Variables, also known as Custom Properties, let you store values such as colors, font sizes, and spacing in one place. You can reuse and update them easily across your entire stylesheet.


Syntax

CSS variables are defined using two dashes (--) followed by a custom name, and are accessed with the var() function.

Example

Defining and using a variable:

:root {
  --main-color: coral;
  --padding: 10px;
}

div {
  background-color: var(--main-color);
  padding: var(--padding);
}
Try it Yourself »

The :root selector defines global variables that can be used anywhere on the page.


Why Use CSS Variables?

  • Keep your CSS consistent and organized.
  • Make global style changes quickly.
  • Reduce repetition and simplify maintenance.
  • Support theme customization (light/dark modes, etc.).

Variable Scope

Variables can be defined globally (in :root) or locally within an element.

Example

Global vs local variables:

:root { --main-bg: lightblue; }
section { --main-bg: lightgreen; }

div.global { background: var(--main-bg); }
section.local div { background: var(--main-bg); }
Try it Yourself »

Local variables override global ones when defined within an element.


Using Fallback Values

You can specify a fallback value in case the variable is not defined.

Example

Fallback for undefined variable:

div {
  color: var(--text-color, black);
}
Try it Yourself »

If --text-color is not set, the text will default to black.


Changing Variables with JavaScript

You can update CSS variables dynamically using JavaScript to change themes or styles instantly.

Example

Changing a variable with JavaScript:

<button onclick="changeTheme()">Change Theme</button>

<script>
function changeTheme() {
  document.documentElement.style.setProperty('--main-color', 'lightseagreen');
}
</script>
Try it Yourself »

CSS Variables and Inheritance

Variables are inherited just like normal CSS properties, meaning child elements can access variables from parent elements.

Example

Inherited variable:

section { --color: blue; }
p { color: var(--color); }
Try it Yourself »

Practical Example: Theme Switching

You can use variables to create light and dark mode themes easily.

Example

Light and dark themes:

:root {
  --bg-color: white;
  --text-color: black;
}

body.dark {
  --bg-color: #121212;
  --text-color: #f1f1f1;
}

body {
  background: var(--bg-color);
  color: var(--text-color);
}
Try it Yourself »

Summary

  • Store global variables inside :root.
  • Define variables using --name syntax.
  • Access them with var(--name).
  • Use meaningful names like --main-color or --font-size-large.
  • Group related variables (colors, spacing, typography).
  • Use fallback values for safety.
  • Change them dynamically with JavaScript for themes and interactivity.

Next, you'll start your CSS Project - putting together what you've learned to style a complete webpage.

Next » CSS Project: Style your Website


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.