In recent years, dark mode has gained significant popularity as a user interface option. It offers a darker background complemented by lighter text, which not only reduces eye strain but also conserves battery life, especially on OLED screens.

Discover how you can add a dark mode option to your websites and web apps, using a combination of CSS and JavaScript.

Initial UI after HTML and CSS has been applied

Understanding Dark Mode

Dark mode is an alternative color schemefor your website that swaps the traditional light background for a dark one. It makes your pages easier on the eyes, especially in low-light conditions. Dark mode has become a standard feature on many websites and applications due to its user-friendly nature.

Setting Up Your Project

Before you implement this, ensure you have a project set up and ready to work on. You should have your HTML, CSS, and JavaScript files organized in a structured manner.

The HTML Code

Start with the following markup for the content of your page. A visitor will be able to use thetheme__switcherelement to toggle between dark and light mode.

The CSS Code

Add the following CSS to style the example. This will act as the default light mode which you’ll later augment with new styles for a dark mode view.

At the moment, your interface should look like this:

Implementing Dark Mode Using CSS and JavaScript

To implement dark mode, you’ll define its look using CSS. You’ll then use JavaScript to handle the switching between dark and light mode.

Creating the Theme Classes

Use one class for each theme so you may easily switch between the two modes. For a more complete project, you should consider howdark mode may affect every aspect of your design.

Selecting the Interactive Elements

Add the following JavaScript to yourscript.jsfile. The first bit of code simply selects the elements you’ll use to handle the toggle.

Adding the Toggle Functionality

Next, use the following JavaScript to toggle between the light mode (light) and dark mode (dark) classes. Note that it’s also a good idea to alter the toggle switch to indicate the current mode. This code does so witha CSS filter.

This makes your page change themes with a click of the toggle container.

UI after dark mode has been toggled on

Enhancing Dark Mode With JavaScript

Consider the following two improvements that can make your dark mode sites more pleasant to use for your visitors.

Detecting User Preferences

This involves checking the user’s system theme before the website loads and adjusting your site to match. Here’s how you can do it using thematchMediafunction:

Now, any user who visits your site will see a design that matches their device’s current theme.

Persisting User Preference With Local Storage

To enhance the user experience further,use local storageto remember the user’s chosen mode across sessions. This ensures that they don’t have to repeatedly select their preferred mode.

Embracing User-Centric Design

Dark mode goes beyond looks; it’s about putting user comfort and preferences first. By following this approach, you can create user-friendly interfaces and encourage repeat visits. As you code and design, prioritize user wellbeing, and deliver a better digital experience for your readers.