Images are created in a loop with:
<div id="row"> <div class="col-sm-6"> <div id="row"> <?php foreach ($products as $product) { ?> <div class="col-sm-6"> <a href="/admin/product/" class="thumbnail"> <div class="caption"> title<br> 10 x viewed </div> <img src="/assets/img/product/<?php echo $product['img'] ?>" class="img img-responsive full-width" /> </a> </div> <?php } ?> </div> </div> </div>
The images are from diffrent sizes some are higher then the width, and some are wider then the height. how can i force all images to have the same with as the height while being responsive. It is ok for them to have widh:100%, but i can not use height:auto, the ratio will stay then. What should i do to make my images squared while they are responsive and i dont know the %.
Example, the green shirt is not as high as his width
I want to do this without jquery so CSS only!
By making use of the :after pseudo-element and 'padding-bottom' , we can create our responsive square using only CSS. The solution relies on the somewhat counterintuitive fact that padding is calculated as a percentage of its parent element's width, not height.
Bootstrap has the built-in img-responsive class for this purpose. This applies max-width: 100%;, height: auto; and display: block; to the image so that it scales nicely to the parent element.
To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.
You can do this :
padding-bottom:100%; overflow:hidden; position:relative
FIDDLE
Explanation :
Padding top/bottom (like margin top/bottom) is calculated according to the width of parent element.As the .image
div has the same width as its parent, setting padding-bottom:100%;
give it the same height as its width so it is square (its content needs to be absolutely positioned or floated so it doesn't change the parent's size).
HTML:
<div class="col-sm-6"> <a href="#" class="thumbnail"> <div class="caption"> title<br/>3 x bekeken </div> <div class="image"> <img src="" class="img img-responsive full-width" /> </div> </a> </div>
CSS:
.image{ position:relative; overflow:hidden; padding-bottom:100%; } .image img{ position:absolute; }
Here is the best solution for every size of images http://jsfiddle.net/oboshto/p9L2q/53/
HTML the same:
<div class="col-sm-6"> <a href="#" class="thumbnail"> <div class="caption"> title<br/>3 x bekeken </div> <div class="image"> <img src="" class="img img-responsive full-width" /> </div> </a> </div>
New CSS:
.image{ position:relative; overflow:hidden; padding-bottom:100%; } .image img{ position: absolute; max-width: 100%; max-height: 100%; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%); }
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