Appearance
居中
水平居中
1.text-align
css
.parent {
text-align: center;
}
.son {
display: inline-block;
}
2.margin
css
.son {
margin: 0 auto;
}
3.flex
css
.parent {
display: flex;
justify-content: center;
}
4.grid
css
.parent {
display: grid;
justify-content: center;
}
/* 或者 */
.parent {
display: grid;
}
.son {
justify-self: center;
}
5.绝对定位
css
.parent {
position: relative;
}
.son {
position: absolute;
left: 50%;
transform: translate(-50%, 0);
}
6.table
css
.parent {
display: table;
text-align: center;
}
.son {
display: inline-block;
}
垂直居中
1.flex 布局
css
.parent {
display: flex;
align-items: center;
}
2.gird 布局
css
.parent {
display: grid;
align-items: center;
}
3.绝对定位
css
.parent {
position: relative;
}
.son {
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}
.son {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
4.vertical-align + line-height
css
.parent {
line-height: 200px;
}
.son {
display: inline-block;
vertical-align: middle;
}
水平垂直居中
1.flex
css
.parent {
display: flex;
justify-content: center;
align-items: center;
}
2.grid
css
.parent {
display: grid;
justify-content: center;
align-items: center;
}
/* OR */
.parent {
display: grid;
}
.son {
justify-self: center;
align-self: center;
}
3.绝对定位
css
.parent {
position: relative;
}
.son {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.son {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}