Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="object-fit__container object-fit__container--large">

  <video class="object-fit__cover" src="http://www.asirobots.com/wp-content/uploads/pages/video/Companies-BG-video-draft.mp4" autoplay muted loop></video>

  <h1>Object-Fit with IE and Edge fallback</h1>
  <p>
    Requires Modernizr detection of object-fit css. Description coming soon.
    </br>
    </br>
    <a href="https://codepen.io/arniebradfo/pen/jEEWeV"></a>
    </br>
    </br>
    <span class="status"></span>
  </p>

</div>

<div class="object-fit__container">
  <h1>Cover</h1>
  <img class="object-fit__cover" src="http://blogs-images.forbes.com/startswithabang/files/2016/01/Milky_Way_at_Concordia_Camp_Karakoram_Range_Pakistan-1200x800.jpg">
</div>

<div class="object-fit__container">
  <h1>Fill</h1>
  <img class="object-fit__fill" src="http://blogs-images.forbes.com/startswithabang/files/2016/01/Milky_Way_at_Concordia_Camp_Karakoram_Range_Pakistan-1200x800.jpg">
</div>

<div class="object-fit__container">
  <h1>Contain</h1>
  <img class="object-fit__contain" src="http://blogs-images.forbes.com/startswithabang/files/2016/01/Milky_Way_at_Concordia_Camp_Karakoram_Range_Pakistan-1200x800.jpg">
</div>

<div class="object-fit__container">
  <h1>None</h1>
  <img class="object-fit__none" src="http://blogs-images.forbes.com/startswithabang/files/2016/01/Milky_Way_at_Concordia_Camp_Karakoram_Range_Pakistan-1200x800.jpg">
</div>

<div class="object-fit__container">
  <h1>Scale-Down</h1>
  <img class="object-fit__scale-down" src="http://blogs-images.forbes.com/startswithabang/files/2016/01/Milky_Way_at_Concordia_Camp_Karakoram_Range_Pakistan-1200x800.jpg">
</div>


              
            
!

CSS

              
                
/*this css is nessary*/
.object-fit{
  &__container{   }
  &__cover{      object-fit: cover;      }
  &__fill{       object-fit: fill;       }
  &__contain{    object-fit: contain;    }
  &__none{       object-fit: none;       }
  &__scale-down{ object-fit: scale-down; }
}
img, video, svg{
  height: 100%;
  width:  100%;
  opacity: 0.6;
}


/*this css is for decoration*/

*{
  box-sizing: border-box;
}

html, body{
  height: 100%;
  width:  100%;
  font-family: sans-serif;
  color: white;
}

.object-fit__container{
  position:relative;
  overflow: hidden;
  margin: 2%;
  height: 200px;
  width:  100%;
  background:rgba(0,0,0,1);
  &--large{
    height:400px;
  }
  img{
    opacity:0.75;
  }
  h1, p{
    z-index:2;
    display:block;
    width:100%;
    text-align:center;
    padding:30px;
    position:absolute;
  }
  h1{
    top:5%;
    text-transform:uppercase;
    font-size:30px;
  }
  p{
    bottom:5%;
  }
  a{
    color:white;
  }
}
              
            
!

JS

              
                // objectFitFallback.js
// requires modernizr

// REFERENCES:
// https://css-tricks.com/almanac/properties/o/object-fit/

(function(Modernizr) {

  // switch between height:100% and width:100% based on comparison of img and container aspect ratios
  function coverFillSwitch(container, img, invert) {
    if (!container || !img) return false;

    var imgHeight = img.naturalHeight || img.videoHeight;
    var imgWidth = img.naturalWidth || img.videoWidth;
    var containerRatio = container.offsetWidth / container.offsetHeight;
    var imgRatio = imgWidth / imgHeight;

    var ratioComparison = false;
    if (imgRatio >= containerRatio) ratioComparison = true;
    if (invert) ratioComparison = !ratioComparison; // flip the bool

    if (ratioComparison) {
      img.style.height = '100%';
      img.style.width = 'auto';
    } else {
      img.style.height = 'auto';
      img.style.width = '100%';
    }

  }

  function objectFitResize() {
    var i, img, container;

    var imgsCover = document.getElementsByClassName('object-fit__cover');
    for (i = 0; i < imgsCover.length; i++) {
      img = imgsCover[i];
      container = img.parentElement;
      if (container.classList.contains('object-fit__container')) {
        coverFillSwitch(container, img);
      }
    }

    var imgsContain = document.getElementsByClassName('object-fit__contain');
    for (i = 0; i < imgsContain.length; i++) {
      img = imgsContain[i];
      container = img.parentElement;
      if (container.classList.contains('object-fit__container')) {
        coverFillSwitch(container, img, true);
      }
    }
  }

  // add absolute center image css properties
  function applyStandardProperties(container, img) {
    var containerStyle = window.getComputedStyle(container);
    if (containerStyle.overflow !== 'hidden') container.style.overflow = 'hidden';
    if (containerStyle.position !== 'relative' &&
      containerStyle.position !== 'absolute' &&
      containerStyle.position !== 'fixed') container.style.position = 'relative';
    img.style.position = 'absolute';
    img.style.top = '50%';
    img.style.left = '50%';
    img.style.transform = 'translate(-50%,-50%)';
  }

  function objectFitInt() {

    var imgs = document.querySelectorAll('[class*="object-fit__"]');
    for (var i = 0; i < imgs.length; i++) {

      var type = 'cover';
      var img = imgs[i];
      var container = img.parentElement;

      if (img.classList.contains('object-fit__container')) type = 'container';
      if (img.classList.contains('object-fit__cover')) type = 'cover';
      if (img.classList.contains('object-fit__fill')) type = 'fill';
      if (img.classList.contains('object-fit__contain')) type = 'contain';
      if (img.classList.contains('object-fit__none')) type = 'none';
      if (img.classList.contains('object-fit__scale-down')) type = 'scale-down';

      switch (type) {
        case 'container':
          break;
        case 'cover':
          coverFillSwitch(container, img);
          applyStandardProperties(container, img);
          break;
        case 'contain': // opposite of cover
          coverFillSwitch(container, img, true);
          applyStandardProperties(container, img);
          break;
        case 'fill':
          img.style.height = '100%';
          img.style.width = '100%';
          applyStandardProperties(container, img);
          break;
        case 'none':
          img.style.height = 'auto';
          img.style.width = 'auto';
          applyStandardProperties(container, img);
          break;
        case 'scale-down':
          img.style.maxHeight = '100%';
          img.style.maxWidth = '100%';
          img.style.height = 'auto';
          img.style.width = 'auto';
          applyStandardProperties(container, img);
          break;
        default:
          break;
      }
    }
  }

  var resizeTimeout;

  function resizeThrottler() { // @source https://developer.mozilla.org/en-US/docs/Web/Events/resize
    if (!resizeTimeout) {
      resizeTimeout = setTimeout(function() {
        resizeTimeout = null;
        objectFitResize();
      }, 66); // The objectFitResize will execute at a rate of 15fps
    }
  }

  // Modernizr.objectfit = Modernizr.testAllProps('objectFit'); // detect IE and Edge
  if (!Modernizr.objectfit) {
    window.addEventListener('load', objectFitInt, false);
    window.addEventListener('resize', resizeThrottler, false);
  }

})(Modernizr);
              
            
!
999px

Console