图片不适合引导程序4中的轮播

时间:2019-03-10 12:53:54

标签: css html5 twitter-bootstrap bootstrap-4

我有一个带有图像的轮播。但是图像显示得太放大了。

这是我的html代码:

<header>
    <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
      <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
      </ol>
      <div class="carousel-inner" role="listbox">
        <!-- Slide One - Set the background image for this slide in the line below -->
        <div class="carousel-item active" style="background-image: url(/images/holz_5.jpg)">
          <div class="carousel-caption d-none d-md-block">
            <h3>First Slide</h3>
            <p>This is a description for the first slide.</p>
          </div>
        </div>
        <!-- Slide Two - Set the background image for this slide in the line below -->
        <div class="carousel-item" style="background-image: url(/images/holz_6.jpg)">
          <div class="carousel-caption d-none d-md-block">
            <h3>Second Slide</h3>
            <p>This is a description for the second slide.</p>
          </div>
        </div>
        <!-- Slide Three - Set the background image for this slide in the line below -->
        <div class="carousel-item" style="background-image: url(/images/holz_4.jpg)">
          <div class="carousel-caption d-none d-md-block">
            <h3>Third Slide</h3>
            <p>This is a description for the third slide.</p>
          </div>
        </div>
      </div>
      <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>
  </header>

这是我的CSS代码:

.carousel-item {
  height: 65vh;
  min-height: 300px;
  background: no-repeat center center scroll;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  object-fit: cover !important;
}

它是这样的:

zoomed

这是原始图像:

original

为什么这么放大图片?我希望它保持原始大小,使其适合轮播。

4 个答案:

答案 0 :(得分:1)

您可以将您的代码与此进行比较。图片在这里完美缩放。

更新:从CSS代码中删除img样式标签,以避免对所有单个图像进行全局样式设置。

.carousel-item {
  background: no-repeat center center scroll;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  object-fit: cover !important;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <div class="carousel-caption d-none d-md-block">
        <h3>Third Slide</h3>
        <p>This is a description for the third slide.</p>
      </div>
      <img src="https://i.stack.imgur.com/8h5Xz.jpg" class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <div class="carousel-caption d-none d-md-block">
        <h3>Third Slide</h3>
        <p>This is a description for the third slide.</p>
      </div>
      <img src="https://i.stack.imgur.com/8h5Xz.jpg" class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <div class="carousel-caption d-none d-md-block">
        <h3>Third Slide</h3>
        <p>This is a description for the third slide.</p>
      </div>
      <img src="https://i.stack.imgur.com/8h5Xz.jpg" class="d-block w-100" alt="...">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

答案 1 :(得分:0)

这是我的整个CSS:这可能是在改写吗?

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

html {
  font-family: sans-serif;
  line-height: 1.15;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
}

.carousel-item {
  height: 65vh;
  min-height: 300px;
  background: no-repeat center center scroll;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  object-fit: cover !important;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  margin-bottom: 0.5rem;
  font-family: inherit;
  font-weight: 500;
  line-height: 1.2;
  color: inherit;
}

.row {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}

.row img {
  width: 24px;
  height: 24px;
}

.navbar-brand img {
  width: 24px;
  height: 24px;
}

.border-top {
  border-top: 1px solid #dee2e6 !important;
}

footer {
  display: block;
}

.featurette-divider {
  margin: 5rem 0; /* Space out the Bootstrap <hr> more */
}

.featurette-heading {
  font-weight: 300;
  line-height: 1;
  letter-spacing: -0.05rem;
}

@media (min-width: 40em) {
  /* Bump up size of carousel content */
  .carousel-caption p {
    margin-bottom: 1.25rem;
    font-size: 1.25rem;
    line-height: 1.4;
  }

  .featurette-heading {
    font-size: 50px;
  }
}

@media (min-width: 62em) {
  .featurette-heading {
    margin-top: 7rem;
  }
}

.marketing h2 {
  font-weight: 400;
}

.p-2 {
  padding: 0.5rem !important;
  padding-top: 0.5rem !important;
  padding-right: 0.5rem !important;
  padding-bottom: 0.5rem !important;
  padding-left: 0.5rem !important;
}

#testimonia {
  background: rgba(228, 136, 31, 0.8);
  padding: 30px 0 30px 0;
  text-align: center;
  margin-top: 40px;
  font-weight: 400;
  line-height: 1;
  letter-spacing: -0.05rem;
}

#testimonia p {
  font-size: 25px;
}

.navbar-brand {
  font-size: 20px;
  color: #000;
  font-weight: bold;
}

img {
  width: 500px;
  height: 500px; 
}

hr {
  box-sizing: content-box;
  height: 0;
  overflow: visible;
}

footer {
  padding-bottom: 30px;
}

#btn1 {
  margin-bottom: 20px;
}

.modal-header {
  background: #e4881f;
}
.full-screen {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

a {
  color: #e4881f;
}

答案 2 :(得分:0)

我认为轮播显示没有任何问题。您需要确定如何在轮播中显示图像。

有2个选项:

1)不要限制转盘的高度。

如果不限制转盘的高度,则整个图像将可见。但是在大屏幕上,高度将非常大。因此,在某个时候最好限制转盘的高度。

2)限制转盘高度。

正如我在您的代码中看到的那样,您已将高度限制为65vh。当您限制轮播项目的高度时,可以在任一页面中显示图像

  • cover模式-图像填充了容器,但是如果宽高比不同,则可能会被裁剪。
  • contain模式-无法裁剪图像,显示整个图像。但是根据长宽比,容器内部可能会水平或垂直放置空白区域。

正如我在您的代码中看到的那样,您使用了background-size: cover,因此图像被垂直裁剪以填充容器区域。

为进一步了解,您可以访问以下关于stackoverflow的文章,以了解如何在covercontain模式下放置图像: https://stackoverflow.com/a/27941378/1266447

答案 3 :(得分:0)

您需要添加background-position: 50% 50%;

然后您的图像居中。

这是我所有的css代码:

.carousel-item img {
    max-height: 80vh;
    min-height: 350px;
    background: no-repeat center center scroll;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    object-fit: cover !important;

    background-position: 50% 50%;
}