If you want to make your WordPress page rank higher in search engines, you need to provide as much information about it as possible. One way to do that is by including metadata such as meta titles, meta tags, or meta descriptions. In this article, you’ll learn why meta descriptions, in particular, are important for […]
Is self-management an essential building block on an organization’s path to business agility or a nice-to-have cultural twist to, for example, keep teams happy and attract new talent?
While many people, particularly at the management level, are skeptical about the concept, I am convinced that organizations need to descale and regroup around aligned, autonomous, self-managing teams in a complex environment. Ultimately, only the people closest to the customers’ problems can solve those within the given constraints while contributing to an organization’s sustainability.
More and more users prefer browsing websites in dark mode to reduce eye strain and save battery life. To provide the best user experience, it’s helpful to implement an automatic dark mode on your website that adjusts according to a user’s operating system settings. In this tutorial, we’ll walk you through the steps to achieve this.
Your Web Designer Toolbox Unlimited Downloads: 500,000+ Web Templates, Icon Sets, Themes & Design Assets
1. Create a CSS file for dark mode
First, create a separate CSS file called “darkmode.css” that contains the styles for your website’s dark mode. This file should include the color palette, font styles, and other design elements that cater to dark mode. For example:
The “media” attribute ensures that the dark mode CSS file will only be applied if the user’s operating system is set to dark mode.
3. Detect operating system’s color scheme
Now it’s time to detect the user’s operating system color scheme with JavaScript. Create a new JavaScript file called “darkmode.js” and add the following code:
This function checks if the user’s operating system is set to dark mode or light mode by evaluating the ‘prefers-color-scheme’ media query.
4. Save the user’s preference
To improve the user experience, we’ll save their preference in the browser’s local storage. Add the following code to the “darkmode.js” file:
function saveUserPreference(preference) {
localStorage.setItem('color-scheme', preference);
}
function loadUserPreference() {
return localStorage.getItem('color-scheme');
}
These two functions save and load the user’s color scheme preference, respectively.
5. Toggle dark mode
Create a function to toggle dark mode on and off by adding or removing the “dark” class from the HTML body element:
function toggleDarkMode(enableDarkMode) {
if (enableDarkMode) {
document.body.classList.add('dark');
} else {
document.body.classList.remove('dark');
}
}
6. Initialize dark mode based on user preference or system settings
Now, add a function to initialize dark mode on page load. This function checks if the user has a saved preference in local storage; if not, it defaults to their operating system’s settings:
As a fallback and also providing a better experience for your users, it is a good practice to give them the ability to switch between dark and light modes manually. To do this, add a button or a switch element in your website’s main HTML file:
<button id="toggle-darkmode">Toggle Dark Mode</button>
Next, add an event listener for this button in your “darkmode.js” file:
This function toggles dark mode, saves the new preference in local storage, and updates the user interface.
8. Listen for operating system changes
To ensure that your website’s dark mode adapts to changes in the user’s operating system settings, add an event listener to the “darkmode.js” file that listens for changes to the ‘prefers-color-scheme’ media query:
function handleSystemColorSchemeChange(event) {
if (!loadUserPreference()) {
toggleDarkMode(event.matches);
}
}
window.matchMedia('(prefers-color-scheme: dark)').addListener(handleSystemColorSchemeChange);
This function checks if the user has a saved preference in local storage. If not, it toggles dark mode based on the updated operating system settings.
9. Link the JavaScript file
Finally, include the “darkmode.js” file in your main HTML file by adding the following script tag inside the head section:
<script src="darkmode.js" defer></script>
The “defer” attribute ensures that the script is executed only after the HTML document has been fully parsed.
All done! Now you have everything you need to implement dark mode on your future projects!