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

              
                <body>

  <header>
    <button onclick="toggleTheme()" id="themeToggle" type="button">Toggle Theme</button>
    <span id="currentTheme"></span>
  </header>

  <main>
    <h1>A few of my favorite things</h1>

    <ul>
      <li>Books</li>
      <li>Coffee</li>
      <li>CSS</li>
    </ul>

    <p>What I'm up to: <a href="https://martine.dev">martine.dev</p>
  </main>
</body>
              
            
!

CSS

              
                .light {
  --background-color: #fafafa;
  --typography-color: rgba(44, 44, 44, 0.86);
  --primary-color: #96085b;
  --primary-color-contrast: #ffffff;
}

.dark {
  --background-color: #303641;
  --typography-color: rgba(255, 255, 255, 0.86);
  --primary-color: #eca7cf;
  --primary-color-contrast: #1b2222;
}

body {
  font-family: Helvetica, Arial, sans-serif;
  background: var(--background-color);
  color: var(--typography-color);
  padding: 4rem;
}

h1 {
  color: var(--primary-color);
}

a:link,
a:visited {
  color: var(--primary-color);
  font-weight: bold;
  text-decoration-style: dotted;
}

a:hover {
  text-decoration-style: solid;
}

button {
  background: none;
  border: solid 1px var(--primary-color);
  background: var(--primary-color);
  color: var(--primary-color-contrast);
  padding: 2ex 4ch;
  border-radius: 4px;
}
button:hover {
  background: none;
  color: var(--primary-color);
  cursor: pointer;
}

button:focus,
a:focus {
  outline: dotted 1px var(--primary-color);
  outline-offset: 2px;
}

header {
  display: flex;
  gap: 1rem;
  align-items: baseline;
}

              
            
!

JS

              
                let currentTheme;

/**
 * On load set theme based on saved preferences or user settings
 */
(() => {
  "use strict";

  //  get theme from local storage
  let theme = localStorage.getItem("theme");

  //  if no theme and an user prefers dark mode, set theme to dark
  if (
    !theme &&
    window.matchMedia &&
    window.matchMedia("(prefers-color-scheme: dark)").matches
  ) {
    theme = "dark";
  }

  //  if no theme or value stored in local storage is not either 'dark' or 'light' use light
  if (!theme || (theme !== "light" && theme !== "dark")) {
    theme = "light";
  }

  // now that we know which theme we want, go ahead and set it
  setTheme(theme);
})();

/**
 * set's the theme's class name on the body
 * @param theme (light or dark)
 */
function setTheme(theme) {
  //  save theme for next time
  localStorage.setItem("theme", theme);
  currentTheme = theme;

  //  add theme class to body
  const body = document.querySelector("body");
  body.classList.remove("light", "dark");
  body.classList.add(theme);

  document.getElementById("currentTheme").innerText = `Current theme: ${theme}`;
}

/**
  Toggles theme when button is clicked
*/
function toggleTheme() {
  const theme = currentTheme === "light" ? "dark" : "light";
  setTheme(theme);
}

              
            
!
999px

Console