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

              
                <dialog id="lightbox">
  <header>
    <button>Close</button>
  </header>
  <sl-ot></sl-ot>
</dialog>

<main id="demo">
  <picture>
    <img src="https://assets.codepen.io/2585/image_fx_a_black_wolf_in_armor_wrecking_a_steampunk_ci.jpg" alt="" />
  </picture>
  <picture>
    <img src="https://assets.codepen.io/2585/image_fx_a_raptor_in_armor_wrecking_a_cyberbunk_city_w.jpg" alt="" />
  </picture>
  <picture>
    <img src="https://assets.codepen.io/2585/image_fx_cyberpunk_city_scene_with_neon_streaks_of_lig+%284%29.jpg" alt="" />
  </picture>
</main>

<figure>
  <picture>
    <img src="https://assets.codepen.io/2585/image_fx_cyberpunk_city_scene_with_neon_streaks_of_lig.jpg" alt="" />
  </picture>
</figure>
              
            
!

CSS

              
                /* these styles ensure the clicked image is on top of the popover and it's backdrop */
::view-transition-group(--light-box),
::view-transition-group(--light-box-backdrop) {
  z-index: 1;
}

::view-transition-group(--light-box-img) {
  z-index: 2;
}

#lightbox {
  view-transition-name: --light-box;
  
  &::backdrop {
    view-transition-name: --light-box-backdrop;
  }
}

@layer support {
  * {
    box-sizing: border-box;
    margin: 0;
  }

  html {
    block-size: 100%;
    color-scheme: dark light;
  }

  body {
    min-block-size: 100%;
    font-family: system-ui, sans-serif;

    display: grid;
    place-content: center;
    place-items: center;
  }
  
  img {
    max-inline-size: 100%;
  }

  main {
    display: grid;
    /* note that grid will hold these slots for the clicked img */
    /* because the images are inside a <picture> */
    /* the effect depends on setting display: none on the <img> */
    grid-template-columns: repeat(3, 25vi);
    gap: 1rem;
    padding: 2rem;

    img {
      cursor: pointer;
    }
  }
  
  figure {
    > picture {
      display: block;
      inline-size: 50px;
      aspect-ratio: 1;
    }
  }
  
  #lightbox {
    max-inline-size: 80vw;
    margin: auto;
    border: none;
    background: none;

    > header {
      display: flex;
      place-content: end;

      > button {
        color: white;
        background: #0003;
        cursor: pointer;
        background: none;
        border: none;
        padding-inline: 0;
        padding-block: .5ch;
        font-size: 1.25rem;
      }
    }
    
    img {
      max-block-size: 80vh;
    }

    &::backdrop {
      background-image: radial-gradient(#0003, 25%, #000e);
    }
  }
}
              
            
!

JS

              
                // TODO:
// this could/should be a web component that could make the pic private, hydrate itself and "just drop in"
// extra sauce: add pointer events to drag the image to close

// state variable to share the clicked image to hide/show functions
let pic = null

function showLightbox() {
  // give the clicked image a VT name
  // this is what enables the position/scale morphing
  // if the user is cool with motion
  if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches)
    pic.style.viewTransitionName = '--light-box-img'
  
  function mutate() {
    // clone it into the lightbox dialog
    lightbox.querySelector('sl-ot').innerHTML = pic.outerHTML
    
    // hide the source image
    pic.style.display = 'none'
    
    // show the dialog that has the image in it
    lightbox.showModal()
  }
  
  // take a snapshot of the image where it's at
  document.startViewTransition
    ? document.startViewTransition(mutate)
    : mutate()
}

async function hideLightbox() {
  function mutate() {
    // hide the popover
    lightbox.close()
    
    // restore the image where it came from
    pic.style.display = null
  }
  
  // take a snapshot of the dialog with the image open
  document.startViewTransition
    ? await document.startViewTransition(mutate).updateCallbackDone
    : mutate()

  // once the updateCallback is done
  // remove the view transition name
  pic.style.viewTransitionName = null
  
  // put the lightbox subject back to null
  pic = null
}

document.onclick = e => {
  // if the clicked element is not an img, bail
  if (e.target.nodeName != 'IMG') return
  
  // stash clicked image
  pic = e.target
  
  // show the lightbox
  showLightbox()  
}

// when the lightbox close button is clicked
lightbox.querySelector(':scope > header > button').addEventListener('click', e => {
  hideLightbox()
})

// light dismiss
lightbox.addEventListener('click', e => {
  if (e.target.nodeName === 'DIALOG')
    hideLightbox()
})

// when esc is clicked, close the lightbox
document.addEventListener('keydown', async e => {
  if (!lightbox.open) return
  
  if (e.key === 'Escape') {
    e.preventDefault()
    hideLightbox()
  }
})
              
            
!
999px

Console