Bootstrap 4中的垂直对齐中心

新手上路,请多包涵

我正在尝试使用 Bootstrap 4 将我的 Container 放在页面中间。到目前为止,我一直没有成功。任何帮助,将不胜感激。

我已经在 Codepen.io 构建了它,所以你们可以玩它,让我知道什么是有效的,因为我没有想法……

 var currentAuthor = "";
var currentQuote  = "";
function randomQuote() {
  $.ajax({
      url: "https://api.forismatic.com/api/1.0/?",
      dataType: "jsonp",
      data: "method=getQuote&format=jsonp&lang=en&jsonp=?",
      success: function( response ) {
        $("#quote-content").html('<h2 id="quote-content" class="display-5"><i class="fa fa-quote-left" aria-hidden="true"> ' + response.quoteText + ' <i class="fa fa-quote-right" aria-hidden="true"></i></h2>');
        $("#quote-author").html('<p id="quote-author" class="lead"><em>' + response.quoteAuthor + '</em></p>');

        currentAuthor = response.quoteAuthor;
        currentQuote  = response.quoteText
      }
  });
}

function openURL(url){
  window.open(url,'Share', 'width=550, height=400, toolbar=0, scrollbars=1 ,location=0 ,statusbar=0,menubar=0, resizable=0');
}

function tweetQuote(){
      openURL('https://twitter.com/intent/tweet?hashtags=quotes,freecodecamp&related=freecodecamp&text=' + encodeURIComponent('"' + currentQuote + '" - ' + currentAuthor));
}

$(document).ready(function () {
    randomQuote();

    $("#get-another-quote-button").click(function(){
        randomQuote();
    });

   $('#tweet').on('click', function() {
       tweetQuote();
   });
});
 html, body {
  background-image: url("https://www.mylinea.com/wp-content/uploads/beautiful-trees-stock-photo-055.jpg");
    background-color: #17234E;
    margin-bottom: 0;
    min-height: 30%;
    background-repeat: no-repeat;
    background-position: center;
    -webkit-background-size: cover;
    background-size: cover;
}

.btn-new-quote {
    color: #0C0C0D;
    background-color: transparent;
    border-color: #414147;
}

.btn-new-quote:hover {
    color: #0C0C0D;
    background-color: #9A989E;
    border-color: #0C0C0D;
}

#tweet {
    color: RGB(100, 100, 100);
}

#tweet:hover {
    color: RGB(50, 50, 50);
}

.jumbotron {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    opacity: .85;
    border-color:  RGB(50, 50, 50);
    padding-bottom: 8px;
}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
<div class="container">
    <div class="row justify-content-center align-self-center">
        <div class="col-sm-6">
            <div class="jumbotron vertical-center text-center">
                <h2 id="quote-content" class="display-5"><i class="fa fa-quote-left" aria-hidden="true"></i><i class="fa fa-quote-right" aria-hidden="true"></i></h2>
                <p id="quote-author" class="lead"><em></em></p>
                <hr class="my-2">
                <div class="row align-items-center justify-content-between">
                    <div class="col-sm-1-4 text-left">
                        <a id="tweet" href="#">
                            <h2 class="display-4"><i class="fa fa-twitter" aria-hidden="true"></i></h2>
                        </a>
                    </div>
                    <div class="col-sm-1-4 text-right">
                        <button id="get-another-quote-button" type="button" class="btn btn-outline-secondary  btn-new-quote">Don't Quote Me on This...</button>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

原文由 canaan seaton 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 764
2 个回答

重要的! 垂直中心是相对于父级的 高度

如果您要居中的元素的父元素没有定义 height ,则 任何 垂直居中解决方案都不起作用!

现在,到垂直居中…

引导程序 5(2021 年更新)

Bootstrap 5 仍然基于 flexbox ,因此垂直居中的工作方式与 Bootstrap 4 相同。例如, align-items-centerjustify-content-center 或自动边距可用于 flexbox 父级( rowd-flex )。

  • 在 flexbox 行父级上使用 align-items-centerrowd-flex
  • 在 flexbox 列父级上使用 justify-content-center ( d-flex flex-column )
  • 在 flexbox 父级上使用 my-auto

Bootstrap 5中的垂直中心


引导程序 4

您可以使用新的 flexbox & size 实用程序 来制作 container 全高和 display: flex 。这些选项 不需要额外的 CSS (除了 容器的高度(即:html,body)必须为 100% )。

选项 1 align-self-center 在 flexbox 子节点上

<div class="container d-flex h-100">
    <div class="row justify-content-center align-self-center">
     I'm vertically centered
    </div>
</div>

https://codeply.com/go/fFqaDe5Oey

选项 2 align-items-center 在 flexbox 父级.rowdisplay:flex; flex-direction:row

 <div class="container h-100">
    <div class="row align-items-center h-100">
        <div class="col-6 mx-auto">
            <div class="jumbotron">
                I'm vertically centered
            </div>
        </div>
    </div>
</div>

在此处输入图像描述

https://codeply.com/go/BumdFnmLuk

选项 3 justify-content-center 在 flexbox 父级上.carddisplay:flex;flex-direction:column

 <div class="container h-100">
    <div class="row align-items-center h-100">
        <div class="col-6 mx-auto">
            <div class="card h-100 border-primary justify-content-center">
                <div>
                    ...card content...
                </div>
            </div>
        </div>
    </div>
</div>

https://codeply.com/go/3gySSEe7nd


更多关于 Bootstrap 4 垂直居中

现在 Bootstrap 4 提供了 flexbox 和 其他实用程序,有许多垂直对齐的方法。 http://www.codeply.com/go/WG15ZWC4lf

1 - 使用自动边距垂直居中:

另一种垂直居中的方法是使用 my-auto 。这将使元素在其容器中居中。例如, h-100 使行全高,而 my-auto 将使 col-sm-12 列垂直居中。

 <div class="row h-100">
    <div class="col-sm-12 my-auto">
        <div class="card card-block w-25">Card</div>
    </div>
</div>

垂直居中使用自动边距 演示

my-auto 表示垂直y轴上的边距,相当于:

 margin-top: auto;
margin-bottom: auto;


2 - 带有 Flexbox 的垂直中心:

垂直中心网格列

由于 Bootstrap 4 .row 现在是 display:flex 您可以简单地使用 align-self-center 在任何列上将其垂直居中…

        <div class="row">
           <div class="col-6 align-self-center">
                <div class="card card-block">
                 Center
                </div>
           </div>
           <div class="col-6">
                <div class="card card-inverse card-danger">
                    Taller
                </div>
          </div>
    </div>

或者,在整个 align-items-center .row 以垂直居中对齐所有 col-* 在行中…

        <div class="row align-items-center">
           <div class="col-6">
                <div class="card card-block">
                 Center
                </div>
           </div>
           <div class="col-6">
                <div class="card card-inverse card-danger">
                    Taller
                </div>
          </div>
    </div>

垂直中心不同高度的列 演示

请参阅此 Q/A 以居中,但保持相同的高度


3 - 使用 Display Utils 垂直居中:

Bootstrap 4 具有可用于 display:table display:table-celldisplay:inline 等的显示工具,这些工具可以与 垂直对齐方式 对齐使用内联块或表格单元格元素。

 <div class="row h-50">
    <div class="col-sm-12 h-100 d-table">
        <div class="card card-block d-table-cell align-middle">
            I am centered vertically
        </div>
    </div>
</div>

使用 Display Utils 演示 垂直居中

更多示例

<div> 中的垂直中心图像

.container 中的垂直居中 .row

<div> 中的垂直中心和底部

父项内的垂直中心子项

垂直中心全面屏jumbotron


重要的! 我提到身高了吗?

请记住,垂直居中是相对于 父元素的高度而言的。如果您想以整个页面为中心,在大多数情况下,这应该是您的 CSS…

 body,html {
  height: 100%;
}

或在父容器/容器上使用 min-height: 100vh (在 Bootstrap 4.1+ 中为 min-vh-100 )。如果你想在父元素中居中一个子元素。父级必须具有定义的 高度

另见:

bootstrap 4中的垂直对齐

Bootstrap 中心垂直和水平对齐

原文由 Zim 发布,翻译遵循 CC BY-SA 4.0 许可协议

您可以通过使父容器弯曲并添加 align-items:center 来垂直对齐您的容器:

 body {
  display:flex;
  align-items:center;
}

更新的笔

原文由 Pete 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题