Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <body class="bg-main text-main">
  
  <nav class="mx-auto my-6 max-w-2xl font-bold flex justify-between">
    
    <h1 class="text-2xl">Light/Dark</h1>
    
    <a id="toggle" class="relative my-1 w-6 h-6" href="#">
      <svg id="moon" class="absolute inset-0 w-6 h-6 theme-light" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" /></svg>
		  <svg id="sun" class="absolute inset-0 w-6 h-6 theme-dark" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5" /><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42" /></svg>
    </a>
  
  </nav>

  <div class="bg-panel mx-auto max-w-2xl p-5 my-6 rounded-lg">
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore amet animi voluptas aperiam repellendus eos alias, perspiciatis saepe quas, deserunt ea unde commodi tempora et reiciendis recusandae sapiente libero impedit.</p>
  </div>

  <div class="bg-panel mx-auto max-w-2xl p-5 my-6 rounded-lg">
    <div class="relative h-6">
      <p class="absolute inset-0 theme-light">This text appears in light mode</p>
      <p class="absolute inset-0 theme-dark">This text appears in dark mode</p>
    </div>
  </div>
</body>
            
          
!

CSS

            
              :root {
  --background-main: #f7fafc;
  --background-panel: #edf2f7;
  --text-main: #1a202c;
  --opacity-light: 1;
  --opacity-dark: 0;
}

[data-theme='dark'] {
  --background-main: #1a202c;
  --background-panel: #2d3748;
  --text-main: #f7fafc;
  --opacity-light: 0;
  --opacity-dark: 1;
}

.bg-main {
  background-color: var(--background-main);
}

.bg-panel {
  background-color: var(--background-panel);
}

.text-main {
  color: var(--text-main);
}

.theme-light {
  opacity: var(--opacity-light);
}

.theme-dark {
  opacity: var(--opacity-dark);
}

html.transition,
html.transition *,
html.transition *:before,
html.transition *:after {
  transition-property: background-color color opacity !important;
  transition-duration: 750ms !important;
  transition-delay: 0 !important;
  transition-timing-function: linear !important;
}

            
          
!

JS

            
              window.addEventListener('DOMContentLoaded', function(event) {
  // Load the current theme from storage
  const currentTheme = localStorage.getItem('currentTheme') || 'light';
  // Set the theme attribute
  document.documentElement.setAttribute('data-theme', currentTheme);
});

document.getElementById('toggle').addEventListener('click', function(event) {
  event.preventDefault();
  
  // Get the current theme and calculate the new theme
  const currentTheme = localStorage.getItem('currentTheme') || 'light';
  const newTheme = currentTheme === 'light' ? 'dark' : 'light';

  // Store the theme in local storage
  localStorage.setItem('currentTheme', newTheme);

  // Temporarily set a transition style
  document.documentElement.classList.add('transition');
  setTimeout(() => {
    document.documentElement.classList.remove('transition')
  }, 750);

  // Set the theme attribute
  document.documentElement.setAttribute('data-theme', newTheme);
});

            
          
!
999px

Console