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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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
Loading ..................

Console