<div class="father">
<div class="child">
</div>
</div>
child分很多种情况
.father{ width:300px; height:300px; text-align:center; line-height:300px; }
定宽定高:absolute + margin
.father{ position: relative; } .child{ position: absolute; top: 50%; left: 50%; margin: -150px 0 0 -150px; } /* 或者 */ .child{ position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; }
不定高:absolute + translate
.father{ position: relative; } .child{ position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); }
.father{ display: flex; } .child{ margin:auto; } /* 或者 */ .child{ justify-content: center; align-items: center; }
.father{ display: table; } .child{ display: table-cell; text-align: center; vertical-align: middle; }
原文:https://www.cnblogs.com/wyryuebanwan/p/10595436.html