I am working on a responsive gallery layout in which all images should cover the dimensions of their container. In order to remain the aspect ratio of each image I thought I could use the rather new CSS property object-fit
.
The implemented solution uses the flexbox
module to enforce equal heights on each column of the gallery. The cells in each column are stretched to fit in sum the height of their parent. The actual height of each cell is determined by the flexbox
module using flex: 1 0 auto
and is not necessarily the same.
Here is my problem: The images contained in each cell should now match the dimensions of their container (using object-fit: cover; width: 100%; height: 100%
). In Firefox and Internet Explorer (using an object-fit
polyfill) these styles result in the expected layout, but not in webkit browsers like Chrome or Safari. In these browsers the dimensions of each cells are correct, but not the dimensions of the images.
I could not find a related bug report nor find a flexible solution to my problem. I hope someone can help me out, as I am currently running out of ideas.
Tested in the latest versions of Chrome, Safari, Firefox and in IE 10 and 11
// Object Fit Polyfill
// @see <https://github.com/bfred-it/object-fit-images>
objectFitImages();
/* Reset */
body {
font: 100%/26px Lato, sans-serif;
padding: 5%;
}
figure {
margin: 0;
}
blockquote {
margin-left: 0;
}
img {
vertical-align: middle;
max-width: 100%;
height: auto;
font-style: italic;
}
/* Gallery */
.c-gallery {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-flow: wrap row;
-ms-flex-flow: wrap row;
flex-flow: wrap row;
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.c-gallery__column {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-flow: wrap column;
-ms-flex-flow: wrap column;
flex-flow: wrap column;
-webkit-box-flex: 1;
-webkit-flex: 1 0 33%;
-ms-flex: 1 0 33%;
flex: 1 0 33%;
}
.c-gallery__item {
-webkit-box-flex: 1;
-webkit-flex: 1 0 auto;
-ms-flex: 1 0 auto;
flex: 1 0 auto;
}
.c-gallery__item img {
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
}
<div class="c-gallery">
<div class="c-gallery__column">
<figure class="c-gallery__item">
<a href="#"><img src="http://placehold.it/640x1080" alt="Product A" /></a>
</figure>
</div>
<div class="c-gallery__column">
<div class="c-gallery__item">
<figure>
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officiis architecto voluptatum numquam totam quos neque incidunt!</p>
</blockquote>
<figcaption>
<cite>John Doe</cite>
</figcaption>
</figure>
</div>
<figure class="c-gallery__item">
<img src="http://placehold.it/640x720" alt="John Doe" />
</figure>
</div>
<div class="c-gallery__column">
<figure class="c-gallery__item">
<a href="#"><img src="http://placehold.it/640x360" alt="Product B" /></a>
</figure>
<figure class="c-gallery__item">
<a href="#"><img src="http://placehold.it/640x360" alt="Product C" /></a>
</figure>
<figure class="c-gallery__item">
<a href="#"><img src="http://placehold.it/640x360" alt="Product D" /></a>
</figure>
</div>
</div>
<script src="http://bfred-it.github.io/object-fit-images/dist/ofi.browser.js"></script>
In case you prefer CodePen, I created a demo of what I am working on: