如何在 Bootstrap - Carousel 中将图像缩放到全高? [英] How to make the image scale to full height in Bootstrap - Carousel?
问题描述
我试图在 Bootstrap Carousel 控件中显示一个应该填满整个屏幕的图像.我正在使用 1024px*795px 的图像
我正在使用 Bootstrap 站点上给出的带有全尺寸图像的示例.在水平方向上,它很好地填满了屏幕.但在垂直方向上它不能很好地扩展.我应该添加/删除哪些属性以使图像填满整个屏幕,尽管它们的像素尺寸较小?
我怎样才能在响应式设计中做到这一点,以便图像缩放任意大小?
我正在使用示例站点上的 css,并且只更改 carousel.html 中的 CSS.
2019 更新 Bootstrap 4.x 的答案
由于现代浏览器现在支持 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;}
然后相应地向您的轮播添加填充"类:
<div id="myCarousel" class="轮播幻灯片"><div class="carousel-inner"><div class="活动项目"><div class="fill" style="background-image:url('//www.portalapp.com/images/greengrass.jpg');"><div class="容器"><h1 class="pull-left pull-middle light-color"><strong><a href="#">幻灯片 1</a></strong></h1>
<div class="item"><div class="fill" style="background-image:url('//placehold.it/1024x700');"><div class="容器"><h1 class="pull-left pull-middle light-color"><strong><a href="#">幻灯片2</a></strong></h1>