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 INTRO TO HTML & CSS BASH RUST

HTML and CSS Custom Properties (CSS Variables)


HTML & CSS: Custom Properties (CSS Variables)

CSS custom properties, commonly called variables, let you centralize design tokens like colors, spacing, and typography.

They enable dynamic theming and reduce duplication across components.


Declaring variables

  • Use the :root selector to define global tokens.
  • Variables are written as --token-name and accessed with var(--token-name).
  • Custom properties inherit, so redefine them on containers to theme subsections.

Note: :root targets the whole document, so variables defined there are available everywhere.

If you want to read more about CSS Custom Properties or get an in-depth understanding, go to CSS Custom Properties in our CSS tutorial.


Example: Design Tokens

Token setup

:root {
  --color-brand: #2563eb;
  --color-brand-contrast: #eef2ff;
  --font-base: "Inter", system-ui, sans-serif;
  --space-md: clamp(1rem, 2vw, 1.625rem);
}
main {
  font-family: var(--font-base);
  padding: var(--space-md);
  background: var(--color-brand-contrast);
}
.btn-primary {
  background: var(--color-brand);
  color: #fff;
  padding: 0.75rem 1.5rem;
  border-radius: 999px;
}
 

Changing a token once updates every place it is used with var(--token), making themes and global tweaks easy.



Dynamic theming

  • Switch themes by toggling class names on <html> or <body>.
  • Media queries can update variables for prefers-color-scheme.
  • JavaScript can read and update custom properties at runtime.

Note: Because variables are live, changing them once automatically updates every place they are used.

Light/dark toggle

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css">
<script>
const toggleTheme = () => {
  const root = document.documentElement;
  const next = root.dataset.theme === "dark" ? "light" : "dark";
  root.dataset.theme = next;
};
</script>
</head>
<body>
  <button onclick="toggleTheme()">Toggle theme</button>
</body>
</html>
:root {
  color-scheme: light dark;
  --surface: #ffffff;
  --text: #0f172a;
}
:root[data-theme="dark"] {
  --surface: #0f172a;
  --text: #e2e8f0;
}
body {
  margin: 0;
  background: var(--surface);
  color: var(--text);
  font-family: "Inter", sans-serif;
  transition: background 0.3s ease, color 0.3s ease;
}
Try it Yourself »

Scoping tokens

  • Create component-specific tokens under a wrapper class to avoid global overrides.
  • Use fallback values with var(--token, fallback) to guard against missing definitions.
  • Combine with @layer or style modules for maintainable design systems.

Note: Fallbacks act like default values; they are used if the variable has not been set.


Runtime calculations

Variables work inside calc(), clamp(), gradients, and even media queries (where supported).

  • padding: calc(var(--space-md) * 1.5);
  • background: linear-gradient(var(--angle, 45deg), ...);
  • Feature detect with @supports (--custom: property).


×

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.