如何在 Bootstrap 中垂直居中容器? [英] How to vertically center a container in Bootstrap?

查看:35
本文介绍了如何在 Bootstrap 中垂直居中容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法将 container div 垂直居中 jumbotron 并将其设置在页面中间.

.jumbotron 必须适应屏幕的整个高度和宽度..container div 的宽度为 1025px 并且应该位于页面的中间(垂直居中).

我希望这个页面让大屏幕适应屏幕的高度和宽度以及容器垂直居中到大屏幕.我怎样才能实现它?

.jumbotron {高度:100%;宽度:100%;}.容器 {宽度:1025px;}.jumbotron .container {最大宽度:100%;} 

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" rel="样式表"/><div class="jumbotron"><div class="container text-center"><h1>最简单有效的方式</h1><div class="row"><div class="col-md-7"><div class="top-bg"></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>重定向</strong><br>他们转化更多的访问者.</p><li><span><i class="fa fa-cogs simple_bg top-features-bg"></i></span><p><strong>跟踪</strong><br>观看次数、点击次数和转化次数.</p><li><span><i class="fa fa-check simple_bg top-features-bg"></i></span><p><strong>检查</strong><br>不断查看链接的状态.</p><li><span><i class="fa fa-users simple_bg top-features-bg"></i></span><p><strong>协作</strong><br>与客户、合作伙伴和同事合作.</p><a href="pricing-and-signup.html" class="btn-primary btn h2 lightBlue get-Started-btn">开始使用</a><h6 class="get-Started-sub-btn">免费版本可用!</h6>

解决方案

灵活的盒子方式

垂直对齐现在通过使用变得非常简单灵活的盒子布局.现在,除 Internet Explorer 8 和9. 因此我们需要使用一些 hacks/polyfillsIE8/9 的不同方法.

在下文中,我将向您展示如何仅使用 3 行 文本(不管旧的 flexbox 语法).

注意:最好使用额外的类而不是改变 .jumbotron 来实现垂直对齐.例如,我会使用 vertical-center 类名.

示例 (A 镜像 jsbin).

<!--^--- 添加类 --><div class="容器">...

.vertical-center {最小高度:100%;/* 浏览器的回退不支持 vh 单元 */最小高度:100vh;/* 这两行算作一行 :-) */显示:弹性;对齐项目:居中;}

重要注意事项(在演示中考虑):

  1. heightmin-height 属性的 percentage 值是相对于 height父元素,因此您应该明确指定父元素的 height.

  2. 由于简洁,在发布的代码段中省略了供应商前缀/旧的 flexbox 语法,但在线示例中存在.

  3. 在一些旧的网络浏览器中,例如 Firefox 9 (我已经测试过),在这种情况下,flex 容器 - .vertical-center- 不会占用父级内部的可用空间,因此我们需要指定 width 属性,例如:width: 100%.

  4. 同样在上面提到的一些网络浏览器中,弹性项目 - 在这种情况下 .container - 可能不会水平出现在中心.似乎应用的 auto 的左/右 margin 对弹性项目没有任何影响.
    因此我们需要通过box-pack/justify-content来对齐它.

有关列的更多详细信息和/或垂直对齐方式,您可以参考以下主题:

<小时>

旧版网络浏览器的传统方式

这是我在回答这个问题时写的旧答案.这种方法已经讨论过在这里,它应该也适用于 Internet Explorer 8 和 9.我会简短地解释一下:

在内联流中,内联级别元素可以通过 vertical-align: middle 声明.来自 W3C 的规范:

<块引用>


将框的垂直中点与父框的基线加上父框 x 高度的一半对齐.

如果父元素 - .vertical-center 在这种情况下 - 具有明确的 height,如果我们可以有一个具有确切与父级相同的 height,我们将能够 将父级的基线移动到全高子级的中点,并出人意料地生成我们想要的流入式子级 - .容器 - 垂直居中对齐.

聚在一起

话虽如此,我们可以通过 ::before::after.vertical-center 内创建一个全高元素伪元素,并更改它和另一个子元素的默认 display 类型,.containerinline-block.

然后使用vertical-align: middle; 垂直对齐行内元素.

给你:

<div class="容器">...

.vertical-center {高度:100%;宽度:100%;文本对齐:居中;/* 水平对齐 inline(-block) 元素 */字体:0/0 一个;/* 删除 inline(-block) 元素之间的间隙 */}.vertical-center:before {/* 创建一个全高的行内块伪=元素 */内容: " ";显示:内联块;垂直对齐:中间;/* 行内元素的垂直对齐 */高度:100%;}.垂直中心>.容器 {最大宽度:100%;显示:内联块;垂直对齐:中间;/* 行内元素的垂直对齐 *//* 重置字体属性 */字体:16px/1 "Helvetica Neue", Helvetica, Arial, sans-serif;}

工作演示.

此外,为了防止超小屏幕出现意外问题,您可以将伪元素的高度重置为auto0 或更改其display 如果需要,输入 none :

@media (max-width: 768px) {.vertical-center:before {高度:自动;/* 或者 */显示:无;}}

更新演示

还有一件事:

如果容器周围有 footer/header 部分,最好将这些元素正确定位 (relative, >absolute? 由您决定.) 并添加一个更高的 z-index 值(为了保证)以保持它们始终位于其他的顶部.

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.

The .jumbotron has to be adapted to the full height and width of the screen. The .container div has a width of 1025px and should be in the middle of the page (vertically center).

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.

In the following I'll show you how to do that in only 3 lines of text (regardless of old flexbox syntax).

Note: it's better to use an additional class instead of altering .jumbotron to achieve the vertical alignment. I'd use vertical-center class name for instance.

Example Here (A Mirror on 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.

  2. Vendor prefixed / old flexbox syntax omitted in the posted snippet due to brevity, but exist in the online example.

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

  4. Also in some of web browsers as mentioned above, the flex item - .container in this case - may not appear at the center horizontally. It seems the applied left/right margin of auto doesn't have any effect on the flex item.
    Therefore we need to align it by 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

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. I'll explain it in short:

In inline flow, an inline level element can be aligned vertically to the middle by vertical-align: middle declaration. Spec from W3C:

middle
Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.

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.

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.

Then use 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:

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

这篇关于如何在 Bootstrap 中垂直居中容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆