I have an image list, I want images scaled into their containers which have same size. like this:
I created a jsfiddle
<div class="container">
<div class="row">
<div class="col-xs-3">
<a href="#" class="thumbnail">
<img src="http://exmoorpet.com/wp-content/uploads/2012/08/cat.png">
</a>
</div>
<div class="col-xs-3">
<a href="#" class="thumbnail">
<img src="http://www.nose2tail.co.uk/cat-matlock-derbyshire.jpg">
</a>
</div>
<div class="col-xs-3">
<a href="#" class="thumbnail">
<img src="http://www.us.onsior.com/images/3_1/cat-3_1-01.png">
</a>
</div>
<div class="col-xs-3">
<a href="#" class="thumbnail">
<img src="https://www.petfinder.com/wp-content/uploads/2012/11/155293403-cat-adoption-checklist-632x475-e1354290788940.jpg" >
</a>
</div>
</div>
</div>
How can I do that? And in my example, I defined height: 100px;, this leads to not responsive, if I resize the browser, the div's height remain unchanged. If possible, I want this image list responsive.
One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels. For example, the original image is 640×960.
Use the auto Value for Width and the max-height Property to Resize the Image in CSS. We can use the auto value to the width and set the max-height property to specify the width of an image to fit in a container. We will shrink the height of the image to the height of the container.
Change the height
and width
to max-height
and max-width
. The image won't be any bigger than it's parent.
.thumbnail img {
max-height: 100%;
max-width: 100%;
}
Updated Fiddle
.thumbnail {
height: 100px;
display: table;
}
.thumbnail img {
height: 100%;
width: 100%;
display: table-cell;
vertical-align: middle;
}
In 2017 you can use flex. Additionally you will get the images centered in the thumbnail container: updated fiddle
.thumbnail {
height: 300px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border: solid 1px blue;
}
.thumbnail img {
max-width: 100%;
max-height: 100%;
display: block;
margin: 0 auto;
border: solid 1px red;
}
This should help, at least for your example. There might be some cases in which this will not work. In that case you have to find a js solution.
.thumbnail img {
height: 100%;
width: auto;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With