目录
盒子水平垂直居中的方法
一、定位1
<style>
.box1{
width: 1000px;
height: 1000px;
overflow: hidden;
background: blue;
}
.box{
box-sizing: border-box;
width: 200px;
height: 200px;
background: red;
text-align: center;
line-height: 100px;
}
.box1{
position: relative;
}
.box{
position: absolute;
top:50%;
left:50%;
margin-top: -100px;
margin-left: -100px;
}
</style>
<body>
<div class="box1">
<div class="box">
盒子
</div>
</div>
</body>
二、定位2
.box1{
position: relative;
}
.box{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom:0 ;
margin: auto;
}
三、定位3
.box1{
position: relative;
}
.box{
position: absolute;
top:50%;
left: 50%;
transform: translate(-50%,-50%);
}
四、flex弹性布局
.box1{
display: flex;
justify-content: center;
align-items: center;
}
五、table-cell
.box1{
display: table-cell;
vertical-align: middle;
text-align: center;
}
.box{
display: inline-block;
}
六、JS实现