DEV Community

Cover image for Simple Dark/Light mode
matoval
matoval

Posted on • Updated on

Simple Dark/Light mode

Here is the simplest way I found to add dark mode to a project:
Add a toggle button in your HTML with a checkbox and an onClick calling a JavaScript function

 <label id="toggle">
   <input type="checkbox" onclick="toggleDarkMode(event)">
   <span id="slider"></span>
 </label>
Enter fullscreen mode Exit fullscreen mode

Then add a data-mode to your CSS with your CSS variables set to the dark mode colors

:root {
  font-size: 10px;
  --bg: white;
  --font-color: black;
  --btn-bg: black;
  --btn-color: white;
}

[data-mode="dark"] {
  --bg: black;
  --font-color: white;
  --btn-bg: white;
  --btn-color: black;
}
Enter fullscreen mode Exit fullscreen mode

Now all you have left is to write the JavaScript function to set or remove the data-mode

function toggleDarkMode(event) {
  const isDarkmode = event.target.checked

  if (isDarkmode) {
    document.documentElement.setAttribute('data-mode', 'dark')
  } else {
    document.documentElement.setAttribute('data-mode', '')
  }
}
Enter fullscreen mode Exit fullscreen mode

Then you can add some CSS to make the button look better and add a transition

:root {
  font-size: 10px;
  --bg: white;
  --font-color: black;
  --btn-bg: black;
  --btn-color: white;
}

[data-mode="dark"] {
  --bg: black;
  --font-color: white;
  --btn-bg: white;
  --btn-color: black;
}

body {
  width: 100vw;
  font-size: 2rem;
  background-color: var(--bg);
  color: var(--font-color);
}

#toggle {
  margin-right: 2rem;
  width: 6rem;
  height: 3.4rem;
  border-radius: 3rem;
  background-color: var(--btn-bg);
}

#toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

#slider {
  position: absolute;
  z-index: 5;
  cursor: pointer;
  height: 2.5rem;
  width: 2.5rem;
  top: 1.23rem;
  right: 1.8rem;
  border-radius: 3rem;
  background-color: var(--btn-color);
  transition: ease-in .2s;
}

#slider.dark {
  right: 4.2rem;
}
Enter fullscreen mode Exit fullscreen mode

Here is the end result:

Latest comments (25)

Collapse
 
mrxinu profile image
Steve Klassen (They/Them)

That's pretty slick.

Collapse
 
michaeltharrington profile image
Michael Tharrington

Love the way this dark/light mode functions.

Also, seriously cool cover image for this post. 😀

Collapse
 
matoval profile image
matoval

Thanks 😁!

Collapse
 
alexi_be3 profile image
Alexi Taylor 🐶

But what about IE? Aren't CSS variables not supported in that unsightly browser?

 
matoval profile image
matoval

I added the codepen at the bottom of the article.

Collapse
 
nathankurz91 profile image
Nathan Kurz

This is pretty awesome. So cool seeing such an easy way to implement such a highly requested feature in many applications.

 
matoval profile image
matoval

You have two head tags

Thread Thread
 
matoval profile image
matoval

I'll write this code in a codesandbox and add it to the article.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.