【问题标题】:Vertical Align Center in Bootstrap 4 [duplicate]Bootstrap 4中的垂直对齐中心[重复]
【发布时间】:2017-07-04 07:04:27
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

    标签: css twitter-bootstrap bootstrap-4 flexbox


    【解决方案1】:

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

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

    Updated Pen

    【讨论】:

    • 哦,哈哈,我错过了描述中的链接...您还添加了html, body {height:100%} ...添加使其工作!谢谢!
    • 你的答案并没有以所要求的方式解决问题(使用引导程序)
    • 我完全不同意你的观点,尽管问题是关于如何使用 Bootstrap 类,但你建议解决方法。
    • @AlexNikonov 在 OP 问题中没有说他们仅限于使用引导程序 - 对于可能登陆此页面的其他人来说,这是一种替代方法,希望在不使用引导程序的情况下垂直对齐事物类。继续前进 - 这里还有很多其他答案供您使用。我不明白您为什么对此感到困惑-这是一个所有有用想法的论坛-而不仅仅是一心一意的答案。像您这样的用户通过对提供问题解决方案的答案投反对票来破坏该网站,但只是因为您不喜欢它
    • 或 BS4:
      一些文字
    【解决方案2】:
    .jumbotron {
        position: relative;
        top: 50%;
        transform: translateY(-50%);
    }
    

    【讨论】:

      【解决方案3】:

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

      如果您要居中的元素的父元素没有定义 高度没有垂直居中解决方案可以工作!

      现在,到垂直居中...

      引导程序 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

      Vertical Center in Bootstrap 5


      引导程序 4

      您可以使用新的flexbox & size utilities 使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 on flexbox parent.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 on flexbox parent.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 和 other utilities,有很多垂直的方法 结盟。 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>
      

      Vertical Center Using Auto Margins Demo

      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>
      

      或者,在整个.row 上使用align-items-center 将行中的所有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>
      

      Vertical Center Different Height Columns Demo

      See this Q/A to center, but maintain equal height


      3 - 使用显示工具的垂直居中:

      Bootstrap 4 具有display utils,可用于display:tabledisplay:table-celldisplay:inline 等。这些可与vertical alignment utils 一起使用以对齐内联、内联块或表格单元格元素。

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

      Vertical Center Using Display Utils Demo

      更多示例
      Vertical center image in &lt;div&gt;
      Vertical center .row in .container
      Vertical center and bottom in &lt;div&gt;
      Vertical center child inside parent
      Vertical center full screen jumbotron


      重要! 我提到身高了吗?

      记住垂直居中是相对于父元素的高度。如果你想在整个页面居中,在大多数情况下,这应该是你的 CSS...

      body,html {
        height: 100%;
      }
      

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

      另见:
      Vertical alignment in bootstrap 4
      Bootstrap Center Vertical and Horizontal Alignment

      【讨论】:

      • 在带有导航栏的页面上使用上述内容时,我得到了一个滚动条。我认为它在导航栏下方增加了 100% 的高度并居中。我该如何解决?
      • 我正在尝试将多行对齐到弹性框的中心。我想将所有按钮居中对齐。 codeply.com/go/5abdqC33cU
      • 我准备了一个 codepen,目前显示 3 种居中方式,基于此处显示的方式:codepen.io/yigith/pen/oNjmGEL
      • 选项 1 仅在 &lt;div class="container d-flex h-100"&gt; 更改为 &lt;div class="container d-flex vh-100"&gt; 时对我有效
      • 这是正确的,因为h-100 是其父容器的 100%,而 vh-100 是视口的高度。在提出问题时,vh-100 在 Bootstrap 中不存在。
      【解决方案4】:

      在 Bootstrap 4(测试版)中,使用 align-middle。参考Bootstrap 4 Documentation on Vertical alignment

      使用 vertical-alignment 更改元素的对齐方式 公用事业。请注意,vertical-align 只影响 inlineinline-blockinline-table表格单元格 元素。

      .align-baseline.align-top.align-middle.align-bottom 中选择, .align-text-bottom.align-text-top(根据需要)。

      【讨论】:

        【解决方案5】:

        以下引导程序 4 类帮助我解决了这个问题

        <div class="col text-center justify-content-center align-self-center">
            <img width=3rem src=".." alt="...">
        </div>
        

        【讨论】:

        • 这并没有使我的 div 垂直居中。
        • 您能否详细说明您的 css 代码。分享一个sn-p,我会调查你面临的问题。
        【解决方案6】:

        在 Bootstrap 4.1.3 中:

        当我尝试将引导徽章置于 container &gt; row &gt; column 中的中心位置时,这对我很有用。h1 标题旁边。

        起作用的是一些简单的 css:

        .my-valign-center {
          vertical-align: 50%;
        }
        

        <span class="badge badge-pill" style="vertical-align: 50%;">My Badge</span>
        

        【讨论】:

          【解决方案7】:

          因为以上都不适合我,所以我添加另一个答案。

          目标:使用 bootstrap 4 flexbox 类在页面上垂直和水平对齐 div。

          第 1 步:将最外层 div 的高度设置为 100vh。这会将高度设置为 Veiwport 高度的 100%。如果你不这样做,其他任何事情都不会起作用。将其设置为100% 的高度仅相对于父级,因此如果父级不是视口的完整高度,则不会起作用。在下面的示例中,我将 Body 设置为 100vh。

          第 2 步:将容器 div 设置为具有 d-flex 类的 flexbox 容器。

          第 3 步:使用 justify-content-center 类水平居中 div。

          第 4 步:使用 align-items-center 垂直居中 div

          第 5 步:运行页面,查看垂直和水平居中的 div。

          注意,居中的 div 本身(子 div)不需要设置特殊的类

          <body style="background-color:#f2f2f2; height:100vh;">
          
          <div class="h-100 d-flex justify-content-center align-items-center">
              <div style="height:600px; background-color:white; width:600px;">
          </div>
          
          </div>
          
          </body>
          

          【讨论】:

          • @AjayKumar,您愿意分享您的修改吗?
          • 谢谢,尤其是100vh 技巧对我帮助很大。 Bootstrap 还为此提供了vh-100 类。
          【解决方案8】:
          <!DOCTYPE html>
          <html lang="en">
          <head>
            <title>Bootstrap Example</title>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
          </head>
          <body>  
          <div class="container">
              <div class="row align-items-center justify-content-center" style="height:100vh;">     
                   <div>Center Div Here</div>
              </div>
          </div>
          </body>
          </html>
          

          【讨论】:

          • @AjayKumar 将flex-grow-1 类添加到卡片可防止卡片缩小。
          【解决方案9】:
          <div class="col-lg-5 col-sm-5 offset-1 d-flex">
                      <div class="offer-txt justify-content-center align-self-center">
                          <span class="inner-title">Our Offer</span>
                          <h2 class="section-title">Today’s Special </h2>
                          <p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly.</p>
                      </div>
                  </div>
          

          【讨论】:

          • 你的照片让我觉得 stackoverflow 已经开始了广告:D
          【解决方案10】:

          我是用 Bootstrap 这样做的 4.3.1:

          <div class="d-flex vh-100">
            <div class="d-flex w-100 justify-content-center align-self-center">
              I'm in the middle
            </div>
          </div>
          

          【讨论】:

          • 终于,可行的方法
          【解决方案11】:

          我已经尝试了这里的所有答案,但发现这里是 h-100vh-100之间的区别> 这是我的解决方案:

                <div className='container vh-100 d-flex align-items-center col justify-content-center'>
                  <div className="">
                       ...
                  </div>
                </div >
          
          

          【讨论】:

            【解决方案12】:

            在你的 div 上使用 .my-auto (bootsrap4) css 类

            【讨论】:

              【解决方案13】:
              <div class="row">
                <div class="col-md-6">
                   <img src="/assets/images/ebook2.png" alt="" class="img-fluid">
                </div>
                <div class="col-md-6 my-auto">
                   <h3>Heading</h3>
                   <p>Some text.</p>
                 </div>
              </div>
              

              这一行是魔术发生的地方&lt;div class="col-md-6 my-auto"&gt;my-auto 将使列的内容居中。这适用于像上面的代码示例这样的情况,您可能有一个可变大小的图像并且需要将右侧列中的文本与其对齐。

              【讨论】:

                【解决方案14】:

                将您的内容放置在 100% 高的 flexbox 容器中,即 h-100。然后使用 justify-content-center 类集中对齐内容。

                <section class="container h-100 d-flex justify-content-center">
                    <div class="jumbotron my-auto">
                        <h1 class="display-3">Hello, Malawi!</h1>
                    </div>
                </section>
                

                【讨论】:

                  【解决方案15】:

                  垂直对齐使用这个引导程序 4 个类:

                  方法一:

                  父级:d-table

                  child: d-table-cell & align-middle & text-center

                  例如:

                  <div class="tab-icon-holder d-table bg-light">
                     <div class="d-table-cell align-middle text-center">
                       <img src="assets/images/devices/Xl.png" height="30rem">
                     </div>
                  </div>
                  

                  如果你想让父母成为圈子:

                  <div class="tab-icon-holder d-table bg-light rounded-circle">
                     <div class="d-table-cell align-middle text-center">
                       <img src="assets/images/devices/Xl.png" height="30rem">
                     </div>
                  </div>
                  

                  其中两个自定义css类如下:

                  .tab-icon-holder {
                    width: 3.5rem;
                    height: 3.5rem;
                   }
                  .rounded-circle {
                    border-radius: 50% !important
                  }
                  

                  最后的用法可以像这样:

                  <div class="col-md-5 mx-auto text-center">
                      <div class="d-flex justify-content-around">
                       <div class="tab-icon-holder d-table bg-light rounded-circle">
                         <div class="d-table-cell align-middle text-center">
                           <img src="assets/images/devices/Xl.png" height="30rem">
                         </div>
                       </div>
                  
                       <div class="tab-icon-holder d-table bg-light rounded-circle">
                         <div class="d-table-cell align-middle text-center">
                           <img src="assets/images/devices/Lg.png" height="30rem">
                         </div>
                       </div>
                  
                       ...
                  
                      </div>
                  </div>
                  

                  方法二: 您可以使用以下内容:

                  父级:h-100 d-table mx-auto

                  child: d-table-cell align-middle

                  方法3:

                  您可以使用以下内容:

                  父级:d-flext align-items-center

                  如果您也希望内容水平居中:

                  父级:d-flext align-items-center justify-content-center

                  【讨论】:

                  • align-middle 是我一直在找的,非常简单正确,谢谢!
                  【解决方案16】:

                  在 Bootstrap 5 中,我们可以轻松做到这一点。下面是对齐卡片的示例。我们只需要设置卡片宽度,其余设置使用bootstrap 5类。

                  <div class="container d-flex vh-100">
                    <div class="row mx-auto">
                      <div class="col align-self-center p-4">
                        <div class="card rounded-3 shadow-sm p-3" style="width:400px">
                          <div class="card-body">
                            <h1>I'am centered vertically and horizontally</h1>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                  

                  【讨论】:

                    【解决方案17】:

                    使用 bootstrap 版本 5 或更高版本,使用下面的代码将内容水平和垂直居中。

                    <div class="vh-100 d-flex justify-content-center align-items-center">
                      <h1>Centered horizontally and vertically!</h1>
                    </div>
                    

                    【讨论】:

                    • 你才是真正的 MVP。
                    • v5 怎么样?这只会水平居中。
                    【解决方案18】:

                    对于所有 Bootstrap 版本 2、3、4 和 5,只有两个自定义类适用于任何高度和宽度。

                    .d_tbl{
                      display:table;
                      width:100%;
                      height:200px;
                    }
                    
                    .d_tblCel{
                      vertical-align: middle;
                      display: table-cell;
                      text-align: center;
                    }
                    <!DOCTYPE html>
                    <html lang="en">
                    <head>
                      <meta charset="utf-8">
                      <meta name="viewport" content="width=device-width, initial-scale=1">
                      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
                      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
                    </head>
                    <body>
                    
                    <div class="container">
                      <div class="d_tbl" style="background-color:antiquewhite">
                        <div class="d_tblCel">
                          <h3>Centered horizontally and vertically</h3>
                          <p>for all bootstrap versions with only two custom classes</p>
                        </div>
                      </div>
                    </div>
                    
                    </body>
                    </html>

                    【讨论】:

                      【解决方案19】:

                      bootstrap 5 和 blazor Web 程序集:

                      <div class="d-flex align-items-center justify-content-center min" style="height: 50vh">
                      <button class="btn btn-outline-success mx-2">Register</button>
                      <button class="btn btn-outline-primary mx-2" style="width:123.44px">Login</button></div>
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2017-12-10
                        • 2017-09-04
                        • 2017-05-07
                        • 2018-08-23
                        • 2020-03-11
                        • 1970-01-01
                        相关资源
                        最近更新 更多