如何使这三个图像具有相同的高度和宽度?

时间:2019-01-30 14:08:34

标签: css bootstrap-4

我是在这里使用Bootstrap 4的初学者,并且我将这些图像存储在卡中,并且希望使它们具有相同的高度和宽度,但是我不确定应该向哪个div输入属性使它工作...有人可以帮忙吗?我复制了图片下方的html代码!

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
  <div class="col-sm-4 col-md-4 col-lg-4">
    <div class="card">
      <div class="card-body">
        <img class="card-img-top img-responsive" src="https://images.autotrader.com/scaler/620/420/cms/content/articles/oversteer/2017/02-feb/02-24/262253.jpg">
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
  </div>
  <div class="col-sm-4 col-md-4 col-lg-4">
    <div class="card">
      <div class="card-body">
        <img class="card-img-top img-responsive" src="https://lonestarforklift.com/images/default-source/overview-images/hyundai-80d-9.jpg?sfvrsn=b356117d_0">
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
  </div>
  <div class="col-sm-4 col-md-4 col-lg-4">
    <div class="card">
      <div class="card-body">
        <img class="card-img-top img-responsive" src="https://www.wagehourinsights.com/wp-content/uploads/sites/697/2014/06/cars-1.jpg">
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
  </div>
</div>

How to make them equal in height and width?

谢谢大家的关注!

2 个答案:

答案 0 :(得分:0)

不能使所有图像的宽度和高度始终保持相同 但是您可以限制图像尺寸,以使您的UI看起来不会很差

只需添加以下代码即可查看

.card-body .img-responsive{
   max-height: 300px; // you can adjust this height as you want
}

希望有帮助

答案 1 :(得分:0)

您可以将<img>替换为<div>,并使用background-size: cover;属性/值按如下所示对其进行样式设置。如果您担心裁切图像,则只需将background-size的值更改为contain。如果出于ADA或网络搜寻器的原因而需要显示<img>标签,则只需将<img>嵌套在<div>中并通过样式隐藏即可。

.img {
  height: 150px; /*you can adjust this height as you want*/
  background-size: cover;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
  <div class="col-sm-4 col-md-4 col-lg-4">
    <div class="card">
      <div class="card-body">
        <div class="img" style="background-image: url(https://images.autotrader.com/scaler/620/420/cms/content/articles/oversteer/2017/02-feb/02-24/262253.jpg);"></div>
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
  </div>
  <div class="col-sm-4 col-md-4 col-lg-4">
    <div class="card">
      <div class="card-body">
        <div class="img" style="background-image: url(https://lonestarforklift.com/images/default-source/overview-images/hyundai-80d-9.jpg?sfvrsn=b356117d_0);"></div>
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
  </div>
  <div class="col-sm-4 col-md-4 col-lg-4">
    <div class="card">
      <div class="card-body">
        <div class="img" style="background-image: url(https://www.wagehourinsights.com/wp-content/uploads/sites/697/2014/06/cars-1.jpg);"></div>
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
  </div>
</div>