【问题标题】:How to make background image in Bootstrap 4 carousel full-width?如何在 Bootstrap 4 轮播中制作全角背景图像?
【发布时间】:2017-07-31 03:44:38
【问题描述】:

我有一个用 Bootstrap 4 编写的 2 幻灯片轮播。每张幻灯片都有一个图像作为其背景,但我无法将这些图像设置为全宽。

通常我会使用background-size: coverbackground-attachment: fixed;background-repeat: no-repeat 在网页上实现类似的效果,但我无法使用轮播。

我的代码如下。我尝试将背景大小、附件和封面应用于以下类:.carousel.carousel-inner.carousel-itemimg 在轮播中。

<div id="officeCarousel" class="carousel slide" data-ride="carousel" style="width:100%;">
    <ol class="carousel-indicators">
      <li data-target="#officeCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#officeCarousel" data-slide-to="1"></li>
    </ol>
    <div class="carousel-inner" role="listbox">
      <div class="carousel-item active">
        <img class="d-block img-fluid" src="office1.png" alt="First slide">
        <div class="carousel-caption d-none d-md-block">
          <div class="card card-inverse card-success mb-3 text-center">
            <div class="card-block">
              <h4 class="card-title">Office 1</h4>
              <p class="card-text">Work from the heart of the newly renovated area.</p>
                <a href="#" class="card-link">About this office</a>
<a href="#" class="card-link">Book this office</a>
            </div>
          </div>
        </div>
      </div>
      <div class="carousel-item">
        <img class="d-block img-fluid" src="office2.jpg" alt="Second slide">
        <div class="carousel-caption d-none d-md-block">
          <div class="card card-inverse card-success mb-3 text-center">
            <div class="card-block">
              <h4 class="card-title">Office 2</h4>
              <p class="card-text">Work from this inner-city rural village.</p>
                <a href="#" class="card-link">About this office</a>
<a href="#" class="card-link">Book this office</a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <a class="carousel-control-prev" href="#officeCarousel" 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="#officeCarousel" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>

非常感谢任何帮助!

编辑:下面相关部分的 CSS。这是默认的 V4 Bootstrap CSS。

.carousel {
  position: relative;
}

.carousel-inner {
  position: relative;
  width: 100%;
  overflow: hidden
}

.carousel-item {
  position: relative;
  display: none;
  width: 100%
}

【问题讨论】:

  • 也许你也可以包含 CSS?
  • 确保轮播不在容器中,因为它有一个最大宽度。
  • ZimSystem,绝对!我现在将编辑我的原始问题以包含 CSS。

标签: css carousel bootstrap-4


【解决方案1】:

这里有一个很好的答案,包括工作演示: https://startbootstrap.com/template-overviews/full-slider/

将以下内容放入 HTML 页面(您需要下载 Bootstrap 4 或将链接替换为 CDN):

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Full Slider - Start Bootstrap Template</title>

    <!-- Bootstrap core CSS -->
    <link href="../Content/css/bootstrap.min.css" rel="stylesheet" />

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="../Scripts/bootstrap.bundle.min.js"></script>
</head>
<body>

<style>
    .carousel-item {
    height: 100%;
    min-height: 300px;
    background: no-repeat center center scroll;
    background-size: cover;
    }
</style>

<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('http://placehold.it/1900x1080')">
            <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('http://placehold.it/1900x1080')">
            <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('http://placehold.it/1900x1080')">
            <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>


</body>
</html>

【讨论】:

  • 提供的链接已过时。
  • 对我不起作用,但我的默认 Bootstrap Carousel HTML 看起来有点不同。
猜你喜欢
  • 2018-04-17
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多