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.

Quick-add: + add another resource

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.

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="notice"></div>

<div class="article-block js-bgimg" style="background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/9186/sky-square.jpg')" data-bgimg="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9186/sky-square.jpg" data-bgimg-retina="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9186/sky-square@2x.jpg" data-bgimg-large="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9186/sky.jpg" data-bgimg-large-retina="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9186/sky@2x.jpg">
  <div class="article-block__body">
    <h1>Blue Heaven on Earth</h1>
  </div>
</div>
            
          
!
            
              body { padding: 2em; }

h1 {
  color: white;
  margin: 0;
}

.notice {
  background: gray;
  margin-bottom: 2em;
  padding: 0.5em;
  color: white;
  border-radius: 5px;
}

.article-block {
  position: relative;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  padding: 2em;
  min-height: 320px;
  border-radius: 1em;
  overflow: hidden;
}

.article-block__body {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 1.5em;
  background: rgba(0,0,0,.3);
}
            
          
!
            
              /*!
 * enquire.js v2.1.2 - Awesome Media Queries in JavaScript
 * Copyright (c) 2014 Nick Williams - http://wicky.nillia.ms/enquire.js
 * License: MIT (https://www.opensource.org/licenses/mit-license.php)
 */

!function(a,b,c){var d=window.matchMedia;"undefined"!=typeof module&&module.exports?module.exports=c(d):"function"==typeof define&&define.amd?define(function(){return b[a]=c(d)}):b[a]=c(d)}("enquire",this,function(a){"use strict";function b(a,b){var c,d=0,e=a.length;for(d;e>d&&(c=b(a[d],d),c!==!1);d++);}function c(a){return"[object Array]"===Object.prototype.toString.apply(a)}function d(a){return"function"==typeof a}function e(a){this.options=a,!a.deferSetup&&this.setup()}function f(b,c){this.query=b,this.isUnconditional=c,this.handlers=[],this.mql=a(b);var d=this;this.listener=function(a){d.mql=a,d.assess()},this.mql.addListener(this.listener)}function g(){if(!a)throw new Error("matchMedia not present, legacy browsers require a polyfill");this.queries={},this.browserIsIncapable=!a("only all").matches}return e.prototype={setup:function(){this.options.setup&&this.options.setup(),this.initialised=!0},on:function(){!this.initialised&&this.setup(),this.options.match&&this.options.match()},off:function(){this.options.unmatch&&this.options.unmatch()},destroy:function(){this.options.destroy?this.options.destroy():this.off()},equals:function(a){return this.options===a||this.options.match===a}},f.prototype={addHandler:function(a){var b=new e(a);this.handlers.push(b),this.matches()&&b.on()},removeHandler:function(a){var c=this.handlers;b(c,function(b,d){return b.equals(a)?(b.destroy(),!c.splice(d,1)):void 0})},matches:function(){return this.mql.matches||this.isUnconditional},clear:function(){b(this.handlers,function(a){a.destroy()}),this.mql.removeListener(this.listener),this.handlers.length=0},assess:function(){var a=this.matches()?"on":"off";b(this.handlers,function(b){b[a]()})}},g.prototype={register:function(a,e,g){var h=this.queries,i=g&&this.browserIsIncapable;return h[a]||(h[a]=new f(a,i)),d(e)&&(e={match:e}),c(e)||(e=[e]),b(e,function(b){d(b)&&(b={match:b}),h[a].addHandler(b)}),this},unregister:function(a,b){var c=this.queries[a];return c&&(b?c.removeHandler(b):(c.clear(),delete this.queries[a])),this}},new g});

// begin custom function

function img() {
  
  $('.js-bgimg').each(function() {
    var bgImg = $(this);
    
    var imgStandard = bgImg.data('bgimg');
    var imgRetina = bgImg.data('bgimg-retina');
    var imgLarge = bgImg.data('bgimg-large');
    var imgLargeRetina = bgImg.data('bgimg-large-retina');
    
    // simple function to replace background image
    function replaceImg(el, replacementImg) {
      el.attr('style', 'background-image: url("'+ replacementImg +'")');
    }
    
    // where are we? I set this up so you can what state is active in enquire.
    $('.notice').text('No enquire states, yet.');
    
    enquire
      // when min-width: 62em registers, run this code
      .register("screen and (min-width: 62em)", {
        match : function() {
          // update our notice, it matches
          $('.notice').text('match');
          // now we just update the image for our large image
          replaceImg(bgImg, imgLarge);
        },
        // if the screen gets smaller, it'll "unmatch" from our media query
        unmatch : function() {
          // update the notice! no longer matching.
          $('.notice').text('unmatch');
          // let's use our standard square image
          replaceImg(bgImg, imgStandard);
        },
        // we need to test for retina on setup
        setup : function() {
          // we can use matchmedia to test if we have retina or not.
          // since the standard image is pre-loaded in mark-up, we only need to test for retina
          if (matchMedia('screen and (-webkit-min-device-pixel-ratio: 2), screen and (min-resolution: 192dpi)').matches) {
            // update our notice
            $('.notice').text('setup officially official');
            // update our image for retina!
            replaceImg(bgImg, imgRetina);  
          }
        }
      })
      // okay, now we're testing our screen width AND for retina images.
      .register("screen and (min-width: 62em) and (-webkit-min-device-pixel-ratio: 2), screen and (min-width: 62em) and (min-resolution: 192dpi)", {
        match : function() {
          // let's update the notice
          $('.notice').text('retina match');
          // we have retina and a large screen, let's use the large retina graphic
          replaceImg(bgImg, imgLargeRetina);
        },      
        unmatch : function() {
          // update the notice
          $('.notice').text('retina unmatch');
          // no longer in a wide screen, so update the image to be our square retina graphic
          replaceImg(bgImg, imgRetina);
        }
      });
    
  });
  
}

img();
            
          
!
999px
Loading ..................

Console