The Definitive Guide on the Dark mode in Web development

July 29, 2020 3:07 pm 0

Dark mode has gained much attraction today. The giant firm Apple for example added dark mode to its iOS and macOS operating systems. Moreover, Google and Windows have done the same. 

Dark mode in Web development

What exactly is a dark mode? In simple terms, it’s a simple toggle that allows you to change the color of the background of the app window into black. It has been launched on a lot of popular applications, and an optional smartphone theme even. The dark mode is here to stay and not only for smartphones and devices but also in the context of websites as well, such as in PHP development for instance.

Dark Mode in Web Development

Typically, there is already a light theme for your website and you want to make a darker counterpart on your site. Or, if you’re a starter, you have both light and dark themes. One theme must be defined as the default, which in most instances is the light theme. 

Furthermore, there has to be a way of switching to the other theme automatically, as when a user clicks a button and the theme’s color changes. There are several approaches to do this, including the use of a body class, separate stylesheets, server-side scripts, and custom properties. The correct method comes down to your project’s requirements. 

You could for instance choose CSS properties for a large project to wrestle a big codebase. For a project that needs to support legacy browsers, another approach should instead be used.

Dark Mode at the level of the Operating System

We’ve used a button for toggling between light and dark mode, but we simply could allow the OS of the user to do the lifting. Many operating systems for example let users choose directly between light and dark themes.

We simply looked at accounting the system-wide color scheme preferences of a user. What happens however if a user wants to override their preference for a website? 

It’s not all the time that users who go for dark mode for their OS would prefer it on a site as well. Thus, providing a way to override dark mode manually is a great idea, whatever the system settings may be.

User Preferences Storage

The choice of users must be saved so it would be applied all over the website consistently, and on subsequent visits as well. In order to do this, we could save the choice of the user to the local storage when toggling a theme. Also, cookies are perfect for the job.

Handling the Styles of a User Agent

The color-scheme meta tag could be used to let the browser US stylesheet know regarding the color scheme preferences and the color schemes that are supported in the page. Let’s say for instance the page must support both dark and light themes. Separated by spaces, both could be placed in the meta tag. 

To support only the light theme, you only get to use ‘light’ as the value.

<meta name="color-scheme" content="dark light">

When adding a meta tag, the browser takes into account the color scheme preferences of the user when rendering page elements that are UA-controlled. For the root background, spell-check features, and form controls it renders colors based on the preference of the user. 

While themes for the most part are styled manually, informing a browser on the supported themes to avoid even the tiniest chance of a possible FOIT situation. This is true on instances in which HTML is rendered but still waiting for CSS to load. 

We could set this in CSS as well:

/* style.css */
:root {
  color-scheme: light dark;
}

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

A combination of all things

We’ll combine everything and build a working demo that:

  • Let users manually override the system preference
  • Loads a dark or light theme automatically, based on the preferences of a system
  • Maintain the preferred them on page reloads of users
Website in dark mode

Dark Mode Shadows

It could be tricky. While it’s possible to use a dark shadow in dark mode, the background color should be ‘light’ enough in order to provide ample contrast to see the shadow against it. Convey depth by using opacity, with a lower depth for high opacity regions. Elements with higher elevation must have a lesser opacity compared to the elements nearer to the background in depth. 

Typography in Dark Mode

The trick is the same as plenty of images; you need to balance the contrast. Too heavy fonts could result in text that makes you want to get away from the screen. On the other hand, a very light font would strain the eyes while inching closer to the screen. 

The balance would be somewhere in the middle, such as a small CSS, which makes a great difference when it comes to legibility for instance. 

Icons in Dark Mode

Icons belong to this tricky category since they’re some kind of a cross between images and text. We could change the fill with CSS when using SVG icons. If using font icons however, we could just alter the color property. Most of the same design considerations true for text, in general apply to icons as well. 

Dark Mode Colors

On a pure black background, the pure white text would seem shocking. The key is using an off-white for text, as well as off-black for the background.

Dark Mode, or no Dark Mode?

There are of course several benefits of dark mode in website development that PHP development services could offer and more. The question would be, dark mode or not? For both choices, there are perfectly valid reasons. Some even go beyond the user experience scope and include things such as budget, timing, and resources. 

Although you should carefully take into consideration if you want a dark mode implementation of otherwise, here are several reasons why you should have one. 

  • It’s trendy and cool.
  • It lets users decide the most comfortable way for them to consume content while providing a way of maintaining control over the feel and look of things. Keep in mind that we want to beat the Reader Mode button. 
  • It boosts accessibility via supporting users who are sensitive to eye strain in very bright themes. 
  • Greatly popular and would be here to stay. It’s possible that users who opt for a dark mode would expect your website to have one, thus you should be ready for it. 
  • Helps preserve the battery life for devices that have OLDE screens in which brighter colors would eat up more energy. 

Conclusion

To develop an application or website with a Dark Mode feature support, understanding the requirements and the user interactions with the application is of the essence. 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.