Bootstrap 居中垂直和水平对齐
Bootstrap Center Vertical and Horizontal Alignment
我有一个只有表单的页面,我想将表单放在屏幕中央。
<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
,但它不起作用。
我错过了什么?
你需要一些东西来让你的表单居中。但是因为您没有为 html 和正文指定高度,它只会包裹内容 - 而不是视口。换句话说,没有空间让项目居中。
html, body {
height: 100%;
}
.container, .row.justify-content-center.align-items-center {
height: 100%;
min-height: 100%;
}
Bootstrap 5(2021 年更新)
Bootstrap 5 仍然基于 flexbox,因此垂直居中的工作方式与 Bootstrap 4 中的相同。例如,align-items-center
(flex-direction: row) 和 justify-content-center
(flex-direction: column) 可以用在 flexbox parent (row or d-flex).
Centering examples in Bootstrap 5
垂直中心(不要忘记 parent 必须有定义的高度!):
my-auto
用于居中 inside flex (.d-flex
) elements
my-auto
可用于居中列 (.col-
) 内部 row
align-items-center
到中心列(col-*
)里面row
水平中心:
text-center
居中 display:inline
元素和列内容
mx-auto
用于在 flex 元素内居中
mx-auto
可用于在 row
中 居中列 (.col-
)
justify-content-center
到 中心列 (col-*
) 在 row
内
Bootstrap 4.3+(2019 年更新)
不需要 额外的CSS。 Bootstrap 中已经包含的内容将起作用。确保表格的容器 全高 。 Bootstrap 4 现在有一个 h-100
class 100% 高度...
垂直中心:
<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
物品居中的容器高度应为 100%
(或相对于居中项目所需的任何高度)
注意:在任何元素上使用height:100%
(百分比高度)时,元素takes in the height of it's container。在现代浏览器中,可以使用 vh 单位 height:100vh;
代替 %
来获得所需的高度。
因此,您可以设置 html、body {height: 100%},或者在容器上使用新的 min-vh-100
class 而不是 h-100
。
水平中心:
text-center
居中 display:inline
元素和列内容
mx-auto
用于在 flex 元素内居中
offset-*
或 mx-auto
可用于 列居中 (.col-
)
justify-content-center
到 中心列 (col-*
) 在 row
内
Bootstrap 4 full-screen centered form
Bootstrap 4 center input group
Bootstrap 4 horizontal + vertical center full screen
这对我有用:
<section class="h-100">
<header class="container h-100">
<div class="d-flex align-items-center justify-content-center h-100">
<div class="d-flex flex-column">
<h1 class="text align-self-center p-2">item 1</h1>
<h4 class="text align-self-center p-2">item 2</h4>
<button class="btn btn-danger align-self-center p-2" type="button" name="button">item 3</button>
</div>
</div>
</header>
</section>
flexbox 可以帮到你。 info here
<div class="d-flex flex-row justify-content-center align-items-center" style="height: 100px;">
<div class="p-2">
1
</div>
<div class="p-2">
2
</div>
</div>
Bootstrap 有 text-center 来居中文本。例如
<div class="container text-center">
您更改以下内容
<div class="row justify-content-center align-items-center">
以下
<div class="row text-center">
None 对我有用。但他的那个做到了。
由于 Bootstrap 4 .row class 现在是 display:flex 您可以简单地在任何列上使用新的 align-self-center flexbox 实用程序将其垂直居中:
<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>
我是从https://medium.com/wdstack/bootstrap-4-vertical-center-1211448a2eff
那里了解到的
这在 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>
在 CSS 中使用此代码:
.container {
width: 600px;
height: 350px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: inline-flex;
}
我之所以来到这里是因为我遇到了 Bootstrap 4 网格系统和 Angular *ngFor 循环的问题。我通过将 col justify-content-center class 应用于实现 ngFor:
的 div 来修复它
<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>
结果如下:
来自文档(bootstrap 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
浏览了很多关于让表单在页面中居中的帖子,其中 none 成功了。下面的代码来自使用 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>
where height in style is:
常量高度={
身高:'100vh'
}
我需要在 container-fluid 中垂直居中显示表格,所以我为此开发了自己的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<style>
.container-fluid
{
display: table-cell;
height: 100vh;
width: 100vw !important;
vertical-align: middle;
border:1px solid black;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-8 offset-2">
<div class="card shadow">
<div class="card-header bg-danger text-white">
<h2>Login</h2>
</div>
<div class="card-body">
<form action="">
<div class="form-group row">
<label for="txtemail" class="col-form-label col-sm-2">Email</label>
<div class="col-sm-10">
<input type="email" name="txtemail" id="txtemail" class="form-control" required />
</div>
</div>
<div class="form-group row">
<label for="txtpassword" class="col-form-label col-sm-2">Password</label>
<div class="col-sm-10">
<input type="password" name="txtpassword" id="txtpassword" class="form-control"
required />
</div>
</div>
<div class="form-group">
<button class="btn btn-danger btn-block">Login</button>
<button class="btn btn-warning btn-block">clear all</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="js/jquery.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
来自 bootstrap v5.0 只是:
<div class="position-absolute top-50 start-50 translate-middle">
centred
</div>
bootstrap 5
body {
display: flex;
align-items: center;
}
在使用 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 方式
HTML
<div class="row top-row">
<div class="col center-text">
<H1>Center Text Horizantally and vertically</H1>
</div>
</div>
Custom CSS
.top-row{
height: 300px; //It's important to set height
}
.center-text{
display: flex;
align-items: center;
justify-content: center;
}
我有一个只有表单的页面,我想将表单放在屏幕中央。
<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
,但它不起作用。
我错过了什么?
你需要一些东西来让你的表单居中。但是因为您没有为 html 和正文指定高度,它只会包裹内容 - 而不是视口。换句话说,没有空间让项目居中。
html, body {
height: 100%;
}
.container, .row.justify-content-center.align-items-center {
height: 100%;
min-height: 100%;
}
Bootstrap 5(2021 年更新)
Bootstrap 5 仍然基于 flexbox,因此垂直居中的工作方式与 Bootstrap 4 中的相同。例如,align-items-center
(flex-direction: row) 和 justify-content-center
(flex-direction: column) 可以用在 flexbox parent (row or d-flex).
Centering examples in Bootstrap 5
垂直中心(不要忘记 parent 必须有定义的高度!):
my-auto
用于居中 inside flex (.d-flex
) elementsmy-auto
可用于居中列 (.col-
) 内部row
align-items-center
到中心列(col-*
)里面row
水平中心:
text-center
居中display:inline
元素和列内容mx-auto
用于在 flex 元素内居中mx-auto
可用于在row
中 居中列 (justify-content-center
到 中心列 (col-*
) 在row
内
.col-
)
Bootstrap 4.3+(2019 年更新)
不需要 额外的CSS。 Bootstrap 中已经包含的内容将起作用。确保表格的容器 全高 。 Bootstrap 4 现在有一个 h-100
class 100% 高度...
垂直中心:
<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
物品居中的容器高度应为 100% (或相对于居中项目所需的任何高度)
注意:在任何元素上使用height:100%
(百分比高度)时,元素takes in the height of it's container。在现代浏览器中,可以使用 vh 单位 height:100vh;
代替 %
来获得所需的高度。
因此,您可以设置 html、body {height: 100%},或者在容器上使用新的 min-vh-100
class 而不是 h-100
。
水平中心:
text-center
居中display:inline
元素和列内容mx-auto
用于在 flex 元素内居中offset-*
或mx-auto
可用于 列居中 (.col-
)justify-content-center
到 中心列 (col-*
) 在row
内
Bootstrap 4 full-screen centered form
Bootstrap 4 center input group
Bootstrap 4 horizontal + vertical center full screen
这对我有用:
<section class="h-100">
<header class="container h-100">
<div class="d-flex align-items-center justify-content-center h-100">
<div class="d-flex flex-column">
<h1 class="text align-self-center p-2">item 1</h1>
<h4 class="text align-self-center p-2">item 2</h4>
<button class="btn btn-danger align-self-center p-2" type="button" name="button">item 3</button>
</div>
</div>
</header>
</section>
flexbox 可以帮到你。 info here
<div class="d-flex flex-row justify-content-center align-items-center" style="height: 100px;">
<div class="p-2">
1
</div>
<div class="p-2">
2
</div>
</div>
Bootstrap 有 text-center 来居中文本。例如
<div class="container text-center">
您更改以下内容
<div class="row justify-content-center align-items-center">
以下
<div class="row text-center">
None 对我有用。但他的那个做到了。
由于 Bootstrap 4 .row class 现在是 display:flex 您可以简单地在任何列上使用新的 align-self-center flexbox 实用程序将其垂直居中:
<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>
我是从https://medium.com/wdstack/bootstrap-4-vertical-center-1211448a2eff
那里了解到的这在 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>
在 CSS 中使用此代码:
.container {
width: 600px;
height: 350px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: inline-flex;
}
我之所以来到这里是因为我遇到了 Bootstrap 4 网格系统和 Angular *ngFor 循环的问题。我通过将 col justify-content-center class 应用于实现 ngFor:
的 div 来修复它<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>
结果如下:
来自文档(bootstrap 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
浏览了很多关于让表单在页面中居中的帖子,其中 none 成功了。下面的代码来自使用 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>
where height in style is:
常量高度={ 身高:'100vh' }
我需要在 container-fluid 中垂直居中显示表格,所以我为此开发了自己的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<style>
.container-fluid
{
display: table-cell;
height: 100vh;
width: 100vw !important;
vertical-align: middle;
border:1px solid black;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-8 offset-2">
<div class="card shadow">
<div class="card-header bg-danger text-white">
<h2>Login</h2>
</div>
<div class="card-body">
<form action="">
<div class="form-group row">
<label for="txtemail" class="col-form-label col-sm-2">Email</label>
<div class="col-sm-10">
<input type="email" name="txtemail" id="txtemail" class="form-control" required />
</div>
</div>
<div class="form-group row">
<label for="txtpassword" class="col-form-label col-sm-2">Password</label>
<div class="col-sm-10">
<input type="password" name="txtpassword" id="txtpassword" class="form-control"
required />
</div>
</div>
<div class="form-group">
<button class="btn btn-danger btn-block">Login</button>
<button class="btn btn-warning btn-block">clear all</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="js/jquery.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
来自 bootstrap v5.0 只是:
<div class="position-absolute top-50 start-50 translate-middle">
centred
</div>
bootstrap 5
body {
display: flex;
align-items: center;
}
在使用 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 方式
HTML
<div class="row top-row">
<div class="col center-text">
<H1>Center Text Horizantally and vertically</H1>
</div>
</div>
Custom CSS
.top-row{
height: 300px; //It's important to set height
}
.center-text{
display: flex;
align-items: center;
justify-content: center;
}