I'll start by saying that there is no native method for dark mode in HTML and CSS just yet. So whatever method you end up using will have to be a custom approach. The following are 2 of the most common ways that I have seen and used in order to get a Dark Mode layout on a webpage. Starting with the quickest method, and ending with probably the most efficient method.
Using an invert filter
This is a quick one-line effect that can be accomplished with the CSS filter property. You can set the filter to invert the current element, in which the browser will then perform a color inversion on the given element. You might see this used online, but I would not recommend it for a variety of reasons.
- The browser controls the inversion algorithm
- You have to explicitly set background colors in order for them to be inverted
- Child elements cannot be un-inverted
- You get what you pay for
Here's a quick live example of what an invert "Dark Mode" would look like. Take the following element with default styling to match this page.
The following filter will be applied to this element.
Which will look like the following on your particular browser.
Hello World Again
If you notice, the color of the text inside the element isn't quite right. There's almost like a slight shadow on it making it difficult to read. And the inverse of the blue-ish color in the original converts to a bright reddish orange color. This will also much depend on the browser and device that you are using and how they implement their filter algorithms.
There is a way to control to level of inversion on each element however and that is modifying the inverts argument, which can either be a percentage value from 0% (no inversion) to 100% (full inversion) or a numeric value from 0 to 1.
In the same example above, we can lower the inversion level to adjust the overall brightness.
Hello World Again
Maybe if you need a quick dark mode implementation and are willing to deal with the random inversions (temporarily) this might be a way to go.
But the next method is more in line with an actual implementation.
Creating a Dark Mode stylesheet
The most precise method of controlling the dark mode styling is to simply just do it yourself. And you can start by creating a default dark theme for any element with the class name of 'darkmode'.
With this declaration, you can add the 'dark' class to any parent, such as the body tag, and have every child element inherit the default style.
This is a paragraph
You can target individual elements with their dark theme by targeting them directly in CSS.
Creating this hierarchy makes it simpler to manage the original theme as well.
This makes it much easier to design both themes independently as needed.
This gives you much more precise control over the actual dark theme that you are looking to achieve. And you can turn it on or off by removing the 'darkmode' class from the parent element, which you can toggle with jQuery or Vanilla JS.
$('body').toggleClass('darkmode'); // jQuery
document.getElementsByTagName('body').classList.toggle('darkmode'); // vanilla JS
It's possible that some users will mainly use your websites in the dark mode theme that you provide. Depending on the type of screen, it could improve battery life overall and frankly, it just makes sense to have at night time.
Remembering a users selection
By remembering a users preference, you give them a much more personalized experience overall. And you definitely don't want a user to have to click on a 'Dark Mode' icon on every pageload.
Assuming that a user hasn't blocked it on their browsers, then localStorage is a good way to store these sitewide user preferences.
When the page loads for the first time you can check the value in localStorage and toggle the 'darkmode' class as needed.
This will entirely depend on the framework and language that you are using however.
And there you have it. My personal favorite way to implement darkmode currently and also coming to this site really soon.