Dark mode on your website

Dark mode on your website

This article is more than four years old. The content may be outdated, especially technical examples or references to tooling.

With the release of MacOS Mojave and the beta of iOs 13 it's possible to bring the system wide darkmode to the web. Using dark colors instead of white reduces glare, which in turn reduces eye fatigue. Most importantly, Dark Mode can improve your ability to see certain aspects of video and photos, such as color and detail. So on paper it sounds like something we really want to bring to all our websites.

In your stylesheet

Safari comes with a new media query to create a block in your css which will be activated when dark mode is enabled and is called "prefers-color-scheme". It could be used as:

@media (prefers-color-scheme: dark) { }

Possible options for "prefers-color-scheme" are "dark", "light" and "no-preference".

In your HTML document

If you want to show dark images when dark mode is enabled you could benefit from the same media query as in the one your stylesheet. A downside is that you have to upload a second image to your webserver as you will show a different image to your visitor.

<picture>
    <source srcset="dark.png" media="(prefers-color-scheme: dark)">
    <img src="light.jpg">
</picture>

Support

The support for dark mode is still limited. Today (June, 2019) the following browsers do support dark mode:

  • Safari 12.1 or newer
  • Chrome 73 or newer
  • Firefox 67 or newer
  • Safari on iOs 13 or newer

Comments

There are no comments yet, leave yours below.

Leave a comment

Do you have an addition, question or experience related to this article? Share it below.

Comments are briefly reviewed before they appear.

Read more about:

The bullshit of implementing web accessibility

The bullshit of implementing web accessibility

An accessibility audit can turn green while the next feature introduces the same barriers again. I explain why a round of fixes is not enough, and how design, development, content and testing must share responsibility. Accessibility needs to survive the next release, not just the audit.

Continue reading

CSS-only Carousel Slider

CSS-only Carousel Slider

Does a carousel really need a JavaScript framework? This example builds a slider with HTML and CSS, then adds navigation between slides. It is an invitation to look at what the browser already provides before turning another interface element into a JavaScript component.

Continue reading

Validatiemeldingen tonen met AngularJS

Een leeg formulier hoeft niet direct vol rode foutmeldingen te staan. AngularJS houdt per veld en formulier bij wat de gebruiker heeft gedaan en of de invoer geldig is. Met states zoals touched, dirty en submitted bepaal je wanneer een validatiemelding verschijnt. Zo laat je de feedback aansluiten op het invullen van het formulier.

Lees verder