DEV Community

Christian Heilmann
Christian Heilmann

Posted on

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.

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.