Arm1.ru

Adding Dark Mode support for website

iOS 13 coming this fall will include dark mode. Current macOS already has dark mode. With bringing dark mode to iOS Apple also recommends to web developers to adopt colors for dark mode.

It's pretty easy. All you need is to change CSS for your website and add something like that:

:root {
  color-scheme: light dark;
  --body-background-color: white;
  --body-text-color: #2F2F2F;
}

@media (prefers-color-scheme: dark) {
  :root {
    --body-background-color: #222222;
    --body-text-color: #E2E2E2;
  }
}

body {
  background-color: var(--body-background-color);
  color: var(--body-text-color);
}

So you're adding support for color schemes with color-scheme: light dark; and use CSS variables.

If you need to test it you can try latest Safari in latest betas of iOS or macOS or (maybe) Safari developer preview. Developer tools in Safari has special button to test dark mode.

Interesting: in macOS 10.15 default body background for dark mode is also dark. But in 10.14 it will be white in Safari and Firefox. You need to specify background color for dark scheme if you want to support macOS < 10.15. Apple didn't mention that during WWDC session. All they did was just removing background-color property from CSS to let OS choose background color. They provide NSHipster website as an example but it's CSS also contains background color property in CSS to support previous macOS versions :)

I've added dark scheme support to my Space In Box site. Also check NSHipster site that has it too.

This article is some kind of summary from WWDC video Supporting Dark Mode in Your Web Content.

keyboard_return back
local_offer dark mode web css