Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 4, 2020 03:54 am GMT

The best way to Dark Mode your Website.

Dark mode has been there for a while now. From apps to websites, its influence on the people has been really great. It's no wonder why everyone would love to have a switch to dark mode option in their Websites.

Now, you might have seen multiple ways of achieving the Dark mode for your website. Whether it be toggling a simple class to turn the background dark or using the Prefers color scheme to switch depending upon the user's system theme. Well, that's great. But people might not always have devices with support for a system wide dark mode. And also, toggling a class might not help a website with multiple colors. So what's the solution?

Here it is: It's actually pretty simple. The best way to achieve Dark Mode is by changing the entire Style Sheet when the user clicks the button for dark mode or toggles a switch.

This method not only gives you the freedom to style a complete dark version of your website but also helps if there are multiple elements which you want to color accordingly, which otherwise would be difficult to achieve by simply toggling a class. You can also have many other color themes for your website. So how do we do that? Enough of reading! Let's get into the code now.

Example:
Here's our HTML file:

<!DOCTYPE html><html lang="en">  <head>    <meta charset="UTF-8" />    <meta name="viewport" content="width=device-width, initial-scale=10" />    <title>Dark Mode</title>    <link rel="stylesheet" href="light-mode.css" id="theme" />  </head>  <body>    <h1>Demonstration of Dark Mode by changing the style sheet.</h1><div>  <button onclick="switchSheet()">Switch</button></div>    <script src="script.js"></script>  </body></html>

Here's our light-mode.css file:

* {  font-family: "Segoe UI";  font-weight: 200;}h1 {  text-align: center;  margin-top: 40vh;}div {  display: flex;  justify-content: center;}button {  padding: 10px;  background-color: #000;  color: #fff;  border: none;  border-radius: 3px;  -webkit-border-radius: 3px;  -moz-border-radius: 3px;  -ms-border-radius: 3px;  -o-border-radius: 3px;  font-size: 1em;}button:hover {  background: rgb(45, 50, 102);  color: rgb(255, 255, 255);}

Here's the dark-mode.css file:

* {  font-family: "Segoe UI";  font-weight: 200;}body {  background: rgb(19, 18, 18);}h1 {  text-align: center;  margin-top: 40vh;  color: #fff;}div {  display: flex;  justify-content: center;}button {  padding: 10px;  background-color: rgb(255, 255, 255);  color: rgb(0, 0, 0);  border: none;  border-radius: 3px;  -webkit-border-radius: 3px;  -moz-border-radius: 3px;  -ms-border-radius: 3px;  -o-border-radius: 3px;  font-size: 1em;}button:hover {  background: rgb(45, 50, 102);  color: rgb(255, 255, 255);}

Finally, here's the JavaScript for it:

function switchSheet() {  let theme = document.getElementById("theme");  if (theme.getAttribute("href") == "light-mode.css") {    theme.href = "dark-mode.css";  } else {    theme.href = "light-mode.css";  }}

In the above example, when the button is clicked, the function switchSheet() checks for the CSS file using the href attribute by the id we gave to the link tag. If light-mode.css exists, it replaces it with the dark-mode.css file. Else it switches it back to the light-mode.css file. That's it! Now you have the Dark Mode for your website without reloading the page at all. Thanks for reading. Hope it helped you. Have a great day!

Here's the link to repo:

GitHub logo Mohammad-Farmaan / Dark-Mode-For-Web

This example shows how you can achieve dark mode for your website by changing the entire style sheet.

Dark-Mode-For-Web

This example shows how you can achieve dark mode for your website by changing the entire style sheet.imageimage


Original Link: https://dev.to/mohammadfarmaan/the-best-way-to-dark-mode-your-website-1g7f

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To