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>
  Hello
</div>
              
            
!

CSS

              
                @layer type {
  div {
    font-weight: bold;
    font-size: 1.3em;
  }
}

@layer color {
  body {
    background: cornsilk!important;
  }
  div {
    color: rebeccapurple;
  }
}

@layer borders {
  div {
    padding: 1rem;
    border: 1px solid red;
  }
}

body {
  background: pink;
}
              
            
!

JS

              
                // create new attributeSheet
const layers = {}

// Loop through all stylesheets
document?.styleSheets && [...document.styleSheets].forEach((sheet) => {
  console.log(sheet)
  try {
    // try to read the sheet rules
    if(sheet?.rules && [...sheet.rules].some(rule => rule instanceof CSSLayerBlockRule)) {
      // loop through the rules (aka layers)
      for( const rule of [...sheet.rules] ) {
        // create a new stylesheet per layer
        if(rule instanceof CSSLayerBlockRule){
          let newSheet = new CSSStyleSheet()
          newSheet.id = rule.name
          newSheet.replaceSync(rule.cssText)
          
          // add the new sheet to our cached list of layers
          layers[rule.name] = newSheet
          // delete the rule from the stylesheet
          sheet.deleteRule([...sheet.rules].indexOf(rule))
        }
      }
    }
  } catch(e) {
    console.error(e)
  }
});


// add the layers to our adoptedStylesheets
document.adoptedStyleSheets = Object.values(layers);

// generate our checkboxes for each layer
if(Object.values(layers).length > 1) {
  let checkboxes = ``;
  Object.keys(layers).forEach(layer => { checkboxes += `<input id="${layer}" type="checkbox" checked><label for="${layer}">${layer}</label>` })
  const tmpl = `<fieldset id="layers">${checkboxes}</fieldset>`
  document.body.insertAdjacentHTML('beforeBegin', tmpl)

  // bind onchange handlers
  const inputs = document.querySelectorAll('#layers input')
  inputs.forEach( input => {
    input.addEventListener('change', (event) => {
      // get list of active layers
      const activeLayers = [...inputs].filter(el => el.checked).map(el => el.id)
      // because sheet.disabled pops adoptedStylesheets off the stack, 
      // replace all the adoptedStylesheets with only the cached list of active layers
      document.adoptedStyleSheets = activeLayers.map(activeLayer => layers[activeLayer])
    } )
  })
}
              
            
!
999px

Console