Skip to content

Instantly share code, notes, and snippets.

@surjithctly
Last active January 21, 2016 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surjithctly/eb7cb293aa49bad48436 to your computer and use it in GitHub Desktop.
Save surjithctly/eb7cb293aa49bad48436 to your computer and use it in GitHub Desktop.
Object fit CSS and Fallback - Cross Browser
// Thanks Primož Cigler
// https://medium.com/@primozcigler/neat-trick-for-css-object-fit-fallback-on-edge-and-other-browsers-afbc53bbb2c3#.rydx9pub4
if ( ! Modernizr.objectfit ) {
$('.post__image-container').each(function () {
var $container = $(this),
imgUrl = $container.find('img').prop('src');
if (imgUrl) {
$container
.css('backgroundImage', 'url(' + imgUrl + ')')
.addClass('compat-object-fit');
}
});
}
.post {
&__image-container {
width: 120px; // the same width and height as for the <img>
height: 120px;
&.compat-object-fit {
position: relative; // for the link fix
background-size: cover;
background-position: center center;
> a { // link fix
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.post__featured-image { // hide image if object fit is not supported
display: none
}
}
}
&__featured-image {
width: 120px;
height: 120px;
object-fit: cover;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment