*{
padding: 0;
margin: 0;
}
.parent_1{
width: 200px;
height: 200px;
background: red;
position: relative;
margin-bottom: 10px;
}
.child_1{
width: 100px;
height: 100px;
background: #000;
position: absolute;
left:0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
.parent_2{
width: 200px;
height: 200px;
background: red;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.child_2{
width: 100px;
height: 100px;
background: #000;
display: inline-block;
}
.parent_3{
width: 200px;
height: 200px;
background: red;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
}
.child_3{
width: 100px;
height: 100px;
background: #000;
}
.parent_4{
width: 200px;
height: 200px;
background: red;
position: relative;
margin-top: 10px;
}
.child_4{
width: 100px;
height: 100px;
background: #000;
position: absolute;
left:50%;
top:50%;
margin-left:-50px;
margin-top:-50px
}
.parent_5{
width: 200px;
height: 200px;
background: red;
margin-top: 10px;
display: -webkit-box;
-webkit-box-align: center;
-webkit-box-pack: center;
}
.child_5{
width: 100px;
height: 100px;
background: #000;
}
<!--第一种方法 left bottom top right 0导致混乱后用margin:auto直接水平垂直居中-->
<div class="parent_1">
<div class="child_1">
</div>
</div>
<!--第二种方法利用display:table-cell布局 display:inline-block布局-->
<div class="parent_2">
<div class="child_2">
</div>
</div>
<!--flex布局 justify-content:center align-items:center-->
<div class="parent_3">
<div class="child_3">
</div>
</div>
<!--第四种方法根据定位减去宽高-->
<div class="parent_4">
<div class="child_4">
</div>
</div>
<!--弹性盒模型居中-->
<div class="parent_5">
<div class="child_5">
</div>
</div>
五种小div在大div中水平垂直居中方法
坚持原创技术分享,您的支持将鼓励我继续创作!