我有一个只有表单存在的页面,我希望将表单放置在屏幕的中心。
<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
,但它不起作用。
我错过了什么?
原文由 user348173 发布,翻译遵循 CC BY-SA 4.0 许可协议
引导程序 5(2021 年更新)
Bootstrap 5 仍然基于 flexbox ,因此垂直居中的工作方式与 Bootstrap 4 中的相同。例如,
align-items-center
(flex-direction: row)和justify-content-center
(flex-direction: column) 可以在 flexbox 父级(行或 d-flex)上使用。在 Bootstrap 5 中居中示例
垂直中心(不要忘记父级必须有一个定义的高度!):
my-auto
用于在 flex (.d-flex
) 元素 内部 居中my-auto
可用于在 —459463b125cae5c662668eadbc4e5f— 内 居中列(.col-
row
align-items-center
到 内 的 中心柱(col-*
row
水平中心:
text-center
居中display:inline
元素和列内容mx-auto
用于在 flex 元素内部居中mx-auto
可用于在 --- 内 居中列(.col-
row
justify-content-center
到 中心列(col-*
)里面row
Bootstrap 4.3+(2019 年更新)
不需要 额外的 CSS 。 Bootstrap 中已经包含的内容将起作用。确保表格的容器是 全高 的。 Bootstrap 4 现在有一个
h-100
100% 高度的类…垂直中心:
https://codeply.com/go/raCutAGHre
具有居中项目的容器的高度应为 100% (或相对于居中项目的所需高度)
注意:当在任何元素上使用
height:100%
( 百分比高度)时,元素 会采用其容器的高度。在现代浏览器中,可以使用 vh 单位height:100vh;
代替%
来获得所需的高度。因此,您可以设置 html、body {height: 100%},或者在容器上使用新的
min-vh-100
类来代替h-100
。水平中心:
text-center
居中display:inline
元素和列内容mx-auto
用于在 flex 元素内部居中offset-*
或mx-auto
可用于 中心列(.col-
)justify-content-center
到 中心列(col-*
)内部row
Bootstrap中的垂直对齐中心
Bootstrap 4 全屏居中表单
Bootstrap 4 中心输入组
Bootstrap 4 水平+垂直居中全屏