Christian Heilmann

One line of CSS to add basic dark/light mode

Monday, June 20th, 2022 at 11:33 pm

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

See the Pen
by Christian Heilmann (@codepo8)
on 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.

Share on Mastodon (needs instance)

Share on Twitter

My other work: