在Bootstrap中使所有图片显示相同的高度

9

我想让一行中的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>

以下是发生的情况:

输入图像描述

以下是我想要的(所有图片高度相同):

输入图像描述

2个回答

17

您可以在HTML中删除宽度/高度属性,并使用CSS这样做。只需明确设置高度,将宽度设置为auto即可。例如:

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

在进行缩放后,这些图像的确切宽度将不可预知,因此您需要小心留足水平空间。


5
这里提供一种响应式的方法来解决这个问题。
.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才能使您的图像显示出其高度,如果您不想将图像显示为col的100%,而是要将它们设置为块状,则可以从包装器中删除width 100%并添加display block和您想要的自定义宽度,但这应该有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接