Bootstrap:如何在列中垂直居中内容

cmssoen2  于 9个月前  发布在  Bootstrap
关注(0)|答案(4)|浏览(93)

我已经花了几个小时在这个问题上,现在已经阅读了无数的问题,但没有一个提供的解决方案适合我,所以我现在只是要发布我的具体问题:
我想构建一个有四列的行。前三列有图片,第四列有一个文本描述,我想垂直居中显示(这意味着顶部和底部的边距相等,显然不是固定的,但可以响应屏幕大小的变化)。下面是代码:

<section>
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-4 col-sm-6">
                <img ...>
            </div>
            <div class="col-lg-4 col-sm-6">
                <img ...>
            </div>
            <div class="col-lg-3 col-sm-6">
                <img ...>
            </div>
            <div class="col-lg-1 col-sm-6">
                <div class="container-fluid">
                    <p>Some text that is supposed to be vertically centered within the column</p>
                </div>
            </div>
        </div>
    </div>
</section>

字符串
它必须与Bootstrap特定的类做一些事情,我可以找到非基于CSS的解决方案。我如何使它在这个特定的例子中工作?请随意对这部分进行任何更改:

<div class="container-fluid">
    <p>Some text that is supposed to be vertically centered within the column</p>
</div>


但是,如果可能的话,其余结构应保持不变(包括col-lg-1)。
非常感谢您的帮助!

sigwle7e

sigwle7e1#

在Bootstrap 4中,向col添加“self-center”-class,您希望内容垂直居中。

tktrz96b

tktrz96b2#

你可以使用flexbox。让你的外部divdisplay: flex和使用属性align-items使它的内容垂直居中。下面是代码:

#outerDiv{
    display: flex;
    align-items: center;
    flex-wrap: wrap;
}

字符串

eh57zj3b

eh57zj3b3#

所有你需要做的就是添加下面的代码段到你的CSS。
就像这样。

.row{ display: flex; align-items: center;}

字符串
我尝试了同样的代码,也得到了输出。如果你想看,下载文件并运行它。
https://www.dropbox.com/s/0x7iqi6alayd8xg/VerticallyCentered.html?dl=0

t40tm48m

t40tm48m4#

下面的代码将完美地将项目集中在div中:

display: -webkit-flex; /* Safari */
-webkit-justify-content: space-around; /* Safari 6.1+ */
display: flex;
justify-content: space-around;
justify-content: center;

字符串

相关问题