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 image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

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

Okay