【问题标题】:How do I align different size images如何对齐不同尺寸的图像
【发布时间】:2019-08-23 14:53:21
【问题描述】:

我正在尝试编写具有不同尺寸图像的页面,但不知道如何正确对齐它们。 这是网页的外观: https://i.imgur.com/Li17CMl.jpg

我添加了 bootstrap img-fluid 以提高响应速度。如果我设置固定高度图像不会在较小的屏幕上正确缩小。

我用“...”清理了一些不相关的数据,只是为了让代码看起来更干净。 我当前的代码:

    foreach(row...) : ?>
    <div class="col-md-6" style="padding...">

      <div class="card">
        <div class="card-body" style="padding...">

          <a href="">
            <div>
              <img class="img-fluid" style="width: 100%;" src="row->image..." alt="">
            </div>
            <div>
              <h5> title </h1>
              <h6> date </h6>
            </div>
          </a>

        </div> <!-- /card body -->
      </div> <!-- /card -->

    </div> <!-- /col-6 -->
    <?php endforeach; ?>

我希望图像能够在较小的屏幕上按比例缩小。

【问题讨论】:

  • 尝试最小高度图像
  • 这是一个php代码。请添加PHP标签
  • @MMJM altought 问题中的代码是一个 php 块问题与 php 无关。

标签: php html css image alignment


【解决方案1】:

TL:DR object-fit: cover; 可以解决问题。

解决方案 1 - 固定高度

使用此解决方案,您必须为图像的高度设置值:照片不会拉伸,但每个屏幕的比例会有所不同

.img-fluid {
   height: 200px; /* insert here your desired height*/
   object-fit:cover;
}

解决方案 2 - 真实流体

此解决方案有点棘手,但照片将始终具有您选择的比例。

.card-body a div:first-child {
   position: relative;
   width: 100%;
   height: 0;
   padding-top: 60%; /*insert value for the desired ratio. ie: 60% -> 10/6 image*/ 
}

.img-fluid {
   position: absolute;
   top:0;
   left: 0;
   right: 0;
   bottom: 0;
   object-fit: cover;
}

【讨论】:

    【解决方案2】:

    请使用以下代码:

    .img-card img{width:100%;height:300px;object-fit:cover;}
    .img-card{margin-bottom:15px;margin-top:15px;}
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    
    <div class="container">
    <div class="row">
      <div class="col-6">
        <div class="img-card">
          <img src="https://i.ytimg.com/vi/VDNd4KjELkU/maxresdefault.jpg" />
        </div>
      </div>
        <div class="col-6">
        <div class="img-card">
          <img src="https://media-cdn.tripadvisor.com/media/photo-s/0f/5e/fc/f5/inkaya-cinari.jpg" />
        </div>
      </div>
        <div class="col-6">
        <div class="img-card">
          <img src="https://thumbs.dreamstime.com/z/scenery-5680647.jpg" />
        </div>
      </div>
        <div class="col-6">
        <div class="img-card">
          <img src="https://cdn.pixabay.com/photo/2015/11/11/03/47/evening-1038148__340.jpg" />
        </div>
      </div>
    </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 1970-01-01
      • 2011-09-24
      • 2021-05-16
      • 2017-03-25
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 2020-08-15
      相关资源
      最近更新 更多