bootstrap 4 carousel 相同高度用于不同的图像尺寸和屏幕尺寸

Edd*_*dyG 5 html css twitter-bootstrap bootstrap-4

这是 w3schools 的轮播示例。

<div id="demo" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <li data-target="#demo" data-slide-to="0" class="active"></li>
    <li data-target="#demo" data-slide-to="1"></li>
    <li data-target="#demo" data-slide-to="2"></li>
  </ul>

  <!-- The slideshow -->
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="la.jpg" alt="Los Angeles">
    </div>
    <div class="carousel-item">
      <img src="chicago.jpg" alt="Chicago">
    </div>
    <div class="carousel-item">
      <img src="ny.jpg" alt="New York">
    </div>
  </div>

  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>

</div>
Run Code Online (Sandbox Code Playgroud)

为了使轮播响应,我在 css 中添加了以下内容:

.carousel-item-next, .carousel-item-prev, .carousel-item.active {
    display: block !important;
}
Run Code Online (Sandbox Code Playgroud)

在我的网站中,我有几张具有相同尺寸的图像(高度 300 像素和宽度 600 像素),但只有其中一个具有高度 300 像素和宽度 300 像素)。所以在css中我设置:

.carousel-item>img{
    max-height: 300px;
}
Run Code Online (Sandbox Code Playgroud)

这样,当图像在轮播中滑动时,它们具有相同的高度。

但是当我调整页面大小或在较小尺寸的设备上打开它时,由于轮播是响应式的,高度会降低。但是 300 像素宽度的图像的高度较小,宽度 600 像素的图像的高度较大。因此,当轮播滑动图像时,当到达 300x300 图像时,高度会增加,而当到达 600x600 图像时,高度会降低。

我怎样才能让所有尺寸的所有图像的轮播高度相似?

Edd*_*dyG 4

对我来说效果很好的解决方案是为媒体查询设置最大高度。例如:

@media (max-width: 688px) {
    .carousel-item>img {
        max-height: 146px;
    }
}
Run Code Online (Sandbox Code Playgroud)

为了获得最大高度值,我引用了检查元素。