CSS Variables & Theming – HTML & CSS
Custom properties, var(), calc() and design tokens for themes and dark mode.
Custom properties (--name) store values that you reuse with var(--name). Unlike preprocessor variables, they live in the DOM at runtime: they are inherited, can be changed via media query or JavaScript and take effect everywhere they are used. It is common to define them on :root as global "design tokens"; var(--x, fallback) provides a fallback value.
<!DOCTYPE html>
<style>
:root { --farbe-primaer: #e34f26; --abstand: 1rem; --radius: 10px; --bg: #fff; --text: #222; }
body { font-family: system-ui, sans-serif; margin: 12px; background: var(--bg); color: var(--text); }
.btn { background: var(--farbe-primaer); color: #fff; border: 0; cursor: pointer;
padding: calc(var(--abstand) / 2) var(--abstand); border-radius: var(--radius); }
.btn.gruen { --farbe-primaer: seagreen; } /* override locally */
.box { padding: var(--abstand); border: 2px solid var(--farbe-primaer, gray); margin-top: var(--abstand); }
</style>
<button class="btn">Default</button> <button class="btn gruen">Green overridden</button>
<div class="box">Border uses <code>--farbe-primaer</code></div>For themes you redefine the same variables for several states – for example via an attribute on the <html> element – and switch with JavaScript. The rest of the CSS stays unchanged.
<!DOCTYPE html>
<style>
:root { --bg: #ffffff; --text: #1c1c1c; --karte: #f1f3f9; --akzent: #2b59c3; }
[data-theme="dunkel"] { --bg: #14161c; --text: #ececec; --karte: #232733; --akzent: #8fb0ff; }
body { font-family: system-ui, sans-serif; margin: 12px; background: var(--bg); color: var(--text); transition: background .3s, color .3s; }
.karte { background: var(--karte); padding: 1rem; border-radius: 10px; margin-top: 10px; }
button { background: var(--akzent); color: var(--bg); border: 0; padding: 8px 14px; border-radius: 8px; cursor: pointer; }
</style>
<button onclick="var h=document.documentElement;h.dataset.theme=h.dataset.theme==='dunkel'?'hell':'dunkel'">Switch theme</button>
<div class="karte">All colors come from variables – changing one attribute is enough.</div>Following the system automatically: @media (prefers-color-scheme: dark) overrides the variables, and color-scheme: light dark makes sure that form elements and scrollbars also match the theme. calc() combines variables with units (calc(var(--abstand) * 2)); with @property variables can get a type and can even be animated.
<!DOCTYPE html>
<style>
@property --winkel { syntax: "<angle>"; inherits: false; initial-value: 0deg; }
:root { color-scheme: light dark; }
body { font-family: system-ui, sans-serif; margin: 12px; }
.ring { width: 110px; height: 110px; border-radius: 50%;
background: conic-gradient(from var(--winkel), #e34f26, #ffc107, #4caf50, #2196f3, #e34f26);
animation: dreh 4s linear infinite; }
@keyframes dreh { to { --winkel: 360deg; } }
@media (prefers-reduced-motion: reduce) { .ring { animation: none; } }
</style>
<p>The gradient rotates via an animated, typed variable (<code>@property</code>):</p>
<div class="ring"></div>var(--x, 10px)) and testing in the DevTools are important. @property is supported by current browsers, older ones ignore it.--blau-500: #2b59c3) from semantic tokens (--farbe-link: var(--blau-500)). Then you change themes in one place, and the rest of the CSS speaks in meanings, not in color values.