简体   繁体   English

如何在 Bootstrap 中垂直居中容器?

[英]How to vertically center a container in Bootstrap?

I'm looking for a way to vertically center the container div inside the jumbotron and to set it in the middle of the page.我正在寻找一种方法将container div 垂直居中放置在jumbotron并将其设置在页面中间。

The .jumbotron has to be adapted to the full height and width of the screen. .jumbotron必须适应屏幕的整个高度和宽度。 The .container div has a width of 1025px and should be in the middle of the page (vertically center). .container div 的宽度为1025px ,应该位于页面的中间(垂直居中)。

I want this page to have the jumbotron adapted to the height and width of the screen along with the container vertically center to the jumbotron.我希望这个页面让大屏幕适应屏幕的高度和宽度以及容器垂直居中到大屏幕。 How can I achieve it?我怎样才能实现它?

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

The Flexible box way灵活的盒子方式

Vertical alignment is now very simple by the use of Flexible box layout .通过使用灵活的框布局,垂直对齐现在非常简单。 Nowadays, this method is supported in a wide range of web browsers except Internet Explorer 8 & 9. Therefore we'd need to use some hacks/ polyfills or different approaches for IE8/9.如今,这种方法在支持宽范围的不同之处的Internet Explorer 8&9的网络浏览器。因此我们需要使用一些黑客/ polyfills不同的方法用于IE8 / 9。

In the following I'll show you how to do that in only 3 lines of text (regardless of old flexbox syntax) .在下面,我将向您展示如何仅用3 行文本(不管旧的 flexbox 语法)来做到这一点。

Note: it's better to use an additional class instead of altering .jumbotron to achieve the vertical alignment.注意:最好使用额外的类而不是改变.jumbotron来实现垂直对齐。 I'd use vertical-center class name for instance.例如,我会使用vertical-center类名。

Example Here (A Mirror on jsbin) .实施例在此(A镜子上jsbin)。

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

Important notes (Considered in the demo) :重要说明(在演示中考虑)

  1. A percentage values of height or min-height properties is relative to the height of the parent element, therefore you should specify the height of the parent explicitly. heightmin-height属性的百分比值相对于父元素的height ,因此您应该明确指定父元素的height

  2. Vendor prefixed / old flexbox syntax omitted in the posted snippet due to brevity, but exist in the online example.由于简洁,在发布的代码段中省略了供应商前缀/旧的 flexbox 语法,但存在于在线示例中。

  3. In some of old web browsers such as Firefox 9 (in which I've tested) , the flex container - .vertical-center in this case - won't take the available space inside the parent, therefore we need to specify the width property like: width: 100% .在一些旧的 Web 浏览器中,例如 Firefox 9 (我已经测试过) ,flex 容器 - 在这种情况下为.vertical-center - 不会占用父级内部的可用空间,因此我们需要指定width属性比如: width: 100%

  4. Also in some of web browsers as mentioned above, the flex item - .container in this case - may not appear at the center horizontally.同样在上面提到的一些 Web 浏览器中,flex 项 - 在这种情况下为.container - 可能不会水平出现在中心。 It seems the applied left/right margin of auto doesn't have any effect on the flex item.似乎应用的auto左/右margin对 flex 项目没有任何影响。
    Therefore we need to align it by box-pack / justify-content .因此我们需要通过box-pack / justify-content来对齐它。

For further details and/or vertical alignment of columns, you could refer to the topic below:有关列的更多详细信息和/或垂直对齐,您可以参考以下主题:


The traditional way for legacy web browsers传统 Web 浏览器的传统方式

This is the old answer I wrote at the time I answered this question.这是我在回答这个问题时写的旧答案。 This method has been discussed here and it's supposed to work in Internet Explorer 8 and 9 as well.此方法已在此处讨论,它应该也适用于 Internet Explorer 8 和 9。 I'll explain it in short:我将简要解释一下:

In inline flow, an inline level element can be aligned vertically to the middle byvertical-align: middle declaration.在内联流中,内联级别元素可以通过vertical-align: middle声明vertical-align: middle Spec from W3C : W3C 规范

middle中间
Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.将框的垂直中点与父框的基线加上父框 x 高度的一半对齐。

In cases that the parent - .vertical-center element in this case - has an explicit height , by any chance if we could have a child element having the exact same height of the parent, we would be able to move the baseline of the parent to the midpoint of the full-height child and surprisingly make our desired in-flow child - the .container - aligned to the center vertically.如果父元素 - 在这种情况下.vertical-center元素 - 具有明确的height ,如果我们可以有一个与父元素具有完全相同height的子元素,我们将能够移动父元素的基线到全高孩子的中点,并令人惊讶地使我们想要的流入孩子 - .container - 垂直对齐中心。

Getting all together聚在一起

That being said, we could create a full-height element within the .vertical-center by ::before or ::after pseudo elements and also change the default display type of it and the other child, the .container to inline-block .话虽这么说,我们可以在中创建一个全高元.vertical-center通过::before::after伪元素,改变它的默认display的是类型和其他孩子, .containerinline-block

Then use vertical-align: middle;然后使用vertical-align: middle; to align the inline elements vertically.垂直对齐内联元素。

Here you go:干得好:

<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 .工作演示

Also, to prevent unexpected issues in extra small screens, you can reset the height of the pseudo-element to auto or 0 or change its display type to none if needed so:此外,为了防止在超小屏幕中出现意外问题,您可以将伪元素的高度重置为auto0或在需要时将其display类型更改为none ,以便:

@media (max-width: 768px) {
  .vertical-center:before {
    height: auto;
    /* Or */
    display: none;
  }
}

UPDATED DEMO更新的演示

And one more thing:还有一件事情:

If there are footer / header sections around the container, it's better to position that elements properly ( relative , absolute ? up to you.) and add a higher z-index value (for assurance) to keep them always on the top of the others.如果容器周围有footer / header部分,最好正确定位这些元素relativeabsolute ?由您决定。)并添加更高的z-index值(为了保证)以保持它们始终位于其他元素的顶部.

Update 2020 2020 年更新

Bootstrap 4 includes flexbox, so the method of vertical centering is much easier and doesn't require extra CSS . Bootstrap 4包含 flexbox,因此垂直居中的方法要容易得多,并且不需要额外的 CSS

Just use the d-flex and align-items-center utility classes..只需使用d-flexalign-items-center实用程序类..

<div class="jumbotron d-flex align-items-center">
  <div class="container">
    content
  </div>
</div>

http://www.codeply.com/go/ui6ABmMTLv http://www.codeply.com/go/ui6ABmMTLv

Important: Vertical centering is relative to height .重要提示:垂直居中是相对于 height 的 The parent container of the items you're attempting to center must have a defined height .您尝试居中的项目的父容器必须具有定义的 height If you want the height of the page use vh-100 or min-vh-100 on the parent!如果您想要页面的高度,请在父级上使用vh-100min-vh-100 For example:例如:

<div class="jumbotron d-flex align-items-center min-vh-100">
  <div class="container text-center">
    I am centered vertically
  </div>
</div>

Also see: https://stackoverflow.com/questions/42252443/vertical-align-center-in-bootstrap-4 另见:https://stackoverflow.com/questions/42252443/vertical-align-center-in-bootstrap-4

add Bootstrap.css then add this to your css添加 Bootstrap.css 然后将其添加到您的 css

 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>

In Bootstrap 4 :Bootstrap 4 中

to center the child horizontally , use bootstrap-4 class:水平居中孩子,请使用 bootstrap-4 类:

justify-content-center

to center the child vertically , use bootstrap-4 class:垂直居中孩子,请使用 bootstrap-4 类:

 align-items-center

but remember don't forget to use d-flex class with these it's a bootstrap-4 utility class, like so但请记住不要忘记使用d-flex类,它是一个 bootstrap-4 实用程序类,就像这样

<div class="d-flex justify-content-center align-items-center" style="height:100px;">
    <span class="bg-primary">MIDDLE</span>    
</div>

Note: make sure to add bootstrap-4 utilities if this code does not work注意:如果此代码不起作用,确保添加 bootstrap-4 实用程序

I know it's not the direct answer to this question but it may help someone我知道这不是这个问题的直接答案,但它可能会帮助某人

My prefered technique :我喜欢的技术:

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

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

Demo演示

 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>

See also this Fiddle !另见这个小提琴

Tested in IE, Firefox, and Chrome.在 IE、Firefox 和 Chrome 中测试。

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

Found on https://css-tricks.com/centering-css-complete-guide/https://css-tricks.com/centering-css-complete-guide/ 上找到

Give the container class给容器类

.container{
    height: 100vh;
    width: 100vw;
    display: flex;
}

Give the div that's inside the container:给出容器内的 div:

align-content: center;

All the content inside this div will show up in the middle of the page.此 div 中的所有内容都将显示在页面中间。

for bootstrap4 vertical center of few items用于bootstrap4几个项目的垂直中心

d-flex for flex rules d-flex用于 flex 规则

flex-column for vertical direction on items项目垂直方向的flex-column

justify-content-center for centering justify-content-center用于居中

style='height: 300px;'样式='高度:300px;' must have for set points where center be calc or use h-100 class必须具有中心为 calc 或使用h-100类的设置点

then for horizontal center div d-flex justify-content-center and some container然后对于水平中心div d-flex justify-content-center和一些容器

so we have hierarhy of 3 tag: div-column -> div-row -> div-container所以我们有 3 个标签的层次结构:div-column -> div-row -> div-container

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

If you're using Bootstrap 4, you just have to add 2 divs:如果您使用的是 Bootstrap 4,则只需添加 2 个 div:

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

The classes: d-table, d-table-cell, w-100, h-100 and align-middle will do the job类:d-table、d-table-cell、w-100、h-100 和 align-middle 将完成这项工作

Vertically align center with bootstrap 4将中心与 bootstrap 4 垂直对齐

Add this class: d-flex align-items-center to the element将此类: d-flex align-items-center到元素

Example例子

If you had this:如果你有这个:

<div class="col-3">

change it to this:把它改成这样:

<div class="col-3 d-flex align-items-center>

Here is the way which i am using for align content vertically - top/center/bottom with bootstrap 3 rows: 这是我用于垂直对齐内容的方式 - 顶部/中心/底部与引导3行:

Here is fiddle Demo: js fiddle 这是小提琴演示: js小提琴

Bootstrap 3 columns with same height and vertically aligned - 

 /* 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; 
 <div class="container"> <h2>Demo 1</h2> <div class="row"> <div class="row-same-height"> <div class="col-xs-3 col-xs-height">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus tincidunt porttitor. Praesent vehicula aliquam enim. Fusce non luctus eros, sit amet consequat velit. Pellentesque volutpat est et est imperdiet pharetra. Integer vestibulum feugiat malesuada. Proin a felis ut libero vestibulum fermentum ut sit amet est. Morbi placerat eget lacus sed sagittis. Nullam eu elit gravida arcu viverra facilisis. Quisque laoreet enim neque, ut consequat sem tincidunt at. Fusce lobortis scelerisque libero, eget vulputate neque. </div> <div class="col-xs-3 col-xs-height col-top">2st column</div> <div class="col-xs-3 col-xs-height col-middle">3st column</div> <div class="col-xs-3 col-xs-height col-bottom">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus tincidunt porttitor. Praesent vehicula aliquam enim. Fusce non luctus eros, sit amet consequat velit. Pellentesque volutpat est et est imperdiet pharetra.</div> </div> </div><!-- ./row --> </div><!-- ./container --> 

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

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