微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何使旋转木马内的内容垂直居中?

参见英文答案 > How to vertically center a Bootstrap carousel-caption?                                    1个
我指的旋转木马是Bootstrap的旋转木马(http://getbootstrap.com/examples/carousel/).

我正在使用示例中的类似代码,但我不能将内容放在父级的中间.

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

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

这是示例中的相同代码.如何将旋转木马标题垂直居中?

解决方法:

.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>

上图是演示的截图.

>图像响应
>标题文本在图像中垂直和水平居中

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐