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

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.

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

              
                <!-- https://css-tricks.com/the-layered-look/ -->

<section class="page-wrap">
  
  <h1>The Layered Look - <span class="nobr">Faking "lowsrc"</span></h1>
  <h2>Better Responsive Images Using Multiple Backgrounds</h2>

<button data-js="retina">Toggle Retina</button>

<figure>
<div class="crop-height bg-image bg-image-radiocity"><img height="1920" width="2880" class="scale transparent" src="https://s.cdpn.io/23379/photo-radio_city_480x320bw.jpg"></div>

<div class="crop-height bg-image bg-image-camping"><img height="1920" width="2880" class="scale transparent" src="https://s.cdpn.io/23379/photo-camping_480x320bw.jpg"></div>

<div class="crop-height bg-image bg-image-crayons"><img height="1920" width="2880" class="scale transparent" src="https://s.cdpn.io/23379/photo-crayons_480x320bw.jpg"></div>

<div class="crop-height bg-image bg-image-sedona"><img height="1024" width="640" class="scale transparent" src="https://s.cdpn.io/23379/photo-sedona_512x320.jpg"></div>

<figcaption>
Faking "lowsrc": "mobile-first" images all load, then get swapped for high-res. (Empty cache, and reload page to repeat.)
</figcaption>
</figure>

<button data-js="retina">Toggle Retina</button>

</section> <!-- end .page-wrap -->

              
            
!

CSS

              
                .crop-height {
    max-height: 108px;
    height: 108px; /* lt ie8 */
    height: auto;
    overflow: hidden; }

/* base background-image class, specify image below */
.bg-image {
    width: 100%;
    /* prevent scaling past src width (or not) */
    max-width: 1024px;
    /* default background-position */
    background-position: center center;
    /* lt ie 8 */
    -ms-background-position-x: center;
    -ms-background-position-y: center;
    /* scale proportionately */
    background-size: cover;
    /* IE8 workaround - https://louisremi.github.io/background-size-polyfill/ */ }

/* mobile first "lowsrc" - see CSS media queries below for more */

.bg-image-camping {
    background-image: url(https://s.cdpn.io/23379/photo-camping_480x320bw.jpg);
    background-position: center 27%; }

.bg-image-crayons {
    background-image: url(https://s.cdpn.io/23379/photo-crayons_480x320bw.jpg);
    background-position: center 54%; }

.bg-image-radiocity {
  background-image:
    url(https://s.cdpn.io/23379/photo-radio_city_480x320bw.jpg);
    background-position: center 80%; }

.bg-image-sedona {
    background-image: url(https://s.cdpn.io/23379/photo-sedona_512x320.jpg); /* b&w */
    background-position: center 22%; }


/* if you want users to be able to easily save your transparent proxy img,
   be sure to manually declare a height and width on the img element itself
   to match the background-image src size (if the placeholder img is smaller),
   otherwise it won't fill the width of the div so user can't always click on it */

img.scale, object.scale {
  display: block; /* corrects small inline gap at bottom of containing div */
  width: 100% /* corrects obscure Firefox bug */
  max-width: 100%;
  /* just in case, to force correct aspect ratio */
  height: auto !important;
  /*width: auto\9; 
  /* ie8+9 - use modernizr instead of this \9 hack */
  /* lt ie8 */
  -ms-interpolation-mode: bicubic;
    /* force a minimum size if img src size is known */
      /* min-height: 320px; /* max-height of .crop-height */
      /* min-width: 480px; /* consistent with image ratio */
    /* optionally center if img src is not as wide as div */
      /* margin: 0 auto; */ }

.invisible {
    visibility: hidden; }

.transparent {
    /* trigger hasLayout for ie */
    zoom: 1;
    /* 0 value in filters retains layout */
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    filter: alpha(opacity=0);
    opacity: 0; }


/* ==================================================

   MEDIA QUERIES

   .hd class added by jQuery after initial page load
   .multiplebgs class added by modernizr.js

===================================================== */

@media
    /* momma bear, plus any-retina */
    only screen and (min-width : 513px),
    only screen and (-webkit-min-device-pixel-ratio: 1.5),
    only screen and (   min--moz-device-pixel-ratio: 1.5),
    only screen and (     -o-min-device-pixel-ratio: 3/2),
    only screen and (        min-device-pixel-ratio: 1.5),
    only screen and (                min-resolution: 144dpi),
    only screen and (                min-resolution: 1.5dppx) {

        /* no-multiplebgs - medium fallback
           (if you have a mid-sized image, it's probably sufficient here -
           it's unlikely someone with a retina display is on an older browser) */

        .no-multiplebgs .bg-image-sedona.hd,
        .no-js .bg-image-sedona {
            background-image: url(https://s.cdpn.io/23379/photo-sedona_1024x640.jpg); } /* sepia */

        .multiplebgs .bg-image-sedona.hd {
            background-image:
                url(https://s.cdpn.io/23379/photo-sedona_1024x640.jpg),  /* mid-sized on top */
                url(https://s.cdpn.io/23379/photo-sedona_512x320.jpg); } /* "lowsrc" on bottom */

        /* no-multiplebgs high-res fallback */
        .no-multiplebgs .bg-image-camping.hd,
        .no-js .bg-image-camping {
            background-image: url(https://s.cdpn.io/23379/photo-camping2_2880x1920.jpg); }

        /* hi-res retina */
        .multiplebgs .bg-image-camping.hd {
            background-image:
                url(https://s.cdpn.io/23379/photo-camping2_2880x1920.jpg),   /* high-res on top */
                url(https://s.cdpn.io/23379/photo-camping_480x320bw.jpg); } /* "lowsrc" on bottom */

        /* no-multiplebgs high-res fallback */
        .no-multiplebgs .bg-image-crayons.hd,
        .no-js .bg-image-crayons {
            background-image: url(https://s.cdpn.io/23379/photo-crayons2_2880x1920.jpg); }

        /* hi-res retina */
        .multiplebgs .bg-image-crayons.hd {
            background-image:
                url(https://s.cdpn.io/23379/photo-crayons2_2880x1920.jpg),   /* high-res on top */
                url(https://s.cdpn.io/23379/photo-crayons_480x320bw.jpg); } /* "lowsrc" on bottom */

        /* no-multiplebgs high-res fallback */
        .no-multiplebgs .bg-image-radiocity.hd,
        .no-js .bg-image-radiocity {
            background-image: url(https://s.cdpn.io/23379/photo-radio_city2_2880x1920.jpg); }

        /* hi-res retina */
        .multiplebgs .bg-image-radiocity.hd {
          background-image:
            url(https://s.cdpn.io/23379/photo-radio_city2_2880x1920.jpg),  /* high-res on top */
            url(https://s.cdpn.io/23379/photo-radio_city_480x320bw.jpg); } /* "lowsrc" on bottom */

    }


/* moving to larger images after the first "bump" we may choose to separate
   width-based adjustments vs. device-based logic. For example, a larger retina
   device can go directly from "lowsrc" default to high-res with no mid-size image */

@media
    /* poppa bear - smaller here for demo */
    only screen and (min-width : 716px) {

        /* no-multiplebgs - medium fallback above */

        .multiplebgs .bg-image-sedona.hd {
            background-image:
                url(https://s.cdpn.io/23379/[email protected]), /* high-res on top  */
                url(https://s.cdpn.io/23379/photo-sedona_1024x640.jpg),    /* mid-sized in middle */
                url(https://s.cdpn.io/23379/photo-sedona_512x320.jpg); }  /* "lowsrc" on bottom */
    }


@media
    /* larger retina devices, such as iPad, triggers immediately
       so we no need for mid-size image */
    only screen and (min-device-width : 768px) and (-webkit-min-device-pixel-ratio: 1.5),
    only screen and (min-device-width : 768px) and (   min--moz-device-pixel-ratio: 1.5),
    only screen and (min-device-width : 768px) and (     -o-min-device-pixel-ratio: 3/2),
    only screen and (min-device-width : 768px) and (        min-device-pixel-ratio: 1.5),
    only screen and (min-device-width : 768px) and (                min-resolution: 144dpi),
    only screen and (min-device-width : 768px) and (                min-resolution: 1.5dppx) {

        /* no-multiplebgs - medium fallback above */

        .multiplebgs .bg-image-sedona.hd {
            background-image:
                url(https://s.cdpn.io/23379/[email protected]), /* high-res on top  */
                url(https://s.cdpn.io/23379/photo-sedona_512x320.jpg); }   /* "lowsrc" on bottom,  */
    }

/* Presentational CSS Below */

.page-wrap {
  display: block;
  width: 80%;
  max-width: 1280px;
  margin: 0 auto 2em;
  padding: 1.5em 3em 3em;
  background: #fff;
  text-align: left; }

h1 {
  margin: .5em 0 0 0; }

h2 {
  margin: .25em 0 .5em;
  color: #999;
  font-weight: 300; }

figure {
  max-width: 1008px; /* max-width of .bg-image -padding */
  margin: 1.5em 0 .5em; /* change 0 to auto to center */
  padding: 8px 8px 4px 8px;
  background: #eaeaea; }

figcaption {
  margin: .5em 0;
  padding: .125em .5em;
  color: #999;
  text-align: center;
  font-size: 85%;
  font-style: italic; }

code {
  background: lightyellow;
  padding: 1px .25em;
  font-family: Menlo, Monaco, monospace; 
  font-style: normal; }

.nowrap, .nobr, .nobreak {
  white-space: nowrap;
}
              
            
!

JS

              
                  /* waits until everything is loaded, not just DOM is ready */
$(window).load(function() {

    $('.bg-image').toggleClass('hd');

});

/* don't lock class names to functionality, use '[data-js=retina]' */
$('[data-js=retina]').click(function () {
  $('.bg-image').toggleClass('hd');
});

              
            
!
999px

Console