css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <p><strong><a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API" target="_blank">Intersection Observer</a> for Lazy Load Images</strong></p>

<p>Listening to scroll events is evil. <a href="https://caniuse.com/#feat=intersectionobserver" target="_blank">Unsupported browsers</a> fall back to loading all images immediately (Safari 😞 – Edge works fine 👌).</p>

<p>This demo includes optional <code>noscript</code> fallback and animated loading image.</p>

<div class="b-image">
  <div class="b-image__inner">
      data-loaded="false" />
      <img src="https://picsum.photos/640/360/?image=1" />

// Duplicating example image for demo purposes only!
// I'm lazy and I don't like HTML pre-processors – don't @ me 😡💢
var $image = document.querySelector('.b-image');
for (var i = 2; i < 21; i++) {
  var $newImage = $image.cloneNode(true);
  var $img = $newImage.querySelector('img[data-src]');
  $img.dataset.src = $img.dataset.src.replace('?image=1', '?image=' + i);
              // Non of this CSS is strictly necessary
.b-image {
  background: #fff;
  margin-bottom: 1.5rem;
  position: relative;
  max-width: 40rem;

// Using an intrinsic ratio to maintain dimensions before the image has loaded
// this is not required for lazying loading but does avoid layout jumps
.b-image__inner {
  height: 0;
  padding-bottom: percentage(9/16);
  position: relative;

  img {
    height: 100%;
    left: 50%;
    position: absolute;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);

// House-keeping

body {
  background: #f2f0e6;
  padding: 1.5rem;

p {
  font-size: 18px;
  line-height: 1.35;
  margin: 0 0 1.5rem 0;

a {
  border-bottom: 2px solid rgba(#1d97bf, 0.1);
  color: #1d97bf;
  text-decoration: none;
  transition: border 150ms, color 150ms;

  &:hover {
    border-color: #fffbcc;
    color: #39809e;
              window.addEventListener('DOMContentLoaded', function() {
  // Find all images to lazy load
  var $images = [].slice.call(
  // Keep a count of loaded images
  var loaded = 0;
  var observer = null;
  // Trigger an image to load
  var lazyLoad = function(image) {
    // Ignore if already loaded (or non-lazy)
    if (image.getAttribute('data-loaded') !== 'false') {
    // Replace the loading src with the real thing
    image.src = image.getAttribute('data-src');
    image.setAttribute('data-loaded', 'true');
    if (!observer) {
    // Unobserve th image and disconnect the Observer
    // if all images have now loaded
    if (++loaded >= $images.length) {
  if ('IntersectionObserver' in window) {
    // Setup the Observer
    observer = new IntersectionObserver(
      function(entries) {
        entries.forEach(function(entry) {
          // Trigger the image to load if within view
          if (entry.intersectionRatio > 0) {
            // `requestAnimationFrame` is not necessary
            // but it (probably) doesn't hurt?
            requestAnimationFrame(function() {
        rootMargin: '50px 0px',
        threshold: 0.01
    // Start observing the lazy load images
    $images.forEach(function(img) {
    // If Intersection Observer is unsupported
    // load all images immediately
  } else {

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................