【问题标题】:How can I remove the gap above my carousel?如何消除旋转木马上方的间隙?
【发布时间】:2016-08-11 13:05:30
【问题描述】:

我使用的是 Bootstrap 3.3.6,但无法移除轮播上方的间隙。我尝试在轮播基类中添加margin-top: -20px;,但没有运气:(

这是我的代码。

HTML:

  <!-- Carousel
================================================== -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img class="first-slide" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="First slide">
      <div class="container">
        <div class="carousel-caption">
          <h1>Example headline.</h1>
          <p>Note: If you're viewing this page via a <code>file://</code> URL, the "next" and "previous" Glyphicon buttons on the left and right might not load/display properly due to web browser security rules.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Sign up today</a></p>
        </div>
      </div>
    </div>
    <div class="item">
      <img class="second-slide" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Second slide">
      <div class="container">
        <div class="carousel-caption">
          <h1>Another example headline.</h1>
          <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Learn more</a></p>
        </div>
      </div>
    </div>
    <div class="item">
      <img class="third-slide" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Third slide">
      <div class="container">
        <div class="carousel-caption">
          <h1>One more for good measure.</h1>
          <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Browse gallery</a></p>
        </div>
      </div>
    </div>
  </div>
  <a class="left carousel-control" href="#myCarousel" 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="#myCarousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>



       <span class="sr-only">Next</span>
      </a>
    </div><!-- /.carousel -->

CSS:

 /* Carousel base class */
.carousel {
  height: 50px;
    top: -20px; 
    width:100%; 
    margin: 0px auto;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
  z-index: 10;
}

/* Declare heights because of positioning of img element */
.carousel .item {
  height: 500px;
  background-color: #777;
}
.carousel-inner > .item > img {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  height: 500px;
}

/* RESPONSIVE CSS
-------------------------------------------------- */

@media (min-width: 768px) {
  /* Navbar positioning foo */

  .navbar-wrapper .container {
    padding-right: 15px;
    padding-left: 15px;
  }
  .navbar-wrapper .navbar {
    padding-right: 0;
    padding-left: 0;
  }

  /* The navbar becomes detached from the top, so we round the corners */
  .navbar-wrapper .navbar {
    border-radius: 4px;
  }

  /* Bump up size of carousel content */
  .carousel-caption p {
    margin-bottom: 20px;
    font-size: 21px;
    line-height: 1.4;
  }

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

我曾尝试在 .carousel 等类中添加 margin-top: -20px;,但没有成功:(

【问题讨论】:

  • 这个轮播顶部没有空隙:bootply.com/1LnaP2xA1Z - 你的代码中一定有什么东西导致了它。如果您检查元素,是否有任何其他样式应用于它或它上面的元素?
  • 谢谢你,我检查了我的主页,我的导航栏由于某种原因有一个边距底部。谢谢:D

标签: html css twitter-bootstrap carousel


【解决方案1】:

尝试添加:

body {
  padding-top: 70px;
}

【讨论】:

  • 刚刚在我的导航栏上方添加了 70px :(
  • 这将如何解决轮播上方的空白?最多只会增加差距
【解决方案2】:

根据您的问题,您说您添加了margin-top:-20px,但您的示例代码说您添加了top:-20px;当我在jsfiddle 中运行代码时,顶部没有任何空间。

但是,我想尝试并提供帮助,所以我会说不要直接更改 .carousel 类,而是定位 #myCarousel 并在那里创建您的 top: -20px。如果引导带在容器以外的其他地方使用.carousel,我会提出此建议。

但是,这可能会改变页面其余部分的处理方式,因此我建议查看包含 #myCarousel 的元素,因为小提琴显示没有问题。希望这会有所帮助!

【讨论】:

    【解决方案3】:

    我认为由于导航栏而出现差距,因此您应该尝试以下代码。


    .navbar{
        margin-bottom: 0px!important;
    }
    

    我希望它有效...!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2014-02-01
      • 2021-04-09
      • 1970-01-01
      • 2021-01-27
      • 2012-12-24
      • 1970-01-01
      相关资源
      最近更新 更多