我正在使用Bootstrap 4.我有一个带图片的旋转木马。图像继续显示拉伸。 Idk是什么问题。
.carousel {
margin-bottom: 4rem;
position: relative;
}
.carousel-caption {
bottom: 3rem;
z-index: 10;
}
.carousel-inner img {
position: absolute;
top: 0;
left: 0;
height: 32rem;
object-fit: containt !important;
display: block;
}
.carousel-item {
height: 32rem;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active col-xs-12 col-sm-12">
<img src="https://i.stack.imgur.com/cPtnU.jpg" class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h1>Lorem</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident, minima.</p>
</div>
</div>
<div class="carousel-item col-xs-12 col-sm-12">
<img src="https://i.stack.imgur.com/cPtnU.jpg" class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h1>Lorem</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident, minima</p>
</div>
</div>
<div class="carousel-item col-xs-12 col-sm-12">
<img src="https://i.stack.imgur.com/cPtnU.jpg" class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h1>Lorem</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident, minima</p>
</div>
</div>
</div>
</div>
它看起来像这样:
有人可以帮我解决这个问题吗?
而不是使用object-fit: contain !important;
,你尝试使用object-fit: cover !important;
来达到你想要的效果吗?
我假设您想要将背景放置在从左到右以及从上到下覆盖而不会变形图像的位置?为此,我使用background-size: cover;
或object-fit: cover;
。
你可以将img height的值更改为auto
.carousel-inner img {
position: absolute;
top: 0;
left: 0;
height: auto
object-fit: containt !important;
display: block;
}
或者添加宽度auto并保持高度值:
.carousel-inner img {
....
height: 32rem;
....
width : auto; /* or 100% */
}