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.

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

              
                <!DOCTYPE html>
<head>
    <title>Toggle Dark Mode</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="utf-8">
    <link rel="shortcut icon" href="/assets/favicon.ico">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
    <header class="header">
        <img src="./images/dcode-logo.png"
            alt="dcode"
            class="header__logo">
        <div class="select">
            <i class="material-icons">palette</i>
            <select class="select__input" id="selTheme">
                <option value="auto">Auto</option>
                <option value="light">Light</option>
                <option value="dark">Dark</option>
            </select>
        </div>
    </header>
    <main class="main">
        <h1>Welcome to dcode!</h1>
        <p>
            Mauris et neque tincidunt, sagittis mauris sit amet, elementum quam. Quisque sed laoreet neque. Nulla est
            augue, porttitor non laoreet tristique, pellentesque quis nisi. Integer sodales blandit sapien non ultrices.
            Proin porta eu neque a elementum. Cras id tortor rhoncus, lacinia justo eu, luctus nisi. Etiam ac tempus
            enim. Mauris fermentum erat eu ligula mattis faucibus. Morbi volutpat et erat vel cursus.
        </p>
        <p>
            Aenean eu odio dui. Aenean posuere ultricies ullamcorper. Donec fermentum orci in ullamcorper tristique.
            Proin porta ante quis feugiat scelerisque.
        </p>
    </main>
</body>
              
            
!

CSS

              
                :root {
    --dark-background-color: #111111;
    --dark-background-color-alt: #222222;
    --dark-trim-color: #333333;
    --dark-text-color: #eeeeee;
}

body {
    /* Light Theme */
    --background-color: #ffffff;
    --background-color-alt: #eeeeee;
    --trim-color: #dddddd;
    --text-color: #333333;
    
    --primary-color: #009578;
    --font-family: 'Quicksand', sans-serif;

    margin: 60px 0 0 0;
    color: var(--text-color);
    font-family: var(--font-family);
    background: var(--background-color);
}

body.theme-dark {
    --background-color: var(--dark-background-color);
    --background-color-alt: var(--dark-background-color-alt);
    --trim-color: var(--dark-trim-color);
    --text-color: var(--dark-text-color);
}

@media (prefers-color-scheme: dark) {
    body.theme-auto {
        --background-color: var(--dark-background-color);
        --background-color-alt: var(--dark-background-color-alt);
        --trim-color: var(--dark-trim-color);
        --text-color: var(--dark-text-color);
    }
}

.header {
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    padding: 10px;
    background: var(--background-color-alt);
    border-bottom: 1px solid var(--trim-color);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
}

.header__logo {
    height: 100%;
}

.header__button {
    background: none;
    border: none;
    outline: none;
}

.header > *:not(:last-child) {
    margin-right: 10px;
}

.main {
    margin: 0 auto;
    max-width: 1000px;
    padding: 25px;
}

.main p {
    line-height: 1.6;
    font-weight: 500;
}

.select {
    display: inline-flex;
    align-items: center;
    padding: 5px 8px;
    border-radius: 10px;
}

.select__input {
    -webkit-appearance: none;
    -moz-appearance: none;
    padding: 7px 14px;
    background: none;
    border: none;
    outline: none;
    cursor: pointer;
    color: var(--text-color);
    font-weight: 500;
    font-family: var(--font-family);
}

.select__input option {
    color: #000000;
}

              
            
!

JS

              
                function applyTheme(theme) {
  document.body.classList.remove("theme-auto", "theme-light", "theme-dark");
  document.body.classList.add(`theme-${theme}`);
}

document.addEventListener("DOMContentLoaded", () => {
  const savedTheme = localStorage.getItem("theme") || "auto";

  applyTheme(savedTheme);

  for (const optionElement of document.querySelectorAll("#selTheme option")) {
    optionElement.selected = savedTheme === optionElement.value;
  }

  document.querySelector("#selTheme").addEventListener("change", function () {
    localStorage.setItem("theme", this.value);
    applyTheme(this.value);
  });
});

              
            
!
999px

Console