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

              
                <div data-theme-controls>
  <div class="color-scheme-status visually-hidden" role="status"></div>
  <button class="color-scheme-toggle" title="Toggle between light and dark mode">
    <svg class="icon light" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentcolor" viewBox="0 0 256 256">
      <rect width="256" height="256" fill="none"></rect>
      <line x1="92.8" y1="59" x2="85.1" y2="40.5" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <line x1="43" y1="108.8" x2="24.5" y2="101.1" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <line x1="213" y1="108.8" x2="231.5" y2="101.1" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <line x1="163.2" y1="59" x2="170.9" y2="40.5" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <line x1="240" y1="160" x2="16" y2="160" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <line x1="208" y1="200" x2="48" y2="200" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <path d="M70.2,160a60,60,0,1,1,115.6,0" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></path>
    </svg>
    <svg class="icon dark" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentcolor" viewBox="0 0 256 256">
      <rect width="256" height="256" fill="none"></rect>
      <line x1="216" y1="112" x2="216" y2="64" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <line x1="240" y1="88" x2="192" y2="88" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <line x1="168" y1="24" x2="168" y2="56" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <line x1="184" y1="40" x2="152" y2="40" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></line>
      <path d="M216.7,152.6A91.9,91.9,0,0,1,103.4,39.3h0A92,92,0,1,0,216.7,152.6Z" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="22"></path>
    </svg>
    <svg class="icon focus-arrows" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentcolor" viewBox="0 0 256 256">
      <rect width="256" height="256" fill="none"></rect>
      <polyline points="160 48 208 48 208 96" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></polyline>
      <line x1="152" y1="104" x2="208" y2="48" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></line>
      <polyline points="96 208 48 208 48 160" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></polyline>
      <line x1="104" y1="152" x2="48" y2="208" fill="none" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></line>
    </svg>
  </button>

  <div class="theme-status visually-hidden" role="status"></div>
  <label for="theme" class="theme-slider-label visually-hidden">Slide to change the theme</label>
  <input title="Slide to change the theme" class="theme-slider" type="range" id="theme" name="theme" min="1" max="4" value="1" />
</div>
              
            
!

CSS

              
                @mixin color-scheme-dark {
  --color-text: hsl(0, 0%, 88%);
  --color-text-accent: hsl(0, 0%, 68%);
  --color-bg: hsl(285, 18%, 4%);
  --color-bg-accent: hsl(285, 18%, 15%);
  --color-primary: hsl(344, 85%, 50%);
  --color-secondary: hsl(47, 81%, 56%);
}

:where(html) {
  --color-text: hsl(0, 0%, 5%);
  --color-text-accent: hsl(0, 0%, 20%);
  --color-bg: hsl(0, 0%, 98%);
  --color-bg-accent: hsl(0, 0%, 90%);
  --color-primary: hsl(230, 80%, 60%);
  --color-secondary: hsl(344, 85%, 48%);

  @media (prefers-color-scheme: dark) {
    &:not([data-color-scheme]) {
      @include color-scheme-dark;
    }
  }

  &[data-color-scheme="dark"] {
    @include color-scheme-dark;
  }
}

[data-theme="brambleberry crisp"] {
  --color-text: hsl(197, 100%, 15%);
  --color-text-accent: hsl(197, 100%, 20%);
  --color-bg: hsl(210, 55%, 90%);
  --color-bg-accent: hsl(210, 55%, 85%);
  --color-primary: hsl(230, 66%, 47%);
  --color-secondary: hsl(351, 77%, 44%);

  &[data-color-scheme="dark"] {
    --color-text: hsl(197, 100%, 75%);
    --color-text-accent: hsl(197, 100%, 55%);
    --color-bg: hsl(210, 100%, 12%);
    --color-bg-accent: hsl(210, 100%, 22%);
    --color-primary: hsl(146deg 100% 50%);
    --color-secondary: hsl(146deg 100% 50%);
  }
}

[data-theme="lemon blueberry parfait"] {
  --color-text: hsl(0, 0%, 100%);
  --color-text-accent: hsl(0, 0%, 94%);
  --color-bg: hsl(217, 92%, 49%);
  --color-bg-accent: hsl(217, 92%, 39%);
  --color-primary: hsl(57, 100%, 50%);
  --color-secondary: hsl(57, 100%, 65%);

  &[data-color-scheme="dark"] {
    --color-bg: hsl(217, 52%, 10%);
    --color-bg-accent: hsl(218, 76%, 21%);
    --color-primary: hsl(67, 18%, 68%);
    --color-secondary: hsl(0, 71%, 63%);
  }
}

[data-theme="fruit sorbet"] {
  --color-text: hsl(0, 0%, 7%);
  --color-text-accent: hsl(0, 0%, 0%);
  --color-bg: hsl(63, 88%, 64%);
  --color-bg-accent: hsl(63, 88%, 78%);
  --color-primary: hsl(0, 0%, 7%);
  --color-secondary: hsl(0, 0%, 7%);

  &[data-color-scheme="dark"] {
    --color-bg: hsl(314, 88%, 51%);
    --color-bg-accent: hsl(314, 88%, 71%);
  }
}

html,
body {
  height: 100%;
}

body {
  --space-base: 1rem;
  --space-xs: calc(var(--space-base) / 3);
  --space-sm: calc(var(--space-base) / 2);

  display: grid;
  place-items: center;
  color: var(--color-text);
  background-color: var(--color-bg);
  font-size: clamp(1.1rem, 4vw, 1.3rem);
  font-weight: 300;
  line-height: 1.6;
  transition: 200ms cubic-bezier(0.33, 1, 0.68, 1);
  transition-property: background-color, color;
}

*:where(:not(iframe, canvas, img, svg, video, a):not(svg *, symbol *)) {
  all: unset;
  display: revert;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

.visually-hidden {
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

@mixin current-mode($value) {
  @if $value == "dark" {
    .light {
      display: none;
    }
    .dark {
      display: block;
    }
  } @else {
    .light {
      display: block;
    }
    .dark {
      display: none;
    }
  }
}

[data-theme-controls] {
  display: flex;
  gap: var(--space-xs);
}

.color-scheme-toggle {
  @include current-mode(light);

  @media (prefers-color-scheme: dark) {
    @include current-mode(dark);
  }

  [data-color-scheme="light"] & {
    @include current-mode(light);
  }

  [data-color-scheme="dark"] & {
    @include current-mode(dark);
  }

  position: relative;
  display: flex;
  align-items: center;
  padding: var(--space-xs);
  cursor: pointer;

  &:active {
    transform: translateY(1px);
  }

  &:focus-visible {
    outline: 2px dashed var(--color-secondary);
    outline-offset: var(--outline-offset);
  }
}

.color-scheme-toggle .focus-arrows {
  display: none;
  position: absolute;
  bottom: -0.6em;
  left: 100%;
  color: var(--color-secondary);
  transform: translateX(50%) rotate(45deg);
  pointer-events: none;
}

.color-scheme-toggle:focus-visible .focus-arrows {
  display: block;
}

.theme-slider {
  flex: 1 1 0;
  width: 100%;
  max-width: 4.5rem;
}

.theme-slider-label {
  user-select: none;
}

input[type="range"] {
  --thumb-size: 12px;
  --thumb-offset: -4px;
  --track-height: 4px;
  background-color: transparent;
  padding: var(--space-xs);
  -webkit-appearance: none;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: var(--track-height);
  background: var(--color-primary);
  border: none;
  border-radius: 360px;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  margin-top: var(--thumb-offset);
  width: var(--thumb-size);
  height: var(--thumb-size);
  background-color: var(--color-text);
  border: none;
  border-radius: 360px;
  cursor: grab;
  box-shadow: inset var(--color-bg) 0 0 0 2px, var(--color-text) 0 0 0 2px;
}

input[type="range"]:active::-webkit-slider-thumb {
  cursor: grabbing;
}

input[type="range"]::-moz-range-track {
  background-color: var(--color-primary);
  height: var(--track-height);
  border: none;
  border-radius: 360px;
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: var(--thumb-size);
  height: var(--thumb-size);
  background-color: var(--color-primary);
  border: none;
  border-radius: 360px;
  cursor: grab;
}

input[type="range"]:active::-moz-range-thumb {
  cursor: grabbing;
}

              
            
!

JS

              
                const html = document.documentElement;
const themeControls = document.querySelector("[data-theme-controls]");
const toggle = themeControls.querySelector(".color-scheme-toggle");
const slider = themeControls.querySelector(".theme-slider");
const colorSchemeStatus = themeControls.querySelector(".color-scheme-status");
const themeStatus = themeControls.querySelector(".theme-status");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");

const themeFlavors = [
  "cookies and cream",
  "brambleberry crisp",
  "lemon blueberry parfait",
  "fruit sorbet"
];

const setToggleLabel = () => {
  let mode;

  mode = !html.dataset.colorScheme
    ? prefersDark.matches
      ? "dark"
      : "light"
    : html.dataset.colorScheme;

  toggle.setAttribute(
    "title",
    `Enable ${mode === "dark" ? "light" : "dark"} mode`
  );
};

const setColorScheme = (value) => {
  html.dataset.colorScheme = value;
  localStorage.setItem("color-scheme", value);
  setToggleLabel();
};

const setTheme = (value) => {
  const theme = themeFlavors[value - 1];
  html.dataset.theme = theme;
  localStorage.setItem("theme", theme);

  if (!html.dataset.colorScheme) {
    const scheme = prefersDark.matches ? "dark" : "light";
    setColorScheme(scheme);
    updateColorSchemeStatus(scheme);
  }
};

const updateColorSchemeStatus = (value) => {
  colorSchemeStatus.innerText = `Color mode is now "${value}"`;
};

const updateThemeStatus = (value) => {
  themeStatus.innerText = `Site display set to ${
    themeFlavors[value - 1]
  } theme`;
};

const init = () => {
  slider.setAttribute("tabIndex", -1);
  slider.setAttribute(
    "value",
    themeFlavors.indexOf(html.dataset.theme) + 1 || 1
  );
  setToggleLabel();
};

toggle.insertAdjacentHTML(
  "beforebegin",
  `<p id="theme-controls-focus-text" class="visually-hidden">
      In addition, use the left and right arrow keys to change the theme
    </p>`
);

toggle.setAttribute("aria-describedby", "theme-controls-focus-text");

toggle.addEventListener("click", () => {
  if (!html.dataset.colorScheme && prefersDark.matches) {
    setColorScheme("light");
    updateColorSchemeStatus("light");
    return;
  }

  const scheme = html.dataset.colorScheme === "dark" ? "light" : "dark";

  setColorScheme(scheme);
  updateColorSchemeStatus(scheme);
});

toggle.addEventListener("keydown", (e) => {
  if (e.key === "ArrowRight") {
    slider.value === slider.max ? (slider.value = 0) : slider.value++;
  }

  if (e.key === "ArrowLeft") {
    slider.value === slider.min ? (slider.value = slider.max) : slider.value--;
  }

  setTheme(slider.value);
  updateThemeStatus(slider.value);
});

slider.addEventListener("keydown", (e) => {
  if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
    toggle.focus();
  }
});

slider.addEventListener("input", () => {
  setTheme(slider.value);
  updateThemeStatus(slider.value);
});

init();

              
            
!
999px

Console