Skip to content

Instantly share code, notes, and snippets.

@r4fx
Last active May 18, 2016 15:04
Show Gist options
  • Save r4fx/48d26b5303ec4c36f3e721ebc7acc92a to your computer and use it in GitHub Desktop.
Save r4fx/48d26b5303ec4c36f3e721ebc7acc92a to your computer and use it in GitHub Desktop.
css object-fit polyfill
if ('objectFit' in document.documentElement.style === false) {
var imgToFit = document.querySelectorAll('img.object-fit');
for (var i = 0; i < imgToFit.length; i++) {
var container = imgToFit[i].parentElement;
var imgToFitUrl = imgToFit[i].src;
if (imgToFitUrl) {
container.classList.add('no-object-fit');
container.style.backgroundImage = 'url(' + imgToFitUrl + ')';
}
}
}
.object-fit {
width: 100%;
height: 100%;
object-fit: cover;
}
.no-object-fit {
background-size: cover;
background-position: center center;
.object-fit {
opacity: 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment