前幾天面試一家公司,被問到垂直居中的方法,我只答出了margin、table-cell、flex三種。回來以後以爲特別慚愧,因而整理了一下居中的方案作個記錄,但願對你們也有幫助。
若是哪裏寫的不對,歡迎指正,很是感謝。html
塊級元素居中 html代碼部分web
<div class="parent"> <div class="child">child</div> </div>
行內元素居中 html代碼部分面試
<div class="parent"> <span class="child">child</span> </div>
.parent { text-align: center; }
(低版本瀏覽器還須要設置 text-align: center;)瀏覽器
.parent { text-align: center; } .child { margin: auto; border: 1px solid blue; }
因爲本文主要想記錄的是垂直居中的方案,這裏水平垂直的其餘方案就不作過多記錄了。wordpress
.parent { height: 200px; line-height: 200px; border: 1px solid red; }
優勢:兼容性不錯
缺點:須要提早知道尺寸,margin-top: -(高度的一半); margin-left: -(寬度的一半);佈局
.parent { position: relative; height: 200px; } .child { width: 80px; height: 40px; background: blue; position: absolute; left: 50%; top: 50%; margin-top: -20px; margin-left: -40px; }
優勢:不須要提早知道尺寸
缺點:兼容性很差post
.parent { position: relative; height: 200px; } .child { width: 80px; height: 40px; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); background: blue; }
優勢:不須要提早知道尺寸,兼容性好
缺點:這個方法是我最喜歡用的一個,要說缺點的話,我目前還不知道。
此方法出自張鑫旭老師的博客 小tip: margin:auto實現絕對定位元素的水平垂直居中flex
.parent { position: relative; height: 200px; } .child { width: 80px; height: 40px; position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; background: blue; }
缺點:若是高度固定,須要提早計算尺寸(只在某些特定狀況適用)。spa
.parent { padding: 5% 0; } .child { padding: 10% 0; background: blue; }
.parent { width: 600px; height: 200px; border: 1px solid red; display: table; } .child { display: table-cell; vertical-align: middle; }
或:code
這個方案是在知乎看到的,原文說是淘寶團隊的方案:
用 CSS 實現元素垂直居中,有哪些好的方案? - Gino的回答 - 知乎
張鑫旭老師的博客也有提到過:
我所知道的幾種display:table-cell的應用
.parent { height: 300px; border: 1px solid red; display: table-cell; vertical-align: middle; /* *display: block; *font-size: (heightX0.873); *font-family: arial; */ }
一樣適用於多行文字的垂直居中處理
HTML代碼:
<div class="parent"> <span class="child">child child child child child child child child child child child child child child child child child child child childchild child child </span> </div>
CSS代碼:
.parent { width: 400px; height: 300px; display: table-cell; vertical-align: middle; border: 1px solid red; } .child { display: inline-block; vertical-align: middle; background: blue; }
缺點:兼容性很差
.parent { width: 600px; height: 200px; border: 1px solid red; display: flex; align-items: center; justify-content: center; /*水平居中*/ } .child { background: blue; }
這個方案是先從這位博主的文章中看到:
CSS:使用僞元素作水平垂直居中的微深刻研究
而後發現張鑫旭老師的文章中也有提到:
:after僞類+content內容生成經典應用舉例
.parent { width: 300px; height: 300px; border: 1px solid red; text-align: center; } .child { background: blue; width: 100px; height: 40px; display: inline-block; vertical-align: middle; } .parent::before { content: ''; height: 100%; display: inline-block; vertical-align: middle; }
並不推薦,但也是個方法。
缺點:兼容性太差,須要計算。
.parent { width: 300px; height: 300px; border: 1px solid red; position: relative; } .child { width: 100px; height: 100px; background: blue; padding: -webkit-calc((100% - 100px) / 2); padding: -moz-calc((100% - 100px) / 2); padding: -ms-calc((100% - 100px) / 2); padding: calc((100% - 100px) / 2); background-clip: content-box; }
HTML代碼:
<div class="parent"> <div class="child">child</div> <div class="brother">brother</div> </div>
CSS代碼:
.parent { width: 400px; height: 400px; border: 1px solid red; position: relative; } .child, .brother { display: inline-block; vertical-align: middle; } .child { background: blue; font-size: 12px; } .brother { height: 400px; font-size: 0; }
固然,還有一種方法,就是使用table佈局:
<table> <tr> <td align="center" valign="middle">content</td> </tr> </table>
由於html還要加table等標籤,冗餘有點多,並且結構也改變了。
做者:Suplum 連接:https://juejin.im/post/5a7a9a545188257a892998ef 來源:掘金 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。