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

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.

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

              
                <html>

<head>
  <title>Light / Dark Theme Switcher in Vanilla JavaScript and CSS</title>
</head>

<body data-theme="">
  <header>
    <div class="logo">Brand</div>
    <nav>
      <ul>
        <li><a href="#home">Home</a></li>
        <li><a href="#news">News</a></li>
        <li><a href="#contact">Contact</a></li>
        <li><a href="#about">About</a></li>
      </ul>
    </nav>
    <button id="theme" onclick="switchTheme()">Dark</button>
  </header>
  <main>
    <h1>Chocolate Chip Muffins</h1>
    <p>
      A muffin is an individual-sized, baked product. It can refer to two distinct items, a part-raised flatbread that is baked and then cooked on a griddle (typically unsweetened) and a cupcake-like quickbread (often sweetened) that is chemically leavened and then baked in a mold. While quickbread muffins are often sweetened, there are savory varieties made with ingredients such as corn and cheese. The flatbread is of British or European derivation, and dates from at least the early 18th century, while the quickbread originated in North America during the 19th century. Both are common worldwide today.
    </p>
    <h2>Muffin top</h2>
    <p>The muffin top is the crisp upper part of the muffin, which has developed a "browned crust that's slightly singed around the edges".In 2018, McDonald's restaurant announced they were planning to sell muffin tops as part of its McCafe breakfast menu.
    </p>
  </main>
</body>

</html>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
}

:root {
  --font: Sans-Serif;
  --font-size-1: 16px;
  --font-size-2: 32px;
  --font-size-3: 48px;
  --font-size-4: 56px;
  --font-weight-1: 400;
  --font-weight-2: 700;
  --font-weight-3: 900;
  --white: white;
  --black: black;
}

[data-theme="dark"],
[data-theme="dark"] a {
  color: var(--white);
  background: var(--black);
}

[data-theme="light"] #theme {
  color: var(--white);
  background: var(--black);
}

body {
  font-family: var(--font);
  font-size: var(--font-size-1);
  max-width: 960px;
  margin: 0 auto;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

header .logo {
  font-size: var(--font-size-2);
  font-weight: var(--font-weight-2);
}

nav ul {
  list-style-type: none;
  display: flex;
}

nav ul li {
  margin-right: 10px;
}

header #theme {
  border: 0;
  padding: 5px 10px;
}

h1,
h2 {
  margin-top: 30px;
  margin-bottom: 10px;
}

h1 {
  font-size: var(--font-size-4);
}

h2 {
  font-size: var(--font-size-3);
}

              
            
!

JS

              
                const themeSwitcher = document.querySelector('#theme');

function switchTheme() {
  // toggle theme
  const toggleValue = localStorage.getItem('theme') === 'dark' ? 'light' : 'dark';
  
  // update localstorage value;
  localStorage.setItem('theme', toggleValue);
  
  // update theme
  setTheme();
}

function setTheme() {
  // set current theme - default to `light` on first load and set `theme` property in localStorage.
  const currentTheme = localStorage.getItem('theme') || 'light';
  
  // update current theme value on `data-theme` attribute
  document.body.dataset.theme = currentTheme;

  // update inner text of button dynamically based on current theme
  themeSwitcher.innerText = currentTheme === 'light' ? 'Dark' : 'Light';
}

// set theme on inital load
setTheme();
              
            
!
999px

Console