简体   繁体   English

Bootstrap 3响应图像并排相同的高度

[英]Bootstrap 3 responsive images side by side same height

Is it possible to use 2 responsive images side by side with same height with Bootstrap 3 ? 是否可以使用Bootstrap 3并排使用相同高度的2个响应图像? At the moment the col-sm-4 hasn't the same height. 目前col-sm-4的高度不同。

<div class="container">
    <div class="row">                                     
        <div class="col-sm-4">                 
            <img class="img-responsive" src="http://placehold.it/400x400" />
        </div>                  
        <div class="col-sm-8">
            <img class="img-responsive" src="http://placehold.it/800x400" />
        </div>                  
    </div>
</div>

Demo Fiddle: http://jsfiddle.net/u9av6/3/ 演示小提琴: http //jsfiddle.net/u9av6/3/

Thanks! 谢谢!

try this: 尝试这个:

<div class="col-sm-4"><img src="http://placehold.it/400x400" height="220" /></div>

<div class="col-sm-8"><img src="http://placehold.it/800x400" height="220" /></div>

here's my solution with a simple wrapper. 这是我用简单包装器的解决方案。 The idea is to deal with the aspect ratio : 想法是处理纵横比

jsfiddle demo jsfiddle 演示

Here's a working version, with jsfiddle demo . 这是一个工作版本,带有jsfiddle demo

<div class="col-sm-4"><img style="max-height:220px" src="http://placehold.it/400x400" /></div>                       
<div class="col-sm-8"><img style="max-height:220px" src="http://placehold.it/800x400" /></div>

Maybe your problem is that in your code, the padding is not considering. 也许你的问题是在你的代码中,填充不考虑。

Your 400px and 800px is nice, but with padding considering it's not good. 你的400px800px是不错的,但考虑到填充不好。

In this fiddle : http://jsfiddle.net/Lrc5t/1/ , with no padding, they keep the same height. 在这个小提琴: http//jsfiddle.net/Lrc5t/1/ ,没有填充,它们保持相同的高度。 But it's not nice without padding... 但是如果没有填充,那就不好了......

The padding in each .row is 15px left and right. 每个.row的填充是左右15px

html: HTML:

<div class="row">                   
    <div class="col-sm-4 nopadding"><img class="img-responsive" src="http://placehold.it/400x400"></div>                       
    <div class="col-sm-8 nopadding"><img class="img-responsive" src="http://placehold.it/800x400"></div>              
</div>

css: CSS:

.nopadding{
    padding-left: 0px !important;
    padding-right:0px !important;    
}

UPDATE : 更新:

fiddle : http://jsfiddle.net/Lrc5t/2/ 小提琴http//jsfiddle.net/Lrc5t/2/

Without paddding is not nice so , just inverse in adding extra padding to right image : 没有paddding是不好的,所以,只需向右图像添加额外的填充:

<div class="row">

<div class="col-sm-4"><img class="img-responsive" src="http://placehold.it/400x400"></div>

<div class="col-sm-8 nopadding-two"><img class="img-responsive" src="http://placehold.it/800x400"></div>

</div>

.nopadding-two{
    padding-right:45px !important;
}

ps : You can choose to add your padding at left instead or right... in order to keep the padding in middle. ps :你可以选择在左边或右边添加填充...以便将填充保持在中间。 And... nopadding-one and text-right class are to delete...(I forgot)... 并且... nopadding-onetext-right class将删除...(我忘了)...

UPDATE AFTER COMMENT: * Why 45px ? 评论后更新: * 为什么45px? * Image 1 is float:left so it has just the padding-right at 15px Image 2 has padding left and right at 15px {=30px} So in order do equality (to get same constraints) you need to transmit the total padding to image 2 * 图像1是浮动的:左边只有填充右边15px图像2左边和右边填充15px {= 30px}所以为了做相等(得到相同的约束)你需要将总填充传输到图像2

To make the images displaying side by side, you need to do three things: 要使图像并排显示,您需要做三件事:

1st : make all your images responsive in the html file like this: 1st :在html文件中使所有图像响应如下:

<div class="row">

      <div class="col-xs-3 left-image"  >

        <img  class="img-responsive" src="../img/fourthimg.jpg">
      </div>
      <div class="col-xs-9 right-image">

          <img class="img-responsive" src="../img/firstimg.jpg" >

     </div>

    </div>

This is for putting two images side by side. 这是为了并排放置两个图像。 You could have as many images as possible. 你可以拥有尽可能多的图像。

2nd: Inside your css, give the height to these images like this: 第二:在你的CSS中,给这些图像的高度如下:

   .img-responsive {display:block; height: 600px; width: 100%;}

The width is set to 100% meaning that both (for this example)/all the images would cover the 100% part of the column width provided in your html code. 宽度设置为100%,这意味着两者(对于此示例)/所有图像将覆盖html代码中提供的列宽的100%部分。

3rd (Most important) : CSS has this property to automatically leave margins/paddings on right and left when you put an image inside a div and this padding is associated with the image or any other element that you want to put inside a div. 第3个(最重要的): CSS具有此属性,当您将图像放入div中时,此属性会自动在左右边缘留下边距/填充,并且此填充与图像或要放入div中的任何其他元素相关联。

To tackle this, go to you css file and for each image, set the left-padding and right padding to 0px just like this: 要解决这个问题,请转到css文件,对于每个图像,将左边填充和右边填充设置为0px,如下所示:

.right-image{
padding-left:0px;
padding-right:0px;

}


.left-image{

 padding-right: 0px;
 padding-left: 0px;


}

This is a working example and explanation of how to put in images side by side using bootstrap! 这是一个工作示例和如何使用bootstrap并排放入图像的说明!

Check this code. 检查此代码。 This code will look for the smallest height of the image and set it as max-height of the other images. 此代码将查找图像的最小高度,并将其设置为其他图像的最大高度。 The rest of the image will be hidden. 图像的其余部分将被隐藏。 Check it out. 看看这个。

<script>
   setTimeout(function () {
      equalheight('.portfolio-item');
   }, 3000);
   function equalheight($that) {
      $minHeight = 0;
      $($that).each(function (index) {
         $thisHeight = $(this).children('.portfolio-link').innerHeight();
         if ($minHeight == 0) {
            $minHeight = $thisHeight;
         } else {
            if ($minHeight > $thisHeight) {
               $minHeight = $thisHeight;
            }
         }
      });
      $($that).children('.portfolio-link').css('max-height', $minHeight);
      $($that).children('.portfolio-link').css('overflow', 'hidden');
   }
</script>

My html is 我的HTML是

                <div class="col-md-4 col-sm-6 portfolio-item">
                  <a href="#portfolioModal1" class="portfolio-link" data-toggle="modal" style="max-height: 225px; overflow: hidden;">
                     <div class="portfolio-hover">
                        <div class="portfolio-hover-content">
                           <i class="fa fa-plus fa-3x"></i>
                        </div>
                     </div>
                     <img src="images/trip-01.jpg" class="img-responsive" alt="">
                  </a>
                  <div class="portfolio-caption">
                     <h4>Round Icons</h4>
                     <p class="text-muted">Graphic Design</p>
                  </div>
               </div>
               <div class="col-md-4 col-sm-6 portfolio-item">
                  <a href="#portfolioModal2" class="portfolio-link" data-toggle="modal" style="max-height: 225px; overflow: hidden;">
                     <div class="portfolio-hover">
                        <div class="portfolio-hover-content">
                           <i class="fa fa-plus fa-3x"></i>
                        </div>
                     </div>
                     <img src="images/trip-02.jpg" class="img-responsive" alt="">
                  </a>
                  <div class="portfolio-caption">
                     <h4>Startup Framework</h4>
                     <p class="text-muted">Website Design</p>
                  </div>
               </div>

if you want constant height, should add these properties: 如果你想要恒定的高度,应该添加这些属性:

height: 200x ;   # any constant height
object-fit: cover;

Or else you want width constant, should add these properties: 或者你想要宽度不变,应该添加这些属性:

width: 200px ;  # any constant height
object-fit: cover;

object-fit: cover, fixed the problem of stretching up of the image in my case. object-fit:cover,修复了我案例中图像拉伸的问题。

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

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