旋转木马引导中的拉伸图像

时间:2019-03-10 11:21:26

标签: css html5 twitter-bootstrap bootstrap-4

我正在使用Bootstrap4。那里有一个带有图片的轮播。图像一直处于拉伸状态。出了什么问题。

.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>

它看起来像这样:

stretched

原始图片如下所示: original

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

不是使用object-fit: contain !important;,而是尝试使用object-fit: cover !important;来达到想要的效果吗?

我假设您想将背景放置在从左到右以及从上到下覆盖而不会使图像变形的位置?为此,我使用background-size: cover;object-fit: cover;

答案 1 :(得分:0)

您可以更改img height的值yo auto

.carousel-inner img {
  position: absolute;
  top: 0;
  left: 0;
  height: auto
  object-fit: containt !important;
  display: block;
}

或添加自动宽度并保持高度值:

.carousel-inner img {
  ....
  height: 32rem;
  ....
  width : auto; /* or 100% */
}