Skip to content
CSS·Intermediate·Coding·1 min read

How would you implement a light/dark theme toggle using custom properties without duplicating component styles?

Short interview answer

Define semantic custom properties such as --surface, --text, and --brand once with default values, write every component against those names via var(), then define exactly one additional override block, keyed by a class, a data attribute, or a prefers-color-scheme media query, that redefines the same property names. Every component that already reads var() repaints without being touched.

Example

CSS
:root { --surface: #fff; --text: #15171c; }[data-theme='dark'] { --surface: #171a17; --text: #f2f0e8; }
.card { background: var(--surface); color: var(--text); } /* never theme-specific */

Key takeaway

Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.

← Back to Custom properties and theming

Related questions