简体   繁体   English

Bootstrap:等高图像的响应行,宽度基于图像长宽比

[英]Bootstrap: Responsive row of equal-height images, width based on image aspect ratio

I feel like I've read through about 20 or so similar posts, but none seem to have the exact thing I'm looking for. 我觉得我已经阅读了大约20篇左右的类似文章,但是似乎都没有我想要的东西。

The problem: 问题:

I have a row of images (say 3 for example). 我有一排图像(例如说3张)。 Each image has a different aspect ratio. 每个图像具有不同的纵横比。 I want to display all images in a row such that their heights are equal. 我想连续显示所有图像,以使它们的高度相等。 The width of the column containing the images is fluid. 包含图像的列的宽度是可变的。 I'm not fixing the height myself and I don't want to arbitrarily limit the height of the images. 我自己并没有固定高度,也不想随意限制图像的高度。 The images (and their padding) fill the horizontal space completely; 图像(及其填充)完全填满了水平空间; this determines the height (because the aspect ratio is maintained). 这决定了高度(因为保持了宽高比)。 When the window shrinks (to mobile sizes) I want all the images to be placed one below the other like bootstrap's responsive layout usually does. 当窗口缩小(移动大小)时,我希望所有图像都像引导程序的响应式布局一样放在另一个图像的下面。

I've tried many things. 我已经尝试了很多东西。 I'm pretty sure that I can't use the bootstrap column classes (like col-md-4) because they change the size of their content to maintain a fixed width. 我非常确定我不能使用bootstrap列类(例如col-md-4),因为它们会更改其内容的大小以保持固定的宽度。 As far as I can tell, bootstrap doesn't offer a clean solution to this. 据我所知,bootstrap并没有提供一个干净的解决方案。 I've also tried table-responsive, which doesn't seem to help. 我还尝试了表响应式,这似乎无济于事。 Help is very much appreciated. 非常感谢您的帮助。

Here is a solution: 这是一个解决方案:

 td img { height: 12vw; width: auto; margin: 4px; } div { border: 1px solid gray; } table { width: auto; margin: 0 auto; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div class="row"> <table> <tr> <td> <img src="//placehold.it/200x150/2255EE"> </td> <td> <img src="http://placekitten.com/300/450"> </td> <td> <img src="http://placekitten.com/450/300"> </td> </tr> </table> <table> <tr> <td> <img src="http://placehold.it/200x400"> </td> <td> <img src="http://placehold.it/400x200"> </td> <td> <img src="http://placehold.it/300x300"> </td> </tr> </table> </div> </div> 

Try this . 尝试这个 。 whatever image you add to these three div they will always be equal in height without cutting the image. 无论您添加到这三个div的图像如何,它们的高度始终相等,而不会剪切图像。

Irepeated the loop 45 times to show end result clearly you can do this only 15 times too by increasing width-change from 0.5% to 1% in the code. 重复执行45次循环以清楚地显示最终结果,您也可以通过将代码中的宽度变化从0.5%增加到1%来将其重复执行15次。 (last line of for loop). (for循环的最后一行)。

also you can make the width-change to 2% and make loop go only four rimes and then crop the extra height from the images (it won't be more then 15 to 20px or so) and you have to repeat it 4 times instead of 45. 您也可以将宽度更改为2%,并使循环只走四个边缘,然后从图像中裁剪出多余的高度(不会超过15到20像素左右),而必须重复4次45。

 $(document).ready(function(){ var width = [33.33,33.33,33.33]; for(var j=0;j<45;j++){ var height1 = $(".imagecol").eq(0).css("height"); var height3 = $(".imagecol").eq(1).css("height"); var height3 = $(".imagecol").eq(2).css("height"); var maxH=height1; var maxHi=0; var minH=height1; var minHi=0; for(var i=1;i<3;i++){ if($(".imagecol").eq(i).css("height")>maxH){ maxH = $(".imagecol").eq(i).css("height"); maxHi=i; } if($(".imagecol").eq(i).css("height")<minH){ minH = $(".imagecol").eq(i).css("height"); minHi=i; } } width[maxHi] = width[maxHi]-0.25; width[minHi] = width[minHi]+0.25; $(".imagecol").eq(maxHi).css("width",(width[maxHi]+"%")) $(".imagecol").eq(minHi).css("width",(width[minHi]+"%")) } for(var i=0;i<3;i++){ $(".imagecol").eq(i).css("height",minH); } }); 
 *{ box-sizing:border-box; } .imagecol{ width:33.33%; margin:0; padding:0; float:left; } .imagerow{ width:100%; margin:0; padding:0; } .displayIm{ width:100%; margin:0; padding:5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="imagerow"> <div class="imagecol"> <img class="displayIm" src="https://media.zigcdn.com/media/content/2016/Feb/bike-of-the-year-pulsar-rs200-m_720x540.jpg" /> </div> <div class="imagecol"> <img class="displayIm" src="https://static.pexels.com/photos/50704/car-race-ferrari-racing-car-pirelli-50704.jpeg" /> </div> <div class="imagecol"> <img class="displayIm" src="http://www.iwallfinder.com/files/81/the-second-series-of-the-vertical-screen-flower-photo-40456.jpg" /> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM