在父子盒子模型中,子元素在父元素的盒子上做水平垂直居中
盒子模型
行内元素和块级元素的方式不同,此处仅列举常见的块级元素
- flex 弹性盒子布局
less
.box {
width: 200px;
height: 200px;
border: 1px solid #000;
display: flex;
justify-content: center;
align-items: center;
div {
width: 100px;
height: 60px;
background: aquamarine;
}
}
- flex 子项
less
.box {
width: 200px;
height: 200px;
border: 1px solid #000;
display: flex;
div {
width: 100px;
height: 60px;
background: aquamarine;
margin: auto;
}
}
- grid 网格布局
less
.box {
width: 200px;
height: 200px;
border: 1px solid #000;
display: grid;
justify-content: center;
align-items: center;
div {
width: 100px;
height: 60px;
background: aquamarine;
}
}
- grid 子项
less
.box {
width: 200px;
height: 200px;
border: 1px solid #000;
display: grid;
div {
width: 100px;
height: 60px;
background: aquamarine;
margin: auto;
}
}
- 绝对定位
less
.box {
width: 200px;
height: 200px;
border: 1px solid #000;
position: relative;
div {
width: 100px;
height: 60px;
background: aquamarine;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
- 绝对定位 + margin
less
.box {
width: 200px;
height: 200px;
border: 1px solid #000;
position: relative;
div {
width: 100px;
height: 60px;
background: aquamarine;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
}
- table-cell
less
.box {
width: 200px;
height: 200px;
border: 1px solid #000;
display: table-cell;
vertical-align: middle;
div {
width: 100px;
height: 60px;
background: aquamarine;
}
}