← Back to CSS
44

Modern dark mode: `prefers-color-scheme` and theming

Implement robust dark mode with CSS tokens, system preference support and manual override without breaking accessibility.

📘 Theory

Tokens for light and dark themes

Change variables, not entire components.

1

Define a small token set: background, surface, text and accent. Everything else in the visual system should be built on top of those tokens.

2

Then you can activate dark mode with a media query or with a `data-theme` attribute on the root element.

System preference support

Automatic support for people who already use dark mode at the OS level.

1

`@media (prefers-color-scheme: dark)` lets you adapt styles when the operating system is already in dark mode.

2

If you offer a manual toggle, the user choice should take priority over the automatic preference.

🧪 Learn by doing

Example Demo: themed card The component uses only tokens and does not depend on hard-coded light or dark rules.

🏁 Challenges

Challenge Challenge: manual override Activate a dark palette with `[data-theme='dark']`.

🧰 Resources

Test

Check your knowledge with a test about CSS.

Test for CSS

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com