Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically align image inside a bootstrap carousel

I'm trying to code my first website and I have to build a carousel. The thing is I have a smaller image (400x400) that I'm trying to vertically align it into the carousel and some text to the right. I tried with the position: relative; on the carousel and absolute on the div with the image +

top: 50%;
transform: translateY(-50%);

doesn't work. I think because of the carousel caption that is next to it..

Here's what is looking right now without the aligning I mentioned above

http://i.imgur.com/LqoXWMo.png

Here's the HTML:

<!-- Start Carousel -->
    <div id="product-detail carousel-example-generic" class="carousel slide" data-ride="carousel">
      <!-- Wrapper for slides -->
      <div class="carousel-inner" role="listbox">
        <div class="item active">
          <div class="container">
            <div class="carousel-img"></div>
            <div class="carousel-caption">
              <p class="cat-title">Mercury</p>
            </div>
          </div>
        </div>
        <div class="item">
          <img src="..." 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 control-icon" 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 control-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>

<!-- End Carousel -->

CSS I have for the carousel:

/*************************
    CAROUSEL
*************************/

.carousel .carousel-inner {
    height: 550px;
    margin-bottom: 100px;
    background: #faf9f9;
}

.carousel-control.right,
.carousel-control.left {
    background-image: none;
}

.control-icon {
    color: #393939;
    text-shadow: none;
}

.carousel-inner {
    position: relative;
}

.carousel-img {
    width: 400px;
    height: 400px;
    background-image: url('../img/jewelery.jpg');
    background-size: cover;
}

.cat-title {
    text-shadow: none;
}
like image 380
Ovidiu G Avatar asked Oct 20 '22 02:10

Ovidiu G


1 Answers

Try this and see if it helps:

First make you .item position relative and give it a height of 100% so it takes up the same height as the container .carousel-inner

.item{
    position: relative;
    height:100%;
}

Then next add you absolute position styles back to your .carousel-img element

.carousel-img {
    ...
    position: absolute;   
    top: 50%;
    transform: translateY(-50%);
}

See this fiddle for an example.

Also I noticed that you have static heights on your carousel (550px) and on your carousel-img (400px) so you could just add margin-top:75px; to your .carousel-img

See this fiddle.

like image 50
zgood Avatar answered Oct 21 '22 22:10

zgood