1、flex弹性布局:
.father {
width: 300px;
height: 300px;
display:flex;
justify-content:center;
align-items:center;
}
.box {
width: 100px;
height: 100px;
}
2、table-cell+vertical-align 类似td表格单元
.father {
width: 300px;
height: 300px;
display:table-cell;
vertical-align:middle;
text-align:center;
}
.box {
width: 100px;
height: 100px;
display:line-block;
}
3、line-height+vertical-align
.father {
width: 300px;
height: 300px;
line-height:300px;
text-align:center;
}
.box {
width: 100px;
height: 100px;
vertical-align:middle;
display:line-block;
}
4、absolute+transform
.father {
width: 300px;
height: 300px;
position: relative;
}
.box {
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
/*平移*/
transform: translate(-50%,-50%);
}
5、absolute+负margin
.father {
width: 300px;
height: 300px;
position: relative;
}
.box {
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
/*宽高的一半*/
margin-left: -50px;
margin-top: -50px;
}
6、absolute+margin文章来源:https://www.toymoban.com/news/detail-814390.html
.father {
width: 300px;
height: 300px;
position: relative;
}
.box {
width: 100px;
height: 100px;
position: absolute;
left: 0;
top: 0;
bottom:0;
right:0;
margin:auto;;
}
7、grid文章来源地址https://www.toymoban.com/news/detail-814390.html
.father {
width: 300px;
height: 300px;
display: grid;
}
.box {
width: 100px;
height: 100px;
align-self: center;
justify-self: center;
}
到了这里,关于div垂直居中方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!