我有一个页面,其中只存在表单,我希望表单放在屏幕的中心.

<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-centeralign-self-center,但不能用.

我错过了什么?

DEMO

推荐答案

Bootstrap 5 (Updated 2021)

bootstrap 5仍然基于flexbox,因此垂直居中的工作方式与 bootstrap 4中的相同.例如,可以在弹性框父对象(行或d-flex)上使用align-items-center(FLEX-DIRECTION:ROW)和justify-content-center(FLEX-DIRECTION:COLUMN).

Centering examples in Bootstrap 5

Vertical center (don't forget the parent must have a defined height!):

  • my-auto用于定心inside个挠性(.d-flex)元件
  • my-auto可用于center columns(.col-)inside row
  • align-items-centercenter columns(col-*)inside row

Horizontal center:

  • text-center居中display:inline元素列内容(&A)
  • mx-auto用于在柔性元件内居中
  • mx-auto可用于row内的center columns(.col-)
  • justify-content-centercenter columns(col-*)内row

Bootstrap 4.3+ (Update 2019)

extra CSS元有no need元.Bootstrap中已经包含的内容将会正常工作.确保表单的容器为full height.Bootstrap 4现在有一个h-100%高度的h-100级.

Vertical center:

<div class="container h-100">
  <div class="row h-100 justify-content-center align-items-center">
    <form class="col-12">
      <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>

https://codeply.com/go/raCutAGHre

the height of the container with the item(s) to center should be 100%个 (或所需高度相对于居中项目的任何值)

注意:当对任何元素使用height:100%(percentage height)时,元素takes in the height of it's container.在现代浏览器中,可以使用VH单元height:100vh;而不是%来获得期望的高度.

因此,您可以设置html,body{Height:min-vh-100%},或者在CONTAINER上使用新的min-vh-100类,而不是h-100.


Horizontal center:

  • text-center居中display:inline元素列内容(&A)
  • mx-auto用于在柔性元件内居中
  • offset-*mx-auto可用于center columns(.col-)
  • justify-content-centercenter columns(col-*)内row

Vertical Align Center in Bootstrap
Bootstrap 4 full-screen centered form
Bootstrap 4 center input group
Bootstrap 4 horizontal + vertical center full screen

Css相关问答推荐

为什么我的容器的弹性包裹会导致我的自动填充网格添加额外的空轨道行?

如何在不重叠侧边栏的情况下尽可能使元素居中?

Next.js中的TailWind CSS类名称疑难解答

在文本之前添加了额外的空间-Angular 树视图CSS

显示 P 元素中加载数据的空行

解释 "document.styleSheets[0].cssRules[0].style;"

Angular Material - 将 matsnackbar 置于所有对话框之上

动态覆盖 Vuetify 类

如何使代码块输出在 quarto revealjs 演示文稿中水平滚动

应用填充时边框无法正常工作

如何在不使用 !important 的情况下提高优先级?

父母是内联块,子元素有%填充=奇怪的行为

CSS Slanding Div 边缘在图像上

有没有办法让它比它更敏感?

如何测试页面上的文本被删除

Bootstrap:在列之间添加边距/填充空间

如何将页脚保持在屏幕底部

继承了哪些 CSS 属性?

跨浏览器方法使子 div 适合其父级的宽度

如何重置或覆盖 IE CSS 过滤器?