简体   繁体   English

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

[英]Bootstrap: How to center content vertically within a column

I have spent hours on this problem now and have read through countless questions, but none of the provided solutions works for me, so I'm now just gonna post my specific problem: 我现在花了几个小时来解决这个问题并阅读了无数的问题,但是所提供的解决方案都没有适用于我,所以我现在要发布我的具体问题:

I want to build a row with four columns. 我想构建一个包含四列的行。 The first three columns have pictures in them and the fourth column has a text description that I want to show vertically centered (meaning with an equal margin on the top and bottom that is obviously not fixed, but responsive to changing screen-sizes). 前三列中有图片,第四列有文字描述,我想垂直居中显示(意思是顶部和底部有相同的边距,显然不是固定的,但是响应于改变的屏幕尺寸)。 Here's the code: 这是代码:

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

It must have to do something with the Bootstrap specific classes that non of the CSS-based solutions I can find on this works. 它必须对Bootstrap特定的类做一些事情,而不是基于CSS的解决方案,我可以在这个上找到它。 How can I make it work in this particular example? 如何在这个特定的例子中使它工作? Feel free to make any changes you want on this part: 您可以随意对此部分进行任何更改:

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

However the rest of the structure should remain unchanged if possible (including the col-lg-1). 但是,如果可能,结构的其余部分应保持不变(包括col-lg-1)。

Your help is greatly appreciated! 非常感谢您的帮助!

You can use flexbox. 您可以使用flexbox。 Make your outer div , display: flex and use The property align-items to make it's contents vertically center. 制作外部divdisplay: flex并使用属性align-items使其内容垂直居中。 Here is the code: 这是代码:

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

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

The following code will center items perfectly within a div: 以下代码将项目完美地放在div中:

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

All you need to do is adding below snippet to your CSS. 您需要做的就是在CSS下面添加以下代码段。

just like this. 像这样。

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

I tried the same code and got output too. 我尝试了相同的代码并获得了输出。 if you want to see, download the file and run it. 如果你想看,请下载文件并运行它。

https://www.dropbox.com/s/0x7iqi6alayd8xg/VerticallyCentered.html?dl=0 https://www.dropbox.com/s/0x7iqi6alayd8xg/VerticallyCentered.html?dl=0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM