bootstrap carousel image height

时间:2017-06-03 21:35:41

标签: javascript jquery html css

我在bootstrap v4图像轮播中的图像有些问题。如果我缩小浏览器宽度,图像的高度保持不变,但图像的宽度变小,因此图像在高度上变得非常伸展。

我一直在看stackoverflow上的类似线程,并尝试了我在评论中找到的所有内容,但它仍然无法正常工作。所以我决定为此制作一个新的主题。

这是一个codepen链接https://codepen.io/anon/pen/owNyyx,但如果您不想访问codepen,我也会粘贴代码:)

所以这是我的代码

<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">
    <div class="carousel-item active">
      <img class="d-block img-fluid" src="images/image1.png" alt="First slide">
      <div class="carousel-caption d-none d-md-block">
        <h3>Random Title for image1</h3>
        <p>Random description1</p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block img-fluid" src="images/image2.png" alt="Second slide">
      <div class="carousel-caption d-none d-md-block">
        <h3>Random title for image2</h3>
        <p>Random description2</p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block img-fluid" src="images/image3.png" alt="Third slide">
      <div class="carousel-caption d-none d-md-block">
        <h3>Random title for image3</h3>
        <p>Random description3</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>

1 个答案:

答案 0 :(得分:4)

这是因为img父级.carousel-itemdisplay: fleximg是flex-child,所以它通过父级的默认align-items: stretch“拉伸”。

您可以通过多种方式解决此问题。最简单的可能是为align-items提供除stretch以外的其他值。

或者,如果您不想应用自定义CSS并在标记中保留更改,请将每个img包装在divfigure等块元素中,或者适合您站点。

.carousel-item {
  align-items: flex-start;
}
<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.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<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">
    <div class="carousel-item active">
      <img class="d-block img-fluid" src="http://lorempixel.com/1900/500" alt="First slide">
      <div class="carousel-caption d-none d-md-block">
        <h3>Cheap prizes for hairstyling</h3>
        <p>Good prizes to style your hair</p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block img-fluid" src="http://lorempixel.com/1900/500/cats/2" alt="Second slide">
      <div class="carousel-caption d-none d-md-block">
        <h3>3 Years of experience as Hairstylist</h3>
        <p>Awesome results !</p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block img-fluid" src="http://lorempixel.com/1900/500/cats/3" alt="Third slide">
      <div class="carousel-caption d-none d-md-block">
        <h3>Some random caption text</h3>
        <p>Some random description</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>