Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Dark Mode Theme Switch Button On Full Website With sessionStorage

Dark Mode Theme Switch Button On Full Website With sessionStorage

This JavaScript code snippet helps you to create a dark mode toggle button with local storage to save dark mode settings. The toggle button adds a class name to the body element and changes the whole page styles accordingly. It uses sessionStorage to get the dark mode status to determine whether it should be enabled or not.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.


  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add Dark Mode Theme Switch Button On Full Website With sessionStorage?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.


<style type="text/css">
h1 {
color: skyblue;
text-align: center;
/* if dark-mode class is not added set this background */
body {
background: white;
transition: 0.5s;
text-align: center;
padding: 70px;
#ChangeTheme {
width: 0;
height: 0;
opacity: 0;
display: block;
height: 15px;
width: 60px;
position: relative;
margin: 0px auto;
span.slide {
position: absolute;
width: 100%;
height: 100%;
background: #dadada;
border-radius: 30px;
span.slide:before {
position: absolute;
content: "";
height: 20px;
width: 20px;
background: #fff;
border-radius: 50%;
top: -2px;
left: 0px;
transition: 0.5s;
box-shadow: rgba(0, 0, 0, 0.21) 0px 0px 7px 1px;
.txt {
position: relative;
.txt p:nth-child(1) {
color: grey;
font-weight: bold;
font-size: 1.1em;
transition: 0.5s;
#ChangeTheme:checked + span.slide:before {
transform: translatex(40px);
/* if dark-mode class is added set this background */
body.dark-mode {
background: #353535;
.dark-mode .txt p:nth-child(1) {
color: #000;
font-weight: normal;
.dark-mode .txt p:nth-child(2) {
color: grey;
font-weight: bold;

body.dark-mode header,
body.dark-mode main,
body.dark-mode footer{
background: #616161 !important;

body.dark-mode a.btn{
background: #333 !important;
color: #999 !important;
body.dark-mode p{
color: gray !important;


<h1>Dark mode on full domain with sessionStorage</h1>
<!-- Input checkbox for add or remove class from body with an id -->
<div class="container">
<p style="margin-left: 50px; margin-bottom: 20px"> Dark Mode</p>
<label for="ChangeTheme"> 
<input type="checkbox" id="ChangeTheme" /> <span class="slide"></span>


<script type="text/javascript">
var checkbox = document.getElementById("ChangeTheme"); //get the checkbox to a variable

//check storage if dark mode was on or off
if (sessionStorage.getItem("mode") == "dark") {
darkmode(); //if dark mode was on, run this funtion
} else {
nodark(); //else run this funtion

//if the checkbox state is changed, run a funtion
checkbox.addEventListener("change", function() {
//check if the checkbox is checked or not
if (checkbox.checked) {
darkmode(); //if the checkbox is checked, run this function
} else {
nodark(); //else run this funtion

//function for checkbox when checkbox is checked
function darkmode() {
document.body.classList.add("dark-mode"); //add a class to the body tag
checkbox.checked = true; //set checkbox to be checked state
sessionStorage.setItem("mode", "dark"); //store a name & value to know that dark mode is on

//function for checkbox when checkbox is not checked
function nodark() {
document.body.classList.remove("dark-mode"); //remove added class from body tag
checkbox.checked = false; //set checkbox to be unchecked state
sessionStorage.setItem("mode", "light"); //store a name & value to know that dark mode is off or light mode is on


No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
How To Disable Mouse On Anywhere On Your Blog And Website?

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *