Bootstrap 4
包含flexbox,因此垂直居中的方法要容易得多,并且不需要额外的CSS我正在寻找一种方法,可以将container
div在jumbotron
内垂直居中,并将其设置在页面中间。
.jumbotron
必须适应屏幕的整个高度和宽度。.container
div的宽度为1025px
,应位于页面中间(垂直居中)。
我想让此页面的大屏幕适应屏幕的高度和宽度,并使容器垂直于大屏幕垂直居中。如何实现?
.jumbotron {
height:100%;
width:100%;
}
.container {
width:1025px;
}
.jumbotron .container {
max-width: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="jumbotron">
<div class="container text-center">
<h1>The easiest and powerful way</h1>
<div class="row">
<div class="col-md-7">
<div class="top-bg"></div>
</div>
<div class="col-md-5 iPhone-features" style="margin-left:-25px;">
<ul class="top-features">
<li>
<span><i class="fa fa-random simple_bg top-features-bg"></i></span>
<p><strong>Redirect</strong><br>Visitors where they converts more.</p>
</li>
<li>
<span><i class="fa fa-cogs simple_bg top-features-bg"></i></span>
<p><strong>Track</strong><br>Views, Clicks and Conversions.</p>
</li>
<li>
<span><i class="fa fa-check simple_bg top-features-bg"></i></span>
<p><strong>Check</strong><br>Constantly the status of your links.</p>
</li>
<li>
<span><i class="fa fa-users simple_bg top-features-bg"></i></span>
<p><strong>Collaborate</strong><br>With Customers, Partners and Co-Workers.</p>
</li>
<a href="pricing-and-signup.html" class="btn-primary btn h2 lightBlue get-Started-btn">GET STARTED</a>
<h6 class="get-Started-sub-btn">FREE VERSION AVAILABLE!</h6>
</ul>
</div>
</div>
</div>
</div>
垂直对齐现在通过使用Flexible box layout非常简单。如今,除Internet Explorer 8和9之外,Web浏览器的wide range中都支持此方法。因此,对于IE8 / 9,我们需要使用一些hacks / polyfills或different approaches。
下面,我将仅显示文本((与旧的flexbox语法无关)。)中的3行文本。
注:
最好使用其他类,而不是更改.jumbotron
以实现垂直对齐。我将使用vertical-center
类名作为实例。 (jsbin上的A Mirror)>。<div class="jumbotron vertical-center"> <!--
^--- Added class -->
<div class="container">
...
</div>
</div>
注释(在演示中考虑):.vertical-center { min-height: 100%; /* Fallback for browsers do NOT support vh unit */ min-height: 100vh; /* These two lines are counted as one :-) */ display: flex; align-items: center; }
重要
height
或min-height
属性的值相对于父元素的height
,因此您应明确指定父元素的height
。
(在我已经测试过)
.vertical-center
-不会占用父级内部的可用空间,因此我们需要指定width
属性,例如:width: 100%
。.container
-可能不会水平出现在中央。似乎margin
的左/右auto
对弹性项目没有任何影响。因此,我们需要通过box-pack / justify-content
对其进行对齐。有关列的更多详细信息和/或垂直对齐,您可以参考下面的主题:
vertical-align: middle
声明将内联级别元素垂直对齐到中间。来自vertical-align: middle
的规范:将框的垂直中点与父框的基线对齐,再加上父框x高度的一半。[如果父元素-在这种情况下为W3C元素-具有明确的中间
.vertical-center
,如果我们可以拥有一个与父元素完全相同的height
子元素,则我们可以[C0 ]到全高子项的中点,并且令人惊讶地使我们所需的流入子项height
-垂直对齐中心。]聚在一起
.container
伪元素在.vertical-center
内创建全高元素,也可以将其和其他子元素::before
的默认::after
类型更改为display
。然后使用.container
垂直对齐内联元素。
您在这里:
inline-block
vertical-align: middle;
。此外,为防止在较小的屏幕上出现意外问题,您可以将伪元素的高度重置为
<div class="jumbotron vertical-center"> <div class="container"> ... </div> </div>
.vertical-center { height:100%; width:100%; text-align: center; /* align the inline(-block) elements horizontally */ font: 0/0 a; /* remove the gap between inline(-block) elements */ } .vertical-center:before { /* create a full-height inline block pseudo=element */ content: " "; display: inline-block; vertical-align: middle; /* vertical alignment of the inline element */ height: 100%; } .vertical-center > .container { max-width: 100%; display: inline-block; vertical-align: middle; /* vertical alignment of the inline element */ /* reset the font property */ font: 16px/1 "Helvetica Neue", Helvetica, Arial, sans-serif; }
或WORKING DEMO,或者在需要时将其auto
类型更改为0
:
display
并添加更高的还有另一件事:
none
如果容器周围有
@media (max-width: 768px) { .vertical-center:before { height: auto; /* Or */ display: none; } }
/ UPDATED DEMO节,最好将元素正确放置(
footer
,header
?由您决定。]
relative
值(为保证起见),以使其始终位于其他人的顶部。 Bootstrap 4
包含flexbox,因此垂直居中的方法要容易得多,并且不需要额外的CSSabsolute
和z-index
实用程序类。d-flex
align-items-center
重要:垂直居中是相对于高度
。您要居中必须的项目的父容器的已定义高度。如果需要页面的高度,请在父页面上使用<div class="jumbotron d-flex align-items-center">
<div class="container">
content
</div>
</div>
或http://www.codeply.com/go/ui6ABmMTLv!例如:vh-100
添加Bootstrap.css,然后将其添加到您的CSS,请使用bootstrap-4类:min-vh-100
<div class="jumbotron d-flex align-items-center min-vh-100"> <div class="container text-center"> I am centered vertically </div> </div>
在要
html, body{height:100%; margin:0;padding:0} .container-fluid{ height:100%; display:table; width: 100%; padding: 0; } .row-fluid {height: 100%; display:table-cell; vertical-align: middle;} .centering { float:none; margin:0 auto; }
:将孩子水平居中
Now call in your page <div class="container-fluid"> <div class="row-fluid"> <div class="centering text-center"> Am in the Center Now :-) </div> </div> </div>
要,请使用bootstrap-4类:Bootstrap 4将孩子垂直居中
但请记住不要忘记将类与这些这是bootstrap-4实用程序类,像这样d-flex
justify-content-center
如果此代码不起作用,请确保添加bootstrap-4实用程序我知道这不是此问题的直接答案,但可能会对某人有所帮助注意:
align-items-center
演示
<div class="d-flex justify-content-center align-items-center" style="height:100px;">
<span class="bg-primary">MIDDLE</span>
</div>
body {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.jumbotron {
display: table-cell;
vertical-align: middle;
}
另请参阅body {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.jumbotron {
display: table-cell;
vertical-align: middle;
}
!
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div class="jumbotron vertical-center">
<div class="container text-center">
<h1>The easiest and powerful way</h1>
<div class="row">
<div class="col-md-7">
<div class="top-bg">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
<div class="col-md-5 iPhone-features">
<ul class="top-features">
<li>
<span><i class="fa fa-random simple_bg top-features-bg"></i></span>
<p><strong>Redirect</strong><br>Visitors where they converts more.</p>
</li>
<li>
<span><i class="fa fa-cogs simple_bg top-features-bg"></i></span>
<p><strong>Track</strong><br>Views, Clicks and Conversions.</p>
</li>
<li>
<span><i class="fa fa-check simple_bg top-features-bg"></i></span>
<p><strong>Check</strong><br>Constantly the status of your links.</p>
</li>
<li>
<span><i class="fa fa-users simple_bg top-features-bg"></i></span>
<p><strong>Collaborate</strong><br>With Customers, Partners and Co-Workers.</p>
</li>
<a href="pricing-and-signup.html" class="btn-primary btn h2 lightBlue get-Started-btn">GET STARTED</a>
<h6 class="get-Started-sub-btn">FREE VERSION AVAILABLE!</h6>
</ul>
</div>
</div>
</div>
</div>
this Fiddle
在.parent-container {
position: relative;
height:100%;
width: 100%;
}
.child-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
找到
<div class="parent-container">
<div class="child-container">
<h2>Header Text</h2>
<span>Some Text</span>
</div>
</div>
几个项目的垂直中心d-flex
flex-column
justify-content-center
居中style ='height:300px;'
必须具有用于计算中心或使用h-100 class的设置点然后是水平中心div d-flex对齐内容中心
和一些容器所以我们有3个标签的层次结构:div-column-> div-row-> div-containerhttps://css-tricks.com/centering-css-complete-guide/
如果使用的是Bootstrap 4,则只需添加2个div:bootstrap4<div class="d-flex flex-column justify-content-center bg-secondary" style="height: 300px;"> <div class="d-flex justify-content-center"> <div class=bg-primary>Flex item</div> </div> <div class="d-flex justify-content-center"> <div class=bg-primary>Flex item</div> </div> </div>
类:d-table,d-table-cell,w-100,h-100和align-middle将完成工作这是我用于垂直对齐内容的方法-顶部/中央/底部具有自举3行。引导3个具有相同高度并垂直对齐的列。.jumbotron{ height:100%; width:100%; }
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <body> <div class="jumbotron"> <div class="d-table w-100 h-100"> <div class="d-table-cell w-100 h-100 align-middle"> <h5> your stuff... </h5> <div class="container"> <div class="row"> <div class="col-12"> <p> More stuff... </p> </div> </div> </div> </div> </div> </div> </body>
这里是/* columns of same height styles */ .row-full-height { height: 100%; } .col-full-height { height: 100%; vertical-align: middle; } .row-same-height { display: table; width: 100%; /* fix overflow */ table-layout: fixed; } .col-xs-height { display: table-cell; float: none !important; } @media (min-width: 768px) { .col-sm-height { display: table-cell; float: none !important; } } @media (min-width: 992px) { .col-md-height { display: table-cell; float: none !important; } } @media (min-width: 1200px) { .col-lg-height { display: table-cell; float: none !important; } } /* vertical alignment styles */ .col-top { vertical-align: top; } .col-middle { vertical-align: middle; } .col-bottom { vertical-align: bottom;
演示。
Bootstrap 4
包含flexbox,因此垂直居中的方法要容易得多,并且不需要额外的CSSmin-vh-100
<div class="jumbotron d-flex align-items-center min-vh-100">
<div class="container text-center">
I am centered vertically
</div>
</div>
在要
html, body{height:100%; margin:0;padding:0} .container-fluid{ height:100%; display:table; width: 100%; padding: 0; } .row-fluid {height: 100%; display:table-cell; vertical-align: middle;} .centering { float:none; margin:0 auto; }
:将孩子水平居中
html, body{height:100%; margin:0;padding:0}
.container-fluid{
height:100%;
display:table;
width: 100%;
padding: 0;
}
.row-fluid {height: 100%; display:table-cell; vertical-align: middle;}
.centering {
float:none;
margin:0 auto;
}
:
align-items-center
演示
<div class="parent-container">
<div class="child-container">
<h2>Header Text</h2>
<span>Some Text</span>
</div>
</div>
几个项目的垂直中心 <div class="d-flex flex-column justify-content-center bg-secondary"
style="height: 300px;">
<div class="d-flex justify-content-center">
<div class=bg-primary>Flex item</div>
</div>
<div class="d-flex justify-content-center">
<div class=bg-primary>Flex item</div>
</div>
</div>
类:d-table,d-table-cell,w-100,h-100和align-middle将完成工作
.jumbotron{
height:100%;
width:100%;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class="jumbotron">
<div class="d-table w-100 h-100">
<div class="d-table-cell w-100 h-100 align-middle">
<h5>
your stuff...
</h5>
<div class="container">
<div class="row">
<div class="col-12">
<p>
More stuff...
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
这里是/* columns of same height styles */
.row-full-height {
height: 100%;
}
.col-full-height {
height: 100%;
vertical-align: middle;
}
.row-same-height {
display: table;
width: 100%;
/* fix overflow */
table-layout: fixed;
}
.col-xs-height {
display: table-cell;
float: none !important;
}
@media (min-width: 768px) {
.col-sm-height {
display: table-cell;
float: none !important;
}
}
@media (min-width: 992px) {
.col-md-height {
display: table-cell;
float: none !important;
}
}
@media (min-width: 1200px) {
.col-lg-height {
display: table-cell;
float: none !important;
}
}
/* vertical alignment styles */
.col-top {
vertical-align: top;
}
.col-middle {
vertical-align: middle;
}
.col-bottom {
vertical-align: bottom;
演示。