调整轮播高度到图像高度(响应)

时间:2014-08-06 11:01:02

标签: html css image carousel

我正在使用bootstrap caroussel模板。 共有4张图片,全部四张图片均为1330 * 500px。默认情况下,图像会在窗口大小调整时拉伸。我设法保持原始图像比率与CSS宽度:100%,高度:图像的自动设置。问题是旋转木马容器不会调整(我看到灰色间隙代替)...如何调整转盘高度到图像高度(尝试调整旋转木马高度%或使用填充底部%,但没有'工作)? 感谢。

html:

         <div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
    <!-- 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>
        <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>
    <div class="carousel-inner">
        <div class="item active">
            <img src="#" alt="First slide">

            <div class="container">
                <div class="carousel-caption">
                    <img src="#">

                </div>
            </div>
        </div>

的CSS:

/* Carousel base class */

.carousel
{
height: 500px;
margin-bottom: 10px;

}

/* Since positioning the image, we need to help out the caption */

.carousel-caption
{
}

/* Declare heights because of positioning of img element */

.carousel .item
{
height: 500px;
background-color: #777;
margin-top: 0px;
}


.carousel-inner > .item > img
{
position: relative;
top: 0;
left: 0;
min-width: 100%;
height: auto;
}

1 个答案:

答案 0 :(得分:1)

通过覆盖bootstrap的css并将高度:500px添加到轮播类,你似乎打破了滑块。我在bootply中创建了一个简单的旋转木马并且遗漏了所有的Css。现在一切都按预期工作,高度正确调整,没有任何灰色间隙。我还添加了Bootstrap的容器,行和列div。我不知道你是否设置了它们,但我建议你这样做。我使用了您指定的相同尺寸的图像。

这是BOOTPLY EXAMPLE。点击右上方的桌面和移动图标,查看它在不同分辨率下的外观。

希望这有帮助

HTML:

<div class="container">
    <div class="row">
        <div class="col-lg-12">
            <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                  <!-- Indicators -->
                  <ol class="carousel-indicators">
                    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
                    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
                    <li data-target="#carousel-example-generic" data-slide-to="3"></li>
                  </ol>

                  <!-- Wrapper for slides -->
                  <div class="carousel-inner">
                    <div class="item active">
                      <img src="images/img-1.jpg" alt="...">
                      <div class="carousel-caption">
                        ...
                      </div>
                    </div>
                    <div class="item">
                      <img src="images/img-2.jpg" alt="...">
                      <div class="carousel-caption">
                        ...
                      </div>
                    </div>

                    <div class="item">
                      <img src="images/img-3.jpg" alt="...">
                      <div class="carousel-caption">
                        ...
                      </div>
                    </div>

                    <div class="item">
                      <img src="images/img-4.jpg" alt="...">
                      <div class="carousel-caption">
                        ...
                      </div>
                    </div>

                  </div>

                  <!-- Controls -->
                  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left"></span>
                  </a>
                  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                  </a>
            </div>
        </div>
    </div>
</div>