【问题标题】:Making all images appear with the same height in bootstrap使所有图像在引导程序中以相同的高度出现
【发布时间】:2015-12-22 23:04:20
【问题描述】:

我正在尝试使所有 4 行图像都具有相同的高度大小,我已经尝试使用 css 调整宽度和高度,但似乎没有任何效果,纵向图像总是比横向图像高那些,这是代码:

<div class="container" >
    <div class="jumbotron" style="background: rgba(200, 54, 54, 0.0);">
        <div class="row">
            <div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
                <a href="#"><img class="left-block img-responsive" src="images/genimage.png" width="160" height="160" alt="" /></a>
                <p style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ">One Random Item</p>
                <p>50 €</p>
            </div>
            <div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
                <a href="#"><img class="left-block img-responsive" src="images/genimage.png" width="160" height="160" alt="" /></a>
                <p style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ">Another Random Item</p>
                <p>50 €</p>
            </div>
            <div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
                <a href="#"><img class="left-block img-responsive" src="images/genimage2.jpg" height="160" width="160" alt="" /></a>
                <p style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ">Another another Random Item</p>
                <p>50 €</p>
            </div>
            <div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
                <a href="#"><img class="left-block img-responsive" src="images/genimage.png" width="160" height="160" alt="" /></a>
                <p style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ">Any other Random Item Any other Random Item </p>
                <p>50 €</p>
            </div>
        </div>

会发生这种情况:

这就是我想要的(都一样高)

【问题讨论】:

    标签: html css twitter-bootstrap


    【解决方案1】:

    您可以去掉 HTML 中的宽度/高度属性并使用 CSS 来完成。只需明确设置您的高度,并将您的宽度设置为auto。例如:

    .img-responsive {
        width: auto;
        height: 100px;
    }
    

    您需要注意在水平方向留出足够的空间,因为一旦缩放这些图像,您将无法知道它们的确切宽度。

    【讨论】:

      【解决方案2】:

      这是一种响应式的方法。

      .img-wrapper {
        position: relative;
        padding-bottom: 100%;
        overflow: hidden;
        width: 100%;
      }
      .img-wrapper img {
        position: absolute;
        top:0;
        left:0;
        width:100%;
        height:100%;
      }
      
      
      <div class="container" >
          <div class="jumbotron" style="background: rgba(200, 54, 54, 0.0);">
              <div class="row">
                  <div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
                      <a href="#" class="img-wrapper">
                           <img class="left-block" src="images/genimage.png"alt="" />
                      </a>
                      <p style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ">One Random Item</p>
                      <p>50 €</p>
                  </div>
                  <div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
                      <a href="#" class="img-wrapper">
                           <img class="left-block" src="images/genimage.png"alt="" />
                      </a>
                      <p style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ">Another Random Item</p>
                      <p>50 €</p>
                  </div>
                  <div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
                      <a href="#" class="img-wrapper">
                           <img class="left-block" src="images/genimage.png"alt="" />
                      </a>
                      <p style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ">Another another Random Item</p>
                      <p>50 €</p>
                  </div>
                  <div class="col-lg-3 col-md-6 col-sm-12 col-xs-12">
                      <a href="#" class="img-wrapper">
                           <img class="left-block" src="images/genimage.png"alt="" />
                      </a>
                      <p style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ">Any other Random Item Any other Random Item </p>
                      <p>50 €</p>
                  </div>
              </div>
      

      您将不得不稍微使用 img-wrapper 的 padding-bottom 以使您的图像显示高度,如果您希望图像不是 100% 显示 col 而是希望它们阻止您可以删除包装器的宽度 100% 并添加显示块和您想要的自定义宽度,但这应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-19
        • 1970-01-01
        • 2020-07-04
        • 2017-09-13
        • 2020-12-05
        • 1970-01-01
        相关资源
        最近更新 更多