如何使 Bootstrap 轮播标题拉伸轮播的整个宽度?

问题描述 投票:0回答:3

我有一个与页面全宽的轮播。现在我需要将轮播标题设置为页面的整个宽度以及黄色背景。背景应该是页面的整个宽度,但实际的标题文本应该居中。

它应该是这样的:

This is what it should look like.

这就是现在的样子:

This is what it looks like now.

HTML:

<!--START CAROUSEL-->
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="false">
  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="../../images/LD/desktop_hero1.png" alt="...">
      <div class="carousel-caption">
        Text for caption 1 here.
      </div>
    </div>
    <div class="item">
      <img src="../../images/LD/desktop_hero2.png" alt="...">
      <div class="carousel-caption">
        Text for caption 2 here.
      </div>
    </div>
    <div class="item">
      <img src="../../images/LD/desktop_hero3.png" alt="...">
      <div class="carousel-caption">
        Text for caption 3 here.
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>       
<!--END CAROUSEL-->

CSS:

<style type="text/css">
    .carousel-caption {
     max-width: 100%;
     width:100%;
     background-color: #ffb81c;
    }
</style>
html css twitter-bootstrap carousel caption
3个回答
5
投票

carousel-caption
类声明 div 具有绝对定位,其位置距左侧 15%。

所以你可以尝试覆盖它。这是CSS:

.carousel-caption {
    max-width: 100%;
    width:100%;
    background-color: #ffb81c;
    left: 0;
}

0
投票

你只需要添加 text-align: center 因为实际上它已经使用了全宽

如果有东西浮动,你可以使用clearfix来解决它,检查这个jsfidlle

标题示例

<div class="carousel-caption">
    <h1>
    Text for caption 1 here.
    </h1>
  </div>


.carousel-caption h1 {
 max-width: 100%;
 background-color: #ffb81c;
 text-align:center;
}

pd。我添加了 h1 标签以获得更好的语义结构。


0
投票

将以下类别添加到.сarousel-caption,它应该可以工作。

<style>
.carousel-caption {
    text-align: center;
    width: 100vw; /* Fill the entire width of the visible area of the browser */
    left: 50%; /* Align to center the page */
    transform: translateX(-50%); /* Centered horizontally */
}
</style>

© www.soinside.com 2019 - 2024. All rights reserved.