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>Light/Dark Toggle<br> Button</h1>

<div class="theme-toggle">
  <input type="checkbox" id="toggle-button" aria-label="Toggle Dark Mode">
  <label for="toggle-button">
    <i class="fas fa-moon" id="moon-icon"></i>
    <i class="fas fa-sun" id="sun-icon"></i>
  </label>
</div>
              
            
!

CSS

              
                * {box-sizing: border-box;}

body {
  font-family: montserrat;
  text-align: center;
  transition: background 0.2s linear;
  height: 100vh; /* Added to ensure the body takes the full viewport height */
  display: flex; /* Added to create a flex container */
  flex-direction: column; /* Added to arrange content in a column */
  justify-content: center; /* Center horizontally */
  align-items: center; /* Center vertically */
  margin: 0; /* Added to remove default margin */
}

/* Hide the input checkbox */
#toggle-button {
  display: none;
}

/* Style for the moon and sun icons */
#moon-icon {
	color:black;
  transition: opacity 0.3s ease;
  font-size:50px;
  transform:translateX(25px);
}

#sun-icon {
	color:orange;
  transition: opacity 0.3s ease;
  opacity: 0;
  font-size:50px;
  transform:translateX(-25px);
}

/* When the checkbox is checked (i.e., dark theme is active), show the sun icon and hide the moon icon */
#toggle-button:checked + label #moon-icon {
  opacity: 0;
}

#toggle-button:checked + label #sun-icon {
  opacity: 1;
}

/* Optional: Style for the label itself (the clickable area) */
label {
  cursor: pointer;
}

/* Dark Theme Variants*/
body.dark-theme {
  background-color:black;
}

body.dark-theme h1 {
  color:white;
}

              
            
!

JS

              
                // JavaScript
const toggleButton = document.getElementById('toggle-button');
const body = document.body;

// Check if a dark mode preference is stored in LocalStorage
const isDarkMode = localStorage.getItem('darkMode');

// Set the initial toggle state based on the stored preference
toggleButton.checked = isDarkMode === 'true';
body.classList.toggle('dark-theme', toggleButton.checked);

toggleButton.addEventListener('click', () => {
  const isDarkModeActive = body.classList.contains('dark-theme');
  
  // Toggle the class and store the dark mode preference in LocalStorage
  body.classList.toggle('dark-theme', !isDarkModeActive);
  localStorage.setItem('darkMode', !isDarkModeActive);
});

              
            
!
999px

Console