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>progressive-image.js demonstration</h1>

    <p><code>progressive-image.js</code> implements a lazy-loading progressive image similar to those seen on <a href="">Facebook</a> and <a href="">Medium</a>. A very small, blurred image is replaced with the full-resolution equivalent when the element is scrolled into view.</p>

    <p>Please use the code as you wish - <a href="">tweet me @craigbuckler</a> if you find it useful. <a href="">Download from GitHub...</a></p>
    <a href="" data-srcset=" 800w, 1600w" class="primary progressive replace">


    <a href="" class="boxout2 progressive replace">
      <img src="" class="preview" alt="sunset" />

      <li>fast loading, high performance, images loaded on view</li>
      <li>supports any image type (JPEG photographs are most appropriate)</li>
      <li>supports responsive images (<code>srcset</code> and <code>sizes</code> attributes)</li>
      <li>small: 1,007 bytes of JavaScript, 463 bytes of CSS (minified)</li>
      <li>any CSS reveal effect can be applied</li>
      <li>no external dependencies - works with any framework</li>
      <li>works in all modern browsers (IE10+)</li>
      <li>progressively-enhanced to work in older browsers</li>
      <li>easy to use</li>

    <a href="" class="boxout1 progressive replace">
      <img src="" class="preview" alt="tide" />

    <p>The preview image can be very small - perhaps 20px in width and saved with high JPEG compression. This typically results in an image less than 500 bytes in size. It be added to the page directly or inlined as a data URI using base-64 encoding.</p>

    <p>The large image can be any size but should match the preview image's aspect ratio. For example, 20x15 can be scaled to 200x150, 400x300 or 1600x1200.</p>

    <h2>Basic example</h2>

    <p>The page must load the CSS and JavaScript. It can be placed anywhere on the page but, typically, the CSS is loaded in the <code>&lt;head&gt;</code> and the JS is loaded just before the closing <code>&lt;/body&gt;</code> tag:</p>

    <pre><code>&lt;link rel=&quot;stylesheet&quot; href=&quot;css/progressive-image.min.css&quot;&gt;
&lt;script src=&quot;js/progressive-image.min.js&quot;&gt;&lt;/script&gt;</code></pre>

    <p>The simplest progressive image is a link to the full-size graphic around the preview. Note the <code>progressive</code> and <code>replace</code> classes on the link and the <code>preview</code> class on the image.</p>

    <pre><code>&lt;a href=&quot;full.jpg&quot; class=&quot;progressive replace&quot;&gt;
  &lt;img src=&quot;tiny.jpg&quot; class=&quot;preview&quot; alt=&quot;image&quot; /&gt;

    <p>If image loading or JavaScript fails, a blurred version of the preview image can be clicked to view the full image:</p>

    <a href="" class="full progressive" style="cursor:pointer;">
      <img src="" class="preview" alt="eyes" />

    <p>When JavaScript runs successfully, the large image is revealed when the preview is scrolled into view. CSS3 effects are used to fade and zoom the image:</p>

    <a href="" class="full progressive replace">
      <img src="" class="preview" alt="eyes" />

    <p>After replacement, the link click is disabled and resulting HTML will be:</p>

    <pre><code>&lt;a href=&quot;full.jpg&quot; class=&quot;progressive&quot;&gt;
  &lt;img src=&quot;full.jpg&quot; alt=&quot;image&quot; /&gt;

    <h2>Responsive image example</h2>

    <p><a href="">Responsive images</a> of differing sizes and resolutions can be defined in the link using the <code>data-srcset</code> and <code>data-sizes</code> attributes which map to the standard <code>srcset</code> and <code>sizes</code> attributes, e.g.</p>

    <pre><code>&lt;a href=&quot;small.jpg&quot;
  data-srcset=&quot;small.jpg 800w, large.jpg 1200w&quot;
  class=&quot;progressive replace&quot;&gt;
  &lt;img src=&quot;preview.jpg&quot; class=&quot;preview&quot; alt=&quot;image&quot; /&gt;

    <p><em>(carriage returns added to aid legibility)</em></p>

    <p>On replacement, the image code is transformed to:</p>

    <pre><code>&lt;img src=&quot;small.jpg&quot;
srcset=&quot;small.jpg 800w, large.jpg 1200w&quot;
alt=&quot;image&quot; /&gt;</code></pre>

    <p>Modern browsers will load large.jpg on screens of 800px width or greater.</p>

    <h2>Usage notes</h2>

      <li>The preview and full-size images must have identical aspect ratios, e.g. 20x10 and 1200x600.</li>

      <li>Only vertical scrolling is checked. All images in the horizontal plane will be loaded.</li>

      <li>Progressive images dynamically added to the page using JavaScript will only be replaced when a scroll or resize event occurs.</li>

      <li>You may be able to improve actual or perceived performance using <a href="">data URIs to inline images</a> or <a href="">intrinsic placeholders</a>.</li>

      <li>Firefox can struggle when replacing large images and show a noticeable flicker.</li>

    <a href="" data-srcset=" 500w, 1000w, 1500w" class="primary progressive replace">




                /* main styles */
*, *:before, *:after {
  box-sizing: border-box;
  padding: 0;
  margin: 0;

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
  font-size: 100%;
  color: #444;
  background-color: #fff;
  overflow-x: hidden;
  overflow-y: scroll;

article {
  max-width: 40em;
  padding: 0 4em;
  margin: 0 auto;

h1, h2 {
  font-weight: normal;
  margin-top: 1em;

p, a {
  margin-top: 1em;

h2 + p {
  margin-top: 0;

ul, ol {
  margin: 1em 0 0 2em;

li {
  margin-top: 0.5em;

pre {
  white-space: pre;
  padding: 2px 6px;
  margin-top: 1em;
  background-color: #ddd;
  border-radius: 3px;
  overflow: auto;

code {
  font-size: 1.1em;

img {
  max-width: 100%;

a.primary {
  width: 100vw;
  left: 50%;
  margin-left: -50vw;

a.full {
  clear: both;
  width: 100%;
  box-shadow: 0 3px 4px #000;

a.boxout1, a.boxout2 {
  clear: both;
  float: left;
  width: 60%;
  margin: 1em 1em 0 -4em;
  box-shadow: 0 3px 4px #000;

a.boxout2 {
  float: right;
  margin: 1em -4em 0 1em;

/* progressive image CSS */ {
  position: relative;
  display: block;
  overflow: hidden;
  outline: none;
} {
  cursor: default;
} img {
  display: block;
  width: 100%;
  max-width: none;
  height: auto;
  border: 0 none;
} img.preview {
  filter: blur(2vw);
  transform: scale(1.05);
} img.reveal {
  position: absolute;
  left: 0;
  top: 0;
  will-change: transform, opacity;
  animation: reveal 1s ease-out;

@keyframes reveal {
	0% { transform: scale(1.05); opacity: 0; }
	100% { transform: scale(1); opacity: 1; }



                // progressive-image.js
if (window.addEventListener && window.requestAnimationFrame && document.getElementsByClassName) window.addEventListener('load', function() {

  // start
  var pItem = document.getElementsByClassName('progressive replace'), timer;

  window.addEventListener('scroll', scroller, false);
  window.addEventListener('resize', scroller, false);

  // throttled scroll/resize
  function scroller(e) {

    timer = timer || setTimeout(function() {
      timer = null;
    }, 300);


  // image in view?
  function inView() {

    var wT = window.pageYOffset, wB = wT + window.innerHeight, cRect, pT, pB, p = 0;
    while (p < pItem.length) {

      cRect = pItem[p].getBoundingClientRect();
      pT = wT +;
      pB = pT + cRect.height;

      if (wT < pB && wB > pT) {
      else p++;



  // replace with full image
  function loadFullImage(item) {

    if (!item || !item.href) return;

    // load image
    var img = new Image();
    if (item.dataset) {
      img.srcset = item.dataset.srcset || '';
      img.sizes = item.dataset.sizes || '';
    img.src = item.href;
    img.className = 'reveal';
    if (img.complete) addImg();
    else img.onload = addImg;

    // replace image
    function addImg() {

      // disable click
      item.addEventListener('click', function(e) { e.preventDefault(); }, false);

      // add full image
      item.appendChild(img).addEventListener('animationend', function(e) {

        // remove preview image
        var pImg = item.querySelector && item.querySelector('img.preview');
        if (pImg) {
 = pImg.alt || '';




}, false);