Reputation:
I'm facing a problem, while creating a bootstrap grid, with portrait images. When I use portrait images, it does not look good. If I only use landscape images, it looks as it should. How do I fix the issue?
Here is code:
<div class="col-md-3 col-sm-4 col-xs-6 thumb">
<div class="thumbnail" href="#">
<a href="#"><img class="img-responsive" src="http://placehold.it/400x266" alt=""></a>
<div class="caption">
<a href="#" class="btn btn-shopping btn-responsive"><i class="glyphicon glyphicon-shopping-cart"></i></a>
</div>
</div>
</div>
<div class="col-md-3 col-sm-4 col-xs-6 thumb">
<div class="thumbnail" href="#">
<a href="#"><img class="img-responsive" src="http://placehold.it/400x266" alt=""></a>
<div class="caption">
<a href="#" class="btn btn-shopping btn-responsive"><i class="glyphicon glyphicon-shopping-cart"></i></a>
</div>
</div>
</div>
<div class="col-md-3 col-sm-4 col-xs-6 thumb">
<div class="thumbnail" href="#">
<a href="#"><img class="img-responsive" src="http://placehold.it/266x400" alt=""></a>
<div class="caption">
<a href="#" class="btn btn-shopping btn-responsive"><i class="glyphicon glyphicon-shopping-cart"></i></a>
</div>
</div>
</div>
<div class="col-md-3 col-sm-4 col-xs-6 thumb">
<div class="thumbnail" href="#">
<a href="#"><img class="img-responsive" src="http://placehold.it/175x266" alt=""></a>
<div class="caption">
<a href="#" class="btn btn-shopping btn-responsive"><i class="glyphicon glyphicon-shopping-cart"></i></a>
</div>
</div>
</div>
Here is Fiddle
Upvotes: 2
Views: 9466
Reputation: 272256
Apparently, bootstrap stretches responsive images 100% wide but it does not constrain their height. Therefore images with different aspect ratio will end up with different heights (and the grid breaks).
The obvious solution is to set a fixed height on the image container and constrain + center the images inside the container.
However, this creates another problem: the aspect ratio of the container now depends on screen width; on wider screens it will become wider and on narrow screens it will become taller, sometimes toooooo tall compared to the image.
I suggest using fixed aspect ratio container and constrain + center the images inside it.
@import url("http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css");
.thumbnail {
display: block;
padding-bottom: 100%;
position: relative;
}
.thumbnail > .img-responsive {
max-width: 100%;
max-height: 100%;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Thumbnail Gallery (Square Container)</h1>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/400x300" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/300x400" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/400x300" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/300x400" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/640x360" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/400x300" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/600x200" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/400x300" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/640x360" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/200x600" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/400x300" alt=""></a>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#"><img class="img-responsive" src="http://placehold.it/200x200" alt=""></a>
</div>
</div>
</div>
Upvotes: 7
Reputation: 1515
If you want the image height to stay 226px, but the images to resize and keep their aspect ratio, I think you'll need to switch to using background images, rather than <img>
tags.
HTML:
<a href="#" style="background-image: url(http://placehold.it/400x266);"></a>
CSS:
.thumbnail > a {
display: block;
background-position: center center;
background-size: contain;
background-repeat: no-repeat;
height: 266px;
}
Upvotes: 0