Pen Settings



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


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


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.


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.


    <form onsubmit="return false;">
      <button id="change-theme">Switch theme</button>


                :root, .theme-white {
  --base: 0, 0%;
  --lightness: 100%;
  --color: #1098ad;

.theme-dark {
  --base: 210, 10%;
  --lightness: 23%;  
  --color: white;

.theme-red {
  --base: 0, 100%;
  --lightness: 32%;
  --color: white;

.theme-orange {
  --base: 35, 100%;
  --lightness: 47%;
  --color: black;

.theme-blue {
  --base: 209, 77%;
  --lightness: 43%;
  --color: white;

.theme-purple {
  --base: 288, 54%;
  --base2: 54%;
  --lightness: 46%;
  --color: #FAEAB3;

body {
  display: flex;
  justify-content: center;
  height: 100vh;
  align-items: center;
  --bgColor: hsl(var(--base), var(--lightness));
  --darkerBg: hsl(var(--base), calc(var(--lightness) - 10%));
  background-color: var(--bgColor);
  color: var(--color);

button {
  height: 3em;
  font-size: 1.3rem;
  color: inherit;
  background-color: var(--darkerBg);
  border: none;
  cursor: pointer;



                const CURRENT_THEME_KEY = "current_theme_idx"

// Look if there is a theme already set
const currentTheme = localStorage.getItem(CURRENT_THEME_KEY)
const themeToggle = document.getElementById("change-theme")

function getAvailableThemes() {
  // Ideally use something like:
  // const mainStylesheet = Array.from(document.styleSheets).find(
  //   s => s.title === "main"
  // )
const mainStylesheet =  Array.from(document.styleSheets)[0]

  const rules = Array.from(mainStylesheet.rules)

  // All themes are class selectors that start with "theme-"
  return rules
      r =>
        Boolean(r.selectorText) &&
    // get name without dot prefix on a capture group
    .map(r => r.selectorText.match(/\.([^\s]*)/)[1])

const themes = getAvailableThemes()

const mediaQuery = matchMedia("(prefers-color-scheme: dark)")
let defaultTheme = mediaQuery.matches ? "theme-dark" : "theme-white"

mediaQuery.addEventListener("change", val => {
  defaultTheme = mediaQuery.matches ? "theme-dark" : "theme-white"

function setTheme(idx = null) {
  if(idx !== null) {
    document.body.className = themes[idx]
    // store idx of selected theme
    localStorage.setItem("current_theme_idx", idx)
  } else if (currentTheme) {
    document.body.className = themes[currentTheme]
  } else {
    document.body.className = defaultTheme

themeToggle.addEventListener("click", function toggleTheme() {
  const currentTheme =  Array.from(document.body.classList).find(val => themes.includes(val)) ||
  const idx = themes.indexOf(currentTheme)
  const nextIdx = (idx + 1) % themes.length