如何在Bootstrap - 轮播中使图片缩放到完整的高度?

6
我正在尝试在Bootstrap Carousel控件中展示一张占满整个屏幕的图片。我使用了1024px * 795px大小的图片。
我正在使用Bootstrap网站上提供的带有全尺寸图像的示例。水平方向填充了屏幕非常好,但垂直方向缩放效果不佳。我需要添加/删除哪些属性使图像占满整个屏幕,即使它们具有较小的像素尺寸?
如何在响应式设计中实现,以便图像可以适应任何大小?
我正在使用相同的CSS,并且仅在carousel.html中更改CSS。
1个回答

14

Bootstrap 4.x的更新答案(2019年)

由于现代浏览器支持object-fit,因此有更好的选项,如这里所解释的

Bootstrap 3.x的原始答案

我意识到这个答案有点晚了,但是我也面临着与Bootstrap轮播相似的挑战。要使100%的高度起作用,轮播及其任何父容器也必须是100%的高度。

我添加了一些对Bootstrap CSS类的覆盖:

html,body{height:100%;}
.carousel,.item,.active{height:100%;}
.carousel-inner{height:100%;}
.fill{width:100%;height:100%;background-position:center;background-size:cover;}

然后根据你的轮播图添加“fill”类:

<div class="container fill">
<div id="myCarousel" class="carousel slide">
  <div class="carousel-inner">
    <div class="active item">
      <div class="fill" style="background-image:url('//www.portalapp.com/images/greengrass.jpg');">
        <div class="container">
          <h1 class="pull-left pull-middle light-color"><strong><a href="#">Slide 1</a></strong></h1>

        </div>
      </div>
    </div>
    <div class="item">
      <div class="fill" style="background-image:url('//placehold.it/1024x700');">
        <div class="container">
          <h1 class="pull-left pull-middle light-color"><strong><a href="#">Slide 2</a></strong></h1>

        </div>
      </div>
    </div>
  </div>
  <div class="pull-center">
    <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
    <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
  </div>
</div>
</div>

这是一个可用的示例: http://bootply.com/59900

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