如何将旋转木马内的内容垂直居中?

时间:2014-02-03 10:41:12

标签: javascript jquery html css twitter-bootstrap

我所指的旋转木马是Bootstrap的旋转木马(http://getbootstrap.com/examples/carousel/)。

我正在使用示例中的类似代码,但我无法将内容放在父级中间。

My Carousel

如您所见,内容不居中。我已经尝试设置轮播标题{vertical-align:middle;},但没有任何变化。

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

这是示例中的相同代码。我怎样才能将旋转木马标题垂直居中?

3 个答案:

答案 0 :(得分:4)

&#13;
&#13;
.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%;
}
&#13;
<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>
&#13;
&#13;
&#13;

enter image description here

上图是演示的截图。

  1. 图片响应
  2. 图片
  3. 中的标题文字垂直和水平居中

答案 1 :(得分:1)

用于IE8及以上版本

.carousel-caption{
  display:table-cell;
  vertical-align:middle;
}

答案 2 :(得分:0)

最简单的方法是使用display:inline-blockline-height的组合,例如。

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

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

请参阅此处的示例:http://jsfiddle.net/bQs26/