Last active
January 21, 2016 12:22
-
-
Save surjithctly/eb7cb293aa49bad48436 to your computer and use it in GitHub Desktop.
Object fit CSS and Fallback - Cross Browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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