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
ย 
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.

Collapse
ย 
himanshupal0001 profile image
Himanshupal0001 โ€ข

Can we connect?