方法一:flex布局
<div class="container">
<div class="main">main</div>
</div>
.container{
display:flex;
justify-content:center;
align-items: center;
width:300px;
height:300px;
border: 5px solid #ccc;
}
.main{
background: red;
}
方法二: flex布局 + margin
<div class="container">
<div class="main">main</div>
</div>
.container{
display:flex;
width:300px;
height:300px;
border: 5px solid #ccc;
}
.main{
margin: auto;
background: red;
}
方法三: 相对定位
<div class="container">
<div class="main">main</div>
</div>
.container{
position: relative;
width:300px;
height:300px;
border: 5px solid #ccc;
}
.main{
position: absolute;
left: 50%;
top: 50%;
transforn: translate(-50%,-50%);
background: red;
}
方法四: 利用 display:table-cell
<div class="container">
<div class="main">main</div>
</div>
.container{
display: table-cell;
text-align: center;
vertical-align: middle;
width:300px;
height:300px;
border: 5px solid #ccc;
}
.main{
display: inline-block;
background: red;
}
版权声明:本文为qq_42744920原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。