CSS Variables
Custom properties, var(), theming
Skills taught: css.custom-properties, css.variables, css.theming
Defining and using variables
/* Define on :root to make globally available */
:root {
/* Colors */
--color-bg: #0e1116;
--color-surface: #1a1f2b;
--color-border: #2d3748;
--color-accent: #4fd1c5;
--color-text: #e2e8f0;
--color-muted: #94a3b8;
/* Spacing */
--space-sm: 8px;
--space-md: 16px;
--space-lg: 24px;
--space-xl: 40px;
/* Typography */
--font-sans: 'Inter', system-ui, sans-serif;
--font-mono: 'Fira Code', monospace;
--text-base: 1rem;
/* Borders */
--radius-sm: 4px;
--radius-md: 8px;
--radius-lg: 12px;
}
/* Use anywhere in your stylesheet */
body {
background: var(--color-bg);
color: var(--color-text);
font-family: var(--font-sans);
}
.card {
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: var(--radius-md);
padding: var(--space-lg);
}
Fallback values
/* var(--variable, fallback-value) */
.button {
background: var(--button-color, var(--color-accent));
/* If --button-color is not set, use --color-accent.
If --color-accent is also not set, use nothing (invalid). */
color: var(--button-text, #0e1116);
}
Theming — dark and light mode
/* DARK theme (default) */
:root {
--bg: #0e1116;
--surface: #1a1f2b;
--text: #e2e8f0;
--accent: #4fd1c5;
}
/* LIGHT theme — overrides the same variables */
:root[data-theme='light'] {
--bg: #ffffff;
--surface: #f8fafc;
--text: #0f172a;
--accent: #0d9488;
}
/* Or use media query for automatic OS-level preference */
@media (prefers-color-scheme: light) {
:root {
--bg: #ffffff;
--surface: #f8fafc;
--text: #0f172a;
}
}
/* ALL components automatically use the right theme: */
body { background: var(--bg); color: var(--text); }
.card { background: var(--surface); }
.accent { color: var(--accent); }
CSS variables in JavaScript
/* Define a variable for JS to control */
:root { --scroll-progress: 0; }
.progress-bar {
width: calc(var(--scroll-progress) * 100%);
height: 4px;
background: var(--color-accent);
}
<script>
// Toggle theme
document.documentElement.dataset.theme = 'light';
// Update a variable dynamically
window.addEventListener('scroll', () => {
const progress = window.scrollY / (document.body.offsetHeight - window.innerHeight);
document.documentElement.style.setProperty('--scroll-progress', progress);
});
</script>
Frequently Asked Questions
What is the difference between CSS variables and SASS/LESS variables?
SASS/LESS variables are compiled away — they exist only in source code and produce static CSS. CSS custom properties are live in the browser — they can be changed by JavaScript, they cascade and inherit, and they can be inspected in DevTools.
Can I use CSS variables in media queries?
Not directly in the @media condition itself (you can't write @media (min-width: var(--breakpoint))). But you can override variable values inside media queries: @media (prefers-color-scheme: light) { :root { --bg: white; } }.
How do I access a CSS variable from JavaScript?
Read: getComputedStyle(document.documentElement).getPropertyValue('--accent').trim(). Write: document.documentElement.style.setProperty('--accent', '#ff0000').