简体   繁体   English

如何设置第一行垂直对齐中心

[英]How can I set my first row vertical align to center

I use align-items-center class in row section but it doesn't work: 我在行部分中使用align-items-center类,但是它不起作用:

 <div class="container"> <div class="row align-items-center"> <div class="col-12 bg-warning"> hello </div> </div> </div> 

I want that line be vertical centered on page as I show in the picture: 我希望该线垂直于页面居中,如图所示:

在此处输入图片说明

Your code is working fine but you have to set a height . 您的代码工作正常,但必须设置一个height At the moment the .container and .row took the height of the content. 目前, .container.row占用了内容的高度。 So you have to set a height to the .row and your .col should be vertical centered: 因此,您必须将.row设置为height ,并且.col应该垂直居中:

 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div class="row align-items-center" style="height:500px;"> <div class="col-12 bg-warning"> hello </div> </div> </div> 

To set vertical center on the visible area of your site, you can use height:100vh; 要在网站的可见区域设置垂直中心,可以使用height:100vh; instead of the fixed size. 而不是固定大小。

You need to make the page occupy 100% high. 您需要使页面占据100%高。

html, body, .container, .row {
  height: 100%;
}

Try this: https://jsfiddle.net/q1ypnzg8/2/ 试试这个: https : //jsfiddle.net/q1ypnzg8/2/

<div class="container divouter">
    <div class="row align-items-center divmiddle">
        <div class="col-12 bg-warning divinner">
            hello
        </div>
    </div>
</div>

CSS CSS

.divouter {
    display: table;
    position: absolute;
    height: 100%;
    width: 100%;
}

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

.divinner {
    margin-left: auto;
    margin-right: auto; 
    text-align: center;
}

CSS has limitations when it comes to putting a text there at the center. CSS在将文本放在中间时有局限性。 For browser compatibility, I'd advice you to use inline alignment property. 为了与浏览器兼容,建议您使用内联对齐属性。

<div align="center">Content goes here.</div>

Other properties you can use in case of CSS include: 在CSS情况下可以使用的其他属性包括:

#divID{ margin:auto text-align: center; #divID{ margin:auto text-align: center;

}

Though, I'd prefer the margin auto property since the text align may fail in certain browsers for certain tags. 不过,我宁愿使用margin auto属性,因为在某些浏览器中某些标签的文本对齐可能会失败。

You can add this css for .row.align-items-center 您可以为.row.align-items-center添加此CSS

 .row.align-items-center { position: absolute; transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); top: 50%; left: 50%; } 

Hope this will help. 希望这会有所帮助。

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

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