垂直居中

https://jscode.me/t/topic/1936

如果 .parent 的 height 不寫,你只需要 padding: 10px 0; 就能將 .child 垂直居中;

如果 .parent 的 height 寫死了,就很難把 .child 居中,以下是垂直居中的方法。

忠告:能不寫 height 就千萬別寫 height。

1.table自帶功能

<table class="parent">

? ? <tr>? ? ? <td class="child">? ? 一串文字一串文字一串文字一串文字? ? </td>? ? </tr>

? </table>

.parent{? height: 600px;}

2.div 裝成 table

<div class="table">

<div class="td"> <div class="child">一串文字一串文字一串文字 </div></div>

? </div>

div.table{? display: table;? height: 600px;}

div.td{? display: table-cell;? vertical-align: middle;}

3.margin-top -50%

<div class="parent">? <div class="child">一串文字一串文字</div>? </div>

.parent{ height: 600px;? position: relative;}

.child{? ?position: absolute;? top: 50%; margin-top: 負(fù)的高度一半;}

4.translate -50%

<div class="parent"> <div class="child">? 一串文字一串文字 </div></div>

.parent{ height: 600px;? position: relative;}

.child{? position: absolute;? top: 50%; transform: translateY(-50%);}

5.absolute margin auto

<div class="parent"> <div class="child">? 一串文字一串文字 </div></div>

.parent{? height: 600px; position: relative;}

.child{? position: absolute;? height: 200px;? margin: auto;? top: 0;? bottom: 0;? left: 0;? right: 0;}

6.flex

<div class="parent"> <div class="child">? 一串文字一串文字 </div></div>

.parent{? height: 600px;? display: flex;? justify-content: center;? align-items: center;}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容