DEV Community

Christian Heilmann
Christian Heilmann

Posted on

21 5

One line of CSS to add basic dark/light mode

When you have your OS set up in dark mode seeing bright form elements is grating. By starting your style sheet with a single line of CSS, you can make sure that people using dark mode get dark form elements and backgrounds and those using light mode, light ones.

Codepen showing the difference the line of CSS makes - offering the page in a dark and light mode

You can see it in this codepen:

You can also use the browser developer tools to simulate the different modes to see the difference.

Emulating different modes in browser developer tools

Read more about color-scheme on the MDN web docs.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (7)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Baffles me why modern browsers do not also send a request header indicating the browser preference for this? Surely this would be useful... and a trivial change

Collapse
 
codepo8 profile image
Christian Heilmann

Sounds like a good enhancement issue to tell browser makers about :)

Collapse
 
deepakydv9315 profile image
Deepak Yadav • Edited

I don't think this is working on !!
1
2

Collapse
 
lemma profile image
Lemma

Same, not working on latest Firefox

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Works just fine for me on Firefox

Collapse
 
himanshupal0001 profile image
Himanshupal0001

Can we connect?

Collapse
 
aymhenry profile image
Ayman Henry

It is great command.
I made Android app, with webview.

To apply dark light, I've changed all colors in my page.

In next update, I will consider using this CSS.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay