lauWM
lauWM

Reputation: 749

adjust carousel height to image height (responsive)

I am using the bootstrap caroussel template. There is 4 images, all four in 1330*500px. By default image stretches on window resizing. I managed to keep the original image ratio with a CSS width:100% , height:auto setting for the image. Problem is carousel container won't adjust (and I see a gray gap instead)... How can I adjust carrousel height to image height within (tried to adjust carousel height in % or using padding-bottom in % too, but doesn't work) ? Thanks.

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;
}

Upvotes: 0

Views: 4803

Answers (1)

IndieRok
IndieRok

Reputation: 1788

By overwriting bootstrap's css and adding height:500px to the carousel class, you seem to be be breaking the slider. I created a simple carousel in bootply and left out all of your Css. And now everything works as expected and the height adjust itself correctly without any grey gap. I also added Bootstrap's container, row and column divs. I don't know if you had them setup, but I recommend you do. I used images with the same dimensions you specified.

Here is the BOOTPLY EXAMPLE. Click on the desktop and mobile icon situed on the top-right to see how it will look on different resolution.

Hope this helps

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>

Upvotes: 1

Related Questions