Course contentsShow
← React
Lesson 1942 of 2,39644. Styling ApproachesFree lesson

CSS Variables Basics and Browser Support

Understand CSS custom properties syntax, how they cascade, and why they're perfect for React theming patterns.

CSS Variables Basics and Browser Support

You'll learn how CSS custom properties work, how they cascade through the DOM, and why they're ideal for dynamic React theming.

What Are CSS Variables?

CSS custom properties (commonly called "CSS variables") are named values you define once and reuse throughout your styles. Unlike preprocessor variables (Sass/Less), they're native to the browser and cascade through the DOM tree, meaning child elements inherit them from parents.

Think of CSS variables like props that travel down the component tree—but for styles instead of data.

Syntax and Basics

Define variables with -- prefix, usually in :root or a specific selector:

:root {
  --primary-color: #3b82f6;
  --spacing-unit: 8px;
}

Use them with the var() function:

.button {
  background: var(--primary-color);
  padding: var(--spacing-unit);
}

How They Cascade

CSS variables follow the cascade like any CSS property. If you redefine a variable on a child element, descendants use the new value:

.light-theme {
  --bg-color: white;
}

.dark-theme {
  --bg-color: #1a1a1a;
}

Why Perfect for React Theming

CSS variables bridge the gap between JavaScript and CSS. You can update them dynamically from React state without regenerating stylesheets:

<div style={{ '--theme-color': isDark ? '#000' : '#fff' }}>

This updates instantly for all descendant elements using var(--theme-color), no CSS-in-JS runtime needed.

Browser Support

CSS custom properties work in all modern browsers (Chrome 49+, Firefox 31+, Safari 9.1+, Edge 15+). IE11 doesn't support them, but IE is officially retired.

Key Takeaway: CSS variables cascade through the DOM, update instantly from JavaScript, and provide a zero-runtime foundation for React theming patterns—making them powerful for dynamic, performant styling systems.