s

子div在父div中垂直居中的几种方法

<div class="parent">
  <div class="child"></div>
</div>



<!-- 1 --> div.parent { display: flex; justify-content: center; align-items: center; } <!-- 2 --> div.parent { position: relative; } div.child { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } <!-- 3 --> div.parent { display: grid; } div.child { justify-self: center; align-self: center; } <!-- 4 --> div.parent { font-size: 0; text-align: center; &::before { content: ""; display: inline-block; width: 0; height: 100%; vertical-align: middle; } } div.child { display: inline-block; vertical-align: middle; }

 

posted @ 2021-06-11 13:08  努力不搬砖的iori  阅读(428)  评论(0编辑  收藏  举报