cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <div class="container">

  <h1>Placeholders for images with known aspect ratios</h1>

  <p>These images do not have <code>width</code> and <code>height</code> attributes, but they still take up the right amount of space before they load. They don't push text down the screen as the browser figures out what size they are. This is very helpful
    for lazy loading images of unknown size with a known aspect ratio without disturbing the user as they read the page, possibly ahead of the lazy loading image.</p>

  <button>Load images</button>

  <p>This uses the same trick as responsive YouTube embeds. Each image is wrapped in a <code>div</code> element with <code>padding-bottom</code> set to a percentage that creates the wanted aspect ratio. The <code>img</code> element inside the div
    is set to take up 100% of its container's height and width. This way, the wrapping <code>div</code> is responsible for maintaining the height of the image, not the image itself.</p>

  <h2>Square</h2>

  <div class="fixed-aspect-ratio-container square">
    <img class="lazy" data-src="https://lorempixel.com/600/600/city" alt="square image" />
  </div>

  <h2>Landscape</h2>
  <div class="fixed-aspect-ratio-container landscape">
    <img class="lazy" data-src="https://lorempixel.com/600/337/city" alt="landscape image" />
  </div>

  <h2>Portrait</h2>
  <div class="fixed-aspect-ratio-container portrait">
    <img class="lazy" data-src="https://lorempixel.com/337/600/city" alt="portrait image" />
  </div>

</div>
            
          
!
            
              body {
  font-family: sans-serif;
  line-height: 1.4;
}

h1,
h2 {
  font-weight: normal;
}

.container {
  max-width: 600px;
  margin: auto;
}
.fixed-aspect-ratio-container {
  position: relative;
  height: 0;
  outline: 1px solid #888;

  &.square {
    padding-bottom: 100%;
  }

  &.landscape {
    padding-bottom: 56.25%;
  }

  &.portrait {
    padding-bottom: 177.777777778%;
  }

  > * {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
  }
}

button {
  font-size: 1.25em;
  padding: 0.25em;
  border-radius: 5px;
}

            
          
!
            
              document.querySelector("button").addEventListener("click", function(event) {

  // find lazy loaded things that use the src tag(image & iframes)
  const images = document.querySelectorAll(".lazy[data-src]");

  // update their src attribute
  Array.from(images).forEach(function(image) {
    image.src = image.dataset.src;
  });
});

            
          
!
999px
🕑 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 ..................

Console