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

<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


当前回答

我看了很多关于在页面中心放置表单的帖子,但没有一个是有效的。下面的代码来自一个使用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 }

其他回答

你需要一些东西来集中你的身体。但是因为你没有为你的html和body指定高度,它只会包装内容——而不是视口。换句话说,没有空间把物品放在中间。

html, body {
  height: 100%;
}
.container, .row.justify-content-center.align-items-center {
  height: 100%;
  min-height: 100%;
}

引导程序具有文本中心来集中文本。例如

<div class="container text-center">

您可以更改以下内容

<div class="row justify-content-center align-items-center">

到以下

<div class="row text-center">

从bootstrap v5.0开始:

<div class="position-absolute top-50 start-50 translate-middle">
   centred
</div>

从文档(引导4):

https://getbootstrap.com/docs/4.0/utilities/flex/#justify-content

.justify-content-start
.justify-content-end
.justify-content-center
.justify-content-between
.justify-content-around
.justify-content-sm-start
.justify-content-sm-end
.justify-content-sm-center
.justify-content-sm-between
.justify-content-sm-around
.justify-content-md-start
.justify-content-md-end
.justify-content-md-center
.justify-content-md-between
.justify-content-md-around
.justify-content-lg-start
.justify-content-lg-end
.justify-content-lg-center
.justify-content-lg-between
.justify-content-lg-around
.justify-content-xl-start
.justify-content-xl-end
.justify-content-xl-center
.justify-content-xl-between
.justify-content-xl-around

我来到这里是因为我遇到了Bootstrap 4网格系统和Angular *ngFor循环的问题。我通过对实现ngFor的div应用一个col justify-content-center类来修复它:

<div class="row" style="border:1px solid red;">
  <div class="col d-flex justify-content-center">
    <button mat-raised-button>text</button>
  </div>
  <div *ngFor="let text of buttonText" class="col d-flex justify-content-center">
    <button mat-raised-button>text</button>
  </div>
</div>

结果是: