Dark mode on your website

Dark mode on your website

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

Read more about:

10 JavaScript one-liners every developer should know

Code should be readable and pragmatic. For me, that means when I look at a piece of code, I should almost instantly understand what it does. Clear code always has good variable names, is predictable, avoids unnecessary logic, and lives in the right place within a project. But sometimes you need to do something a little complex without turning the rest of your code into a mess. That’s where one-liners come in. Small, pragmatic snippets…

Continue reading

The difference between debounce and throttle

Debounce and throttle are powerful techniques, but only if you really understand what they do. I often ask about them in tech interviews, and to my surprise, many developers (even seniors) struggle to explain the difference. The good news is: it’s actually quite simple. In this post, I’ll break down both techniques and share some practical use cases. If you are unfamiliar with the techniques and are asked about them too in your interview…

Continue reading

The paradox of AI in web development

Since the start of my career as a developer, I’ve seen a lot of changes. From the rise of Dreamweaver with a WYSIWYG editor for generating table code to the arrival of jQuery and professional frameworks that completely transformed web development. Nowadays, there seems to be only one buzzword everywhere: AI.…

Continue reading