Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Dark mode on full domain with sessionStorage</h1>
<!-- Input checkbox for add or remove class from body with an id -->
<label for="ChangeTheme">
  <input type="checkbox" id="ChangeTheme" /> <span class="slide"></span>
</label>
<div class="txt">
  <p>White</p>
  <p>Dark</p>
</div>
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  min-width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
  font-family: "Century Gothic";
}
h1 {
  color: skyblue;
  text-align: center;
}
/* if dark-mode class is not added set this background */
body {
  background: white;
  transition: 0.5s;
}
#ChangeTheme {
  width: 0;
  height: 0;
  opacity: 0;
}
label {
  display: block;
  height: 15px;
  width: 60px;
  position: relative;
  transform: rotate(90deg) translate(50px, 100px);
}
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;
  transform: translatex(-50px);
}
.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;
}

              
            
!

JS

              
                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 funtion
  } 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
}

              
            
!
999px

Console