我有一个页面,其中只有形式存在,我想把形式放在屏幕的中心。

<div class="container">
  <div class="row justify-content-center align-items-center">
    <form>
      <div class="form-group">
        <label for="formGroupExampleInput">Example label</label>
        <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input">
      </div>
      <div class="form-group">
        <label for="formGroupExampleInput2">Another label</label>
        <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input">
      </div>
    </form>   
  </div>  
</div>

justify-content-center使表单水平对齐,但我不知道如何垂直对齐。我尝试使用align-items-center和align-self-center,但它不起作用。

我错过了什么?

DEMO


当前回答

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-12 border border-info"> <div class="d-flex justify-content-center align-items-center" style="height: 100px"> <a href="#" class="btn btn-dark">Transfer</a> <a href="#" class="btn btn-dark mr-2 ml-2">Replenish</a> <a href="#" class="btn btn-dark mr-3">Account Details</a> </div> </div>

其他回答

在使用v4或v5时尝试下面给出的代码。

<div class="d-flex align-items-center justify-content-center" style="height:100vh;">
  Vertically and Horizontally Aligned :)
</div>

…或…如果使用v5,则可以使用这些类。

<div class="position-absolute top-50 start-50 translate-middle">
  Vertically and Horizontally Aligned :)
</div>

这样做,就是这样。

我看了很多关于在页面中心放置表单的帖子,但没有一个是有效的。下面的代码来自一个使用bootstrap 4.1的react组件。高度应该是100vh,而不是100%

<div className="container">
      <div className="d-flex justify-content-center align-items-center" style={height}>
        <form>
          <div className="form-group">
            <input className="form-control form-control-lg" placeholder="Email" type="email"/>
          </div>
          <div className="form-group">
            <input className="form-control form-control-lg" placeholder="Password" type="password"/>
          </div>
          <div className="form-group">
            <button className="btn btn-info btn-lg btn-block">Sign In</button>
          </div>
        </form>
      </div>
    </div>

其中身高在风格上为:

Const height = { 身高:100 vh }

引导的方式

超文本标记语言

 <div class="row top-row">
        <div class="col center-text">
            <H1>Center Text Horizantally and vertically</H1>
        </div>
</div>

自定义CSS

  .top-row{
        height: 300px; //It's important to set height 
    }
  .center-text{
       display: flex;
       align-items: center;
       justify-content: center;
    }

这是在IE 11 Bootstrap 4.3中工作。而在我的情况下,其他答案都不能在IE11中工作。

 <div class="row mx-auto justify-content-center align-items-center flex-column ">
    <div class="col-6">Something </div>
</div>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-12 border border-info"> <div class="d-flex justify-content-center align-items-center" style="height: 100px"> <a href="#" class="btn btn-dark">Transfer</a> <a href="#" class="btn btn-dark mr-2 ml-2">Replenish</a> <a href="#" class="btn btn-dark mr-3">Account Details</a> </div> </div>