今天来看看在 CSS 中有哪些方法可以实现元素的居中!
复制
<div class="container"> <span class="content">水平居中</span></div>
1.
2.
3.
text-align 一般运用在块级元素中,使其中的文本对齐。实际上,运用在块级元素中的text-align会使其包含的内联元素水平对齐。
复制
.container { text-align: center;}
1.
2.
3.
复制
<div class="container"> <div class="content">水平居中</div></div>
1.
2.
3.
如果块元素的高度和宽度已知,就可以通过将元素的左右margin值设置为auto将元素水平居中:
复制
.content { width: 100px; height: 100px; margin-left: auto; margin-right: auto;}
1.
2.
3.
4.
5.
6.
如果有多个块元素,需要将多个元素包裹在一个元素中以使用该方法实现水平居中:
复制
<div class="container"> <div class="box"><div class="content">水平居中</div><div class="content">水平居中</div> </div></div>
1.
2.
3.
4.
5.
6.
复制
.box { display: flex; margin-left: auto; margin-right: auto;}
1.
2.
3.
4.
5.
在 Flex 布局中,justify-content可以用于设置弹性盒子元素在主轴方向上的对齐方式。当其属性值为 center 时,其子元素整体会在主轴的中心位置。
复制
.container { display: flex; justify-content: center;}
1.
2.
3.
4.
如果弹性盒子的主轴是垂直方向,可以使用align-items来代替justify-content以实现元素的水平居中:
复制
.container { display: flex; flex-direction: column align-items: center;}
1.
2.
3.
4.
5.
在 Grid 布局中,justify-content 属性会沿着行轴线(水平方向) 在网格容器中对齐网格。当属性值为center时,就可以将网格对齐到网格容器的水平居中位置。
复制
.container { display: grid; justify-content: center;}
1.
2.
3.
4.
可以通过将使用绝对定位和变换实现元素的水平居中:
复制
.container { position: relative;}.content { position: absolute; left: 50%; transform: translateX(-50%);}
1.
2.
3.
4.
5.
6.
7.
8.
9.
如果块元素的宽度已知,也可以使用负边距来代替transform:
复制
.container { position: relative;}.content { width: 100px; position: absolute; left: 50%; margin-left: -50px;}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
复制
<div class="container"> <div class="content">垂直居中</div></div>
1.
2.
3.
可以通过将使用绝对定位和变换实现元素的垂直居中:
复制
.container { position: relative;}.content { position: absolute; top: 50%; transform: translateY(-50%);}
1.
2.
3.
4.
5.
6.
7.
8.
9.
如果块元素的高度已知,也可以使用负边距来代替transform:
复制
argin-top: -50px;}2. 通用
1.
2.
在 Flex 布局中,align-items 属性用来定义flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式。当其属性值为 center 时,元素位于容器的中心。
复制
.container { display: flex; align-items: center;}
1.
2.
3.
4.
如果将Flex 的主轴切换为垂直方向,则需要使用justify-content来代替align-items以实现元素的垂直居中:
复制
.flex { display: flex; flex-direction: column; justify-content: center;}
1.
2.
3.
4.
5.
使用 CSS Grid 布局中,可以使用 align-content 属性将项目垂直居中到其网格区域。
复制
.container { display: grid; align-content: center;}
1.
2.
3.
4.
如果将网格的排列方向更改为水平,垂直居中依旧是生效的:
复制
.container { display: flex; align-content: center; grid-auto-flow: column;}
1.
2.
3.
4.
5.
复制
<div class="container"> <div class="content">水平垂直居中</div></div>
1.
2.
3.
使元素垂直居中最通用的方法就是使用绝对定位和transform:
复制
.container { position: relative;}.content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
如果元素的高度和宽度已知,也可以使用margin来代替transform:
复制
.container { position: relative;}.content { width: 100px; height: 100px; position: absolute; top: 50%; left: 50%; margin-top: -50px; margin-left: -50px;}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
在使用 Flex 布局时,可以结合上面的水平和垂直居中来实现水平垂直居中:
复制
.container { display: flex; justify-content: center; align-items: center;}
1.
2.
3.
4.
5.
在 Grid 布局中,可以使用以下形式来实现元素的水平垂直居中:
复制
.container { display: grid; place-items: center;}
1.
2.
3.
4.
place-content 属性是align-content和justify-content的简写,当该属性的值为center时,所有的子元素堆叠在父元素的中间对齐。