3

The carousel I am referring to is Bootstrap's carousel (http://getbootstrap.com/examples/carousel/).

I am using the similar code from the example, but I can't place the contents right middle of the parent.

My Carousel

As you can see, the contents are not centered. I've tried setting the carousel-caption{ vertical-align:middle;}, but nothing changes.

<div class="carousel-inner">
    <div class="item active">
        <div class="container">
            <div class="carousel-caption">
                !!contents!!
            </div>
        </div>
    </div>
</div>

This is the same code from the example. How can I vertically center the carousel-caption?

Jee Seok Yoon
  • 4,716
  • 9
  • 32
  • 47

3 Answers3

4

.full-width {
    width: 100%;
}

.carousel-caption {
    position: absolute;
    top: 0;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -moz-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    height: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<div id="carousel-example" class="carousel slide" data-ride="carousel">
    
    <!-- Indicators -->
    <ol class="carousel-indicators">
        <li data-target="#carousel-example" data-slide-to="0" class="active"></li>
        <li data-target="#carousel-example" data-slide-to="1"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        <div class="item active">
            <img src="http://placehold.it/300x200" class="full-width" />
            <div class="carousel-caption">
                <div class="full-width text-center">
                    <p>
                        Some text which is vertically, horizontally centered in image
                    </p>
                </div>
            </div>
        </div>
        <div class="item">
            <img src="http://placehold.it/300x200" class="full-width" />
            <div class="carousel-caption">
                <div class="full-width text-center">
                    Some text which is vertically, horizontally centered in image
                </div>
            </div>
        </div>
    </div>
    
    <!-- Controls -->
    <a class="left carousel-control" href="#carousel-example" 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" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
    
</div>

enter image description here

The above image is a screenshot of demo.

  1. The image is responsive
  2. Caption text is vertically and horizontally centered in the image
TylerH
  • 20,799
  • 66
  • 75
  • 101
Chemical Programmer
  • 4,352
  • 4
  • 37
  • 51
  • You should follow the rules instead of circumventing them. Put your code in the answer next time. – TylerH Dec 27 '16 at 18:03
1

for IE8 and above

.carousel-caption{
  display:table-cell;
  vertical-align:middle;
}
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
0

the easiest way is to use a combination of display:inline-block and line-height, eg.

.container {
    line-height: 100px // height of your carousel
}

.carousel-caption {
    line-height: normal; // restore original line height here
    display: inline-block;
    vertical-align: middle;
}

see example here: http://jsfiddle.net/bQs26/

pwolaq
  • 6,343
  • 19
  • 45