【问题标题】:How to make contents inside carousel vertically center? [duplicate]如何使旋转木马内的内容垂直居中? [复制]
【发布时间】:2016-12-28 02:04:44
【问题描述】:

我指的轮播是Bootstrap的轮播(http://getbootstrap.com/examples/carousel/)。

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

如您所见,内容没有居中。我尝试设置 carousel-caption{ vertical-align:middle;},但没有任何变化。

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

这与示例中的代码相同。如何将轮播标题垂直居中?

【问题讨论】:

  • 试试 {text-align : center}
  • 对不起,我的错..

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

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

上图为demo截图。

  1. 图像响应式
  2. 标题文本在图像中垂直和水平居中

【讨论】:

  • 你应该遵守规则而不是规避它们。下次把你的代码放在答案中。
【解决方案2】:

适用于 IE8 及更高版本

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

【讨论】:

    【解决方案3】:

    最简单的方法是使用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/

    【讨论】:

    • 不适用于图像,或者必须知道它的确切高度。
    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2022-01-08
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多