如何在 Bootstrap 4 轮播中制作全角背景图像?

Posted

技术标签:

【中文标题】如何在 Bootstrap 4 轮播中制作全角背景图像?【英文标题】:How to make background image in Bootstrap 4 carousel full-width? 【发布时间】: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" >
        <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" >
        <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。 【参考方案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 看起来有点不同。

以上是关于如何在 Bootstrap 4 轮播中制作全角背景图像?的主要内容,如果未能解决你的问题,请参考以下文章

使用图像指示器更改 Bootstrap 轮播中的不透明度

bootstrap 获得轮播中的索引 getActiveIndex

在 Bootstrap 轮播中拉伸和填充图像

css Bootstrap轮播中的渐变过渡

我正在尝试将 Vue 数据实现到 Bootstrap 轮播中

如何从 vuetify 轮播中向上移动箭头