通常首選方法是使用flexbox居中內容。只需三行代碼即可:display:flex,然后使用 align-items:center 和 justify-content:center 將子元素垂直和水平居中。
如下代碼:
html:
<div class="flexbox-centering">
<div>Centered content.</div>
</div>
css:
.flexbox-centering {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
}
使用grid(網格)與flexbox非常相似,也是一種常見的技術,尤其是布局中已經使用網格的情況下。與前一種flexbox技術的唯一區別是它顯示為柵格。
如下代碼:
html:
<div class="grid-centering">
<div class="child">Centered content.</div>
</div>
css:
于html的元素居中其實只有兩類居中,一是塊元素居中,二是內聯元素居中。
內聯元素居中使用text-align,塊元素居中使用margin。
一、內聯元素
內聯元素居中,使用原則就是在其父元素上設置text-margin:center。
例如<span>元素居中
網頁上顯示效果為:
另外<a>標簽也是一樣
網頁上顯示效果為:
二、塊元素
塊元素居中,使用原則就是在需要居中的元素上設置margin:0 auto。
例如<div>元素居中
網頁上顯示效果為:
另外,如果塊元素已經浮動(例如float:left)居中方法
例如已經浮動了的<div>元素
網頁上顯示效果為:
上面代碼意義:
因為已經浮動了的緣故,所以采用margin:0 auto對元素已經不起作用了,所以采用position:relative,然后利用位 置top:50%與left:50%可以將元素居中,但是此時是以元素的左上角為參考點。
實際情況元素向右和向下平移了元素寬度與高度的一半,所以后面還需要加上margin:0 -50px(這個例子高度的一半為50px,以實際情況為準),如果垂直方向也要居中的話,就將0改為高度一半的相反數。
對前端感興趣的小伙伴記得關注小編,每天都會更新前端的一些小技巧。沒點贊的小伙伴記得點贊收藏哦,謝謝大家!
幾天有小伙伴在我的web前端交流群里說前幾天面試一家公司,被問到垂直居中的方法,只答出了margin、table-cell、flex三種,然后就不知道了,然后問我除此之外還有哪些?,于是我整理了一下居中的方案做個記錄,希望對大家也有幫助。
如果哪里寫的不對,歡迎指正,非常感謝。
塊級元素居中 html代碼部分
<div class="parent"> <div class="child">child</div> </div>
行內元素居中 html代碼部分
<div class="parent"> <span class="child">child</span> </div>
水平居中
01 行內元素 text-align: center;
.parent { text-align: center; }
02 塊級元素 margin: auto;
低版本瀏覽器還需要設置 text-align: center;
.parent { text-align: center; } .child { width: 100px; margin: auto; border: 1px solid blue; }
由于本文主要想記錄的是垂直居中的方案,這里水平垂直的其他方案就不做過多記錄了。
垂直居中
01 行內元素(單行文字垂直居中):設置 line-height = height
.parent { height: 200px; line-height: 200px; border: 1px solid red; }
02 塊級元素:絕對定位(需要提前知道尺寸)
.parent { position: relative; height: 200px; } .child { width: 80px; height: 40px; background: blue; position: absolute; left: 50%; top: 50%; margin-top: -20px; margin-left: -40px; }
03 塊級元素:絕對定位 + transform
.parent { position: relative; height: 200px; } .child { width: 80px; height: 40px; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); background: blue; }
04 塊級元素:絕對定位 + margin: auto;
此方法出自張鑫旭老師的博客 小tip: margin:auto實現絕對定位元素的水平垂直居中
.parent { position: relative; height: 200px; } .child { width: 80px; height: 40px; position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; background: blue; }
05 塊級元素:padding
.parent { padding: 5% 0; } .child { padding: 10% 0; background: blue; }
06 塊級元素:display: table-cell
.parent { width: 600px; height: 200px; border: 1px solid red; display: table; } .child { display: table-cell; vertical-align: middle; }
或:
這個方案是在知乎看到的,原文說是淘寶團隊的方案:
用 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; }
07 塊級元素:display: flex
.parent { width: 600px; height: 200px; border: 1px solid red; display: flex; align-items: center; justify-content: center; /*水平居中*/ } .child { background: blue; }
08 塊級元素:偽元素
這個方案是先從這位博主的文章中看到:
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; }
09 塊級元素:calc()
也是個不錯的方法。
.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; }
10 塊級元素:inline-block
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等標簽,冗余有點多,而且結構也改變了。
好啦,分享到這里也就結束了,下次見。
關注我的頭條號,分享更多的技術學習文章,我自己是一名從事了多年開發的web前端老程序員,目前辭職在做自己的web前端私人定制課程,今年年初我花了一個月整理了一份最適合2019年學習的web前端學習干貨,各種框架都有整理,送給每一位前端小伙伴,想要獲取的可以關注我的頭條號并在后臺私信我:前端,即可免費獲取。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。