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.


  <h1>Late-load your Codepen embed</h1>

  <p>A web component to late-load Codepens. Nothing, except the optional image, is loaded until you click the component, then the asset is loaded in full and embedded into the page.</p>
<!--     <button>Button</button> -->
  <code-pen description="Detect click method" slug="dyKGjQv" headingtag="h3" user="2kool2" image></code-pen>

  <p> If a background image is required add an "image" attribute. The small version of the codepen image is loaded, then blurred to compensate for the poor quality. It doesn't fit that accurately. Personally, I consider the image as superflous, but included for optional usage.</p>

  <p>Username and codepen identifiers are added to the component as attributes.</p>

  <p>An accessibile name is added via aria-labelledby pointing to the heading.</p>

  <p>Component comprises of a heading and a button. Upon click, the button is replaced by an iframe.</p>
    <figcaption id="HTML_component">HTML component</figcaption>
    <pre class="language-html" contenteditable spellcheck="false" role="textbox" aria-multiline="true" aria-labelledby="HTML_component"><code>&lt;code-pen
    description="Detect click method"
<!--     <code-pen description="Detect click method" slug="dyKGjQv" headingtag="h3" user="2kool2"> -->
<!--     </code-pen> -->
    <h2>More example embeds</h2>

  <code-pen description="Tooltip component" slug="ZERZgox" headingtag="h2"></code-pen>

  <code-pen description="Recursive dialog (modal) windows" slug="rNvovmJ" headingtag="h2"></code-pen>
  <code-pen description="Grid of radio buttons" slug="bGMqGBv" headingtag="h2"></code-pen>

  <code-pen description="Chris Coyler: Using mask repeats" slug="NWGpvKm" headingtag="h2" user="chriscoyler" image></code-pen>



code-pen {
  /* Optional variables to pass into the component */
  --cp-heading-color: var(--text);
  --cp-heading-size: var(--fs-500);
  --cp-heading-weight: var(--fw-bold);
  --cp-border-color: var(--border);
  --cp-border-image: var(--border-gradient) 1;
  --cp-border-image: ;
  --cp-bg: var(--bg);

  --cp-btn-bg: var(--accent);
  --cp-btn-bg-hover: var(--accent-hover);
  --cp-btn-border: var(--bg);
  --cp-btn-border-hover: var(--bg);
  --cp-btn-color: var(--bg);
  --cp-btn-color-hover: var(--bg);
  --cp-text-size: var(--fs-400);
  --cp-text-weight: normal;




const defaultUser = '2kool2';

class codePenHTML extends HTMLElement {

  constructor () {
    // Always call super first in constructor
    // Return value from super() is a reference to this element
    this.attachShadow({mode: 'open'});

  static get observedAttributes () {
    return ['headingtag', 'description', 'slug', 'image', 'user'];

  attributeChangedCallback (property, oldValue, newValue) {
    // BP: handle side effects, like setting ARIA attributes here, but not element attributes
    if (oldValue === newValue) return;
    this[property] = newValue;
    const slot = this.shadowRoot.querySelector(`[name=${property}]`);
    if (slot) slot.textContent = newValue;
    // console.log(property, oldValue, newValue);

    if (property === 'description') {
      const iframe = this.shadowRoot.querySelector(`[title="${oldValue}"]`);
      iframe && iframe.setAttribute('title', newValue);
    if (property === 'image') {
      const button = this.shadowRoot.querySelector('button');'--useBackgroundImage', '""')

  connectedCallback () {
    // BP: Add role, tabindex, internal state and event listeners here

  disconnectedCallback () {
    // Fires when the element is removed from the DOM
    // remove event listeners
  _fetchPenData () {
    // couldn't get this to work.
    // wanted to fetch the title from the pen itself, rather than using an attribute.
    let url;
    url = '';
    url = '';
    url = '';
    url = ''

    fetch(url, {
      // mode: 'cors'
    }).then(function (response) {
      // The API call was successful!
      return response.json();
    }).then(function (data) {
      // This is the JSON from our response
    }).catch(function (err) {
      // There was an error
      console.warn('Something went wrong.', err);
  _getHeadingTag () {
    return this.headingtag || 'h2';
  _getHeading () {
    const tag = this._getHeadingTag();
    const heading = document.createElement(tag); = 'h-' + this.slug;
    heading.setAttribute('name', 'description');
    heading.textContent = this.description;
    return heading;

  _getContainer () {
    const button = document.createElement('button');
    button.type = 'button'; = this.slug;
    button.innerHTML = this.innerHTML || '<span>Click to embed Codepen</span>';
    return button;

  _addHTML () {
    const data = this._fetchPenData();

    const heading = this._getHeading();

    const container = this._getContainer();

    const user = this.user || defaultUser;
    const slug = this.slug;

    container.addEventListener('click', event => {
      const heading = this.shadowRoot.getElementById('h-' + slug);
      const iframe = document.createElement('iframe');
      heading && iframe.setAttribute('aria-labelledby',;
      iframe.title = heading.textContent || 'Codepen embed';
      iframe.src = `${user}/embed/${slug}?default-tab=result`;
      iframe.width = '100%';
      iframe.height = '480';
      iframe.scrolling = 'no';

  _addStyles () {
    const style = document.createElement('style');
    style.textContent = `
${this._getHeadingTag()} {
  color: var(--cp-heading-color, canvasText);
  font-size: var(--cp-heading-size, 1.5em);
  font-weight: var(--cp-heading-weight, 400);
  margin: var(--v-space-2, 2rem) 0 0;
button {
  all: unset;
  box-sizing: border-box;
  border: 1px solid var(--cp-border-color, canvasText);
  border-image: var(--cp-border-image);
  cursor: pointer;
  display: grid;
  font-size: var(--cp-text-size, 1.5em);
  font-weight: var(--cp-text-weight, 400);
  min-height: 480px;
  overflow: hidden;
  place-content: center;
  position: relative;
  width: 100%;
button {
  background-color: var(--cp-bg, canvas);
  margin: var(--v-space, 1rem) 0 0;
button > span {
  background-color: var(--cp-btn-bg, canvas);
  border: 2px solid var(--cp-btn-border, canvasText);
  border-radius: 4px;
  color: var(--cp-btn-color, canvasText);
  display: block;
  line-height: calc(2ex + 6px);
  margin: 0 auto;
  max-width: 70%;
  outline: 2px solid transparent;
  outline-offset: 10px;
  padding: 16px 16px;
  pointer-events: none;
  text-align: center;
  transition: all .3s ease-out;
  z-index: 1;
button:is(:hover, :focus) > span {
  background-color: var(--cp-btn-bg-hover, canvasText);
  border-color: var(--cp-btn-border-hover, canvas);
  color: var(--cp-btn-color-hover, canvas);
  border-image: none;
  box-shadow: 0 2px 4px rgba(0,0,0,.5);
button:focus > span {
  outline-color: var(--FocusRing-outline-color, blue);
  outline-offset: 4px;
button::before {
  content: var(--useBackgroundImage);
  background-color: var(--bg-color, canvasText);
  background-image: url(${this.user}/pen/${this.slug}/image/small.png);
  background-position: top center;
  background-repeat: no-repeat;
  background-size: cover;
  filter: blur(4px);
  inset: -8px;
  position: absolute;

  get description () {
    return this.getAttribute('description');
  set description (value) {
    if (value) {
      this.setAttribute('description', value);
    } else {

  get headingtag () {
    return this.getAttribute('headingtag');
  set headingtag (value) {
    if (value) {
      this.setAttribute('headingtag', value);
    } else {

  get slug () {
    return this.getAttribute('slug');
  set slug (value) {
    if (value) {
      this.setAttribute('slug', value);
    } else {

  get image () {
    return this.getAttribute('image');
  set image (value) {
    if (value) {
      this.setAttribute('image', value);
    } else {

  get user () {
    return this.getAttribute('user');
  set user (value) {
    if (value) {
      this.setAttribute('user', value);
    } else {


// Define the new web component
if ('customElements' in window) {
  customElements.define('code-pen', codePenHTML);