居中的方法 二十餘種 有點吃鯨

一、Line-height 適用情景:單行文字垂直居中技巧 這個方式應該是最多人知道的了,常見於單行文字的應用,像是按鈕這一類對象,或者是下拉框、導航此類元素最多見到的方式了。此方式的原理是在於將單行文字的行高設定後,文字會位於行高的垂直中間位置,利用此原理就能輕鬆達成垂直居中的需求了。css

<div class="content">Lorem ipsam.</div> .content{ width: 400px; background: #ccc; line-height:100px; margin: auto; } 二、Line-height + inline-block 適用情景:多對象的垂直居中技巧 既然可使用第一種方式對行元素達成垂直居中的話,固然沒有理由不能作到多行啊~可是你須要將多個元素或多行元素當成一個行元素來看待,因此咱們必需要將這些數據多包一層,並將其設定爲inline-block,並在該inline-block對象的外層對象使用inline-block來代替height的設置,如此即可以達到垂直居中的目的了,從使你的數據是包含了標題跟內容在內也能夠正常的垂直居中了。 <div class="box box2"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; border: 1px solid #f00; margin: auto; line-height: 200px; text-align: center; } .box2 .content{ display: inline-block; height: auto; line-height:1; width: 400px; background: #ccc; } 三、:before + inline-block前端

適用情景:多對象的CSS垂直居中技巧css3

:before 僞類元素搭配 inline-block 屬性的寫法應該是很傳統的垂直居中的技巧了,此方式的好處在於子元素居中能夠不須要特別設定高度,咱們將利用:before僞類元素設定爲100%高的inline-block,再搭配上將須要居中的子元素一樣設置成inline-block性質後,就能使用vertical-align:middle來達到垂直居中的目的了,此方式在以往實際上是個很是棒的垂直居中解決方案,惟獨須要特別處理掉inline-block元素之間的4-5px空間這個小缺陷,但也很實用了。後端

<h2>3.:before + inline-block</h2> <div class="box box3"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; text-align: center; } .box::before{ content:''; display: inline-block; height: 100%; width: 0; vertical-align: middle; } .box .content{ width: 400px; background: #ccc; display: inline-block; vertical-align: middle; } 四、absolute + margin 負值瀏覽器

適用情景:多行文字的垂直居中技巧性能

誰說絕對定位要少用?Amos認爲沒有少用多用的問題,重點在於你是否有妥善運用纔是重點,絕對定位在這個例子中會設置top:50%來抓取空間高度的50%,接着在將居中元素的margin-top設定爲負一半的高度,這樣就能讓元素居中了,此方法但是自古以來流傳多年的居中方式呢?flex

<h2>4.absolute + margin 負值</h2> <div class="box box4"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; position: relative; } .box4 .content{ width: 400px; background: #ccc; height: 70px; position: absolute; top:50%; left: 50%; margin-left: -200px; margin-top: -35px; } 五、absolute + margin auto設計

適用情景:多行文字的垂直居中技巧orm

又一個絕對定位的垂直居中的方案,這個方式比較特別一點,當元素設置爲絕對定位後,假設它是抓不到總體可運用的空間範圍,因此margin:auto會失效,但當你設置了top:0;bottom:0;時,絕對定位元素就抓到了可運用的空間了,這時你的margin:auto就生效了(神奇吧),若是你的絕對定位元素須要水平居中於父層,那你一樣能夠設定left:0;right:0;來讓絕對定位元素取得空間可運用範圍,再讓marign-left與margin-right設定爲auto便可居中。但此方式的缺點是你的定位元素必須有固定的寬高(百分比也算)才能正常居中。對象

<h2>5.absolute + translate(-50%, -50%)</h2> <div class="box box5"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; position: relative; } .content{ width: 400px; background: #ccc; height: 70px; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; } 六、absolute + translate

適用情景:多行文字的垂直居中技巧

在一個絕對定位居中的方式,此方式應該算是最方便的了,由於此居中的定位元素不須要固定的寬高,咱們利用絕對定位時的top 與right設置元素的上方跟左方各爲50%,再利用translate(-50%,-50%)位移居中元素自身寬與高的50%就能達成居中的目的了。(css3好棒)

<h2>6.absolute + margin: auto</h2> <div class="box box6"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; position: relative; } .box5 .content{ width: 400px; background: #ccc; position: absolute; top:50%; left: 50%; transform: translate(-50%, -50%); } Flex + align-items

適用情景:多行文字的垂直居中技巧

Flex!前端的毒品!後端的寶物!這東西自從面世以後就不斷的考驗網頁開發者的良心,到底要不要拋棄float擁抱flex,我想這答案人人心中自由一把尺,但先碰Flex再碰Float可謂先甜後苦,這順序到底要倒吃甘蔗仍是正吃甘蔗是實在難說,自從有了Flex以後,小孩考試一百分,設計網頁不跑版,客戶網頁都RWD,老闆賺錢好開心,我也加薪(加班)好甘心,不禁的說Flex真的是一個神物,咱們只要設定父層display:flex以及設定次軸(cross axis)屬性align-items:center 就行了(說那麼多結果重點就一行字是哪招啦),這個方式的優勢是此層不須要設定高度便可自動居中,且原始代碼乾淨無比,真的是用一次就讓你昇天啦。

<h2>7.Flex + align-items</h2> <div class="box box7"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; justify-content: center; align-items: center; } .content{ width: 400px; background: #ccc; } 八、Flex + :before + flex-grow

適用情景:多行文字的垂直居中技巧

Flex有多種方式可讓你把數據居中,適用Flex-grow的延展特性來達成,這個例子中Amos適用了flex-direction:column直式排法,搭配:before僞元素適用flex-grow伸展值可以取得剩下全部空間的特性,把它設定成一半的剩餘空間就能作到把內容數據準確的推到垂直中間位置,算是個傳統技法的延伸方式。這樣的話上面第七個方式不是比較快?

<h2>8.Flex + before + flex-grow</h2> <div class="box box8"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; flex-direction: column; align-items: center; } .box:before{ content: ''; flex-grow: .5; } .content{ width: 400px; background: #ccc; } 九、Flex + margin

適用情景:多行文字的垂直居中技巧

繼續用Flex來居中,因爲Flex元素對空間解讀的特殊性,咱們只要在父層元素設定display:flex,接着在須要垂直居中的元素上設定margin:auto,便可自動居中

<h2>9.Flex + margin</h2> <div class="box box9"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; } .content{ width: 400px; background: #ccc; margin: auto; } 十、Flex + align-self

適用情景:多行文字的垂直居中技巧

align-self 應該你們都不陌生,基本上就是對flex次軸cross axis 的個別對齊方式只要對單一子層元素設定align-self:center就能達成垂直居中的目的了。

<h2>10.Flex + align-self</h2> <div class="box box10"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; justify-content: center; } .content{ width: 400px; background: #ccc; align-self: center } 十一、Flex + align-content

適用情景:多行文字的垂直居中技巧

在正常的情況下,align-content 僅能對次軸多行flex item作居中,可是當我今天子層元素不肯定有多少個時,且有時可能會有單個的狀況出現時,此技巧就能用到了(固然你也能有其餘解法),既然是多行子元素才能用,那咱們就爲單個子組件多加兩個兄弟吧,使用:before以及:after 來讓子元素增長到多個,這樣就能使用flex的align-content屬性來居中

<h2>11.Flex + align-content</h2> <div class="box box11"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; flex-wrap: wrap; justify-content: center; align-content: center; } .content{ width: 400px; background: #ccc; } .box11:before, .box11:after{ content: ''; display: block; width:100%; } 十二、Grid + template

適用情景:多行文字的垂直居中技巧

CSS Grid最使人驚訝的就是這個template的功能了,簡直就是把塊元素當畫布在使用,咱們僅須要把模板設置成三列,就能搞定垂直居中了

<h2>12.Grid + template</h2> <div class="box box12"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; grid-template-rows: 1fr auto 1fr; grid-template-columns: 1fr auto 1fr; grid-template-areas: '. . .' '. amos .' '. . .'; } .content{ width: 400px; background: #ccc; grid-area: amos; } 1三、Grid + align-items

適用情景:多行文字的垂直居中技巧

align-items不只是Flex可用,連CSS Grid也擁有此屬性可以使用,但在Flex中align-items是針對次軸cross axis做對齊,而在CSS Grid中則是針對Y軸作對齊,你能夠把它想象成是表格中單元格的vertical-align屬性看待,就能夠很好理解了

<h2>13.Grid + align-items</h2> <div class="box box13"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; justify-content: center; align-items: center; } .content{ width: 400px; background: #ccc; } 1四、Grid + align-content

適用情景:杜航文字的垂直居中技巧

CSS Grid的align-content跟Flex的align-content有點差別,CSS Grid對於空間的解釋會跟Flex有一些些的落差,因此致使align-content在Flex中僅能針對多行元素起做用,但在Grid中就沒這個問題,因此咱們能夠很開心的使用align-content來對子元素作垂直居中,絲絕不費力氣

<h2>14.Grid + align-content</h2> <div class="box box14"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; justify-content: center; align-content: center; } .content{ width: 400px; background: #ccc; } 1五、Grid + align-self

適用情景:多行文字的垂直居中技巧

align-self 應該你們都不陌生,基本上就是對grid Y軸的個別對齊方式,只要對單一子層元素設置爲align-self:center就能達成垂直居中的目的了

<h2>15.Grid + align-self</h2> <div class="box box15"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; justify-content: center; } .content{ width: 400px; background: #ccc; align-self: center; } 1六、Grid + place-items

適用情景:多行文字的垂直居中技巧

place-items這屬性不知道有多少人用過,此屬性是align-items與justify-items的縮寫,簡單的說就是水平與垂直的對齊方式,想固然的,設定center就能居中

<h2>16.Grid + place-items</h2> <div class="box box16"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; height: 150px; margin: 0 auto; place-items: center; } .content{ width: 400px; background: #ccc; } 1七、Grid + place-content

適用情景:多行文字的垂直居中技巧

place-content這屬性有多少人用過,此屬性是align-content與justify-content的縮寫,簡單的說就是水平與垂直的對齊方式,想固然的,設置center就能居中了

<h2>17.Grid + place-content</h2> <div class="box box17"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; height: 150px; margin: 0 auto; place-content: center; } .content{ width: 400px; background: #ccc; } 1八、Grid + margin

適用情景:多行文字的垂直居中技巧

繼續用Grid來居中,因爲Grid元素對空間解讀的特殊性,咱們只要在父層元素設定display:grid,接着在須要垂直居中的元素上設置margin:auto便可自動居中。怎麼這描述似曾相識。

<h2>18.Grid + margin</h2> <div class="box box18"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; } .content{ width: 400px; background: #ccc; margin:auto; } 1九、Display:table-cell

適用情景:多行文字的垂直居中技巧

這一招我想有點年紀的開發者應該都有看過,固然像我這麼嫩的開發者固然是第一次看到啦,這一招的原理在於使用 CSS display屬性將div設置成表格的單元格,這樣就能利用支持存儲單元格對齊的vertical-align屬性來將信息垂直居中

<h2>19.display: table-cell</h2> <div class="box box19"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; text-align: center; display: table-cell; vertical-align: middle; } .content{ width: 400px; background: #ccc; margin: auto; } 20、calc

適用情景:多行文字的垂直居中技巧

Cale是計算機英文單詞calculator的縮寫,這個由微軟提出的css 方法,真的是網頁開發者的一個福音。咱們居然能夠在網頁中直接作計算,這真是太猛了,今後咱們不再用在那邊絞盡腦汁的數學計算了,或是想辦法用js來動態計算,咱們能夠很輕鬆的利用calc()這個方法,來將百分比及時且動態的計算出實際要的是什麼高度,真可謂是劃時代的一個方法啊,但這個方法須要注意的是大量使用的話,網頁性能會是比較差的,因此請謹慎使用。

<h2>20.calc</h2> <div class="box box20"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; } .content{ width: 400px; background: #ccc; position: relative; top:calc((100% - 70px) / 2); margin:auto; height: 70px; } 2一、Relative + translateY

適用情景:多行文字的垂直居中技巧

這個技巧是利用了top:50%的招式,讓你的元素上方能產生固定百分比的距離,接着讓要居中的元素自己使用tanslateY的百分比來達成垂直居中的需求,translate是一個很棒的屬性,因爲translate的百分比單位是利用元素自身的尺寸做爲100%,這樣讓咱們要利用元素自身寬高作事變得方便不少。

<h2>21.relative + translateY(-50%)</h2> <div class="box box21"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; } .content{ width: 400px; background: #ccc; position: relative; top: 50%; transform: translateY(-50%); margin: auto; } 2二、padding

適用情景:多行文字的垂直居中技巧

什麼!這也算垂直居中技巧,連我奶奶都知道這方式吧

對的,這的確也算是一種垂直居中的方式,不可諱言的這方式真的是簡單過頭了,以致於有些開發者認爲這種方式都不能算是一種垂直居中的技巧,但一樣的你沒法反駁的是,個人數據的確垂直居中啦,好啦,就當我硬凹吧,你說的對,好吧

<h2>22.padding</h2> <div class="box box22"> <div class="content"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div> </div> h2{ text-align: center; } .box{ width: 500px; border: 1px solid #f00; margin: auto; height: auto; padding: 50px 0; } .content{ width: 400px; background: #ccc; margin: auto; } 2三、Write-mode

適用情景:多行文字的垂直劇種技巧

這個方式應該是比較少見到的有人使用的了,這個想法是被老友Paul所激發的,write-mode這個css屬性的功能基本上跟垂直居中是八竿子打不着,它的用途是改變文字書寫的方向從橫變豎,且支持度從很早期的IE5就有支持了,但當時Amos不多使用,一來是網頁可能是橫書較多,另外當時除了IE瀏覽器意外,其餘瀏覽器的支持度都不是很好,也就不多使用了。

使用write-mode將一整個文字容器變成直書,接着將此容器利用text-align:center來達到垂直居中的目的,白話一點的解說就是,你把本來橫排的文字變成豎排,因此本來橫排用到的水平對齊方式,就變成了控制直排的中間了,原理就是這麼簡單。但要特別注意的是瀏覽器對此語法的支持度來講,須要拆開寫法才行,否則某些瀏覽器的語法不一樣,可能會讓你的網頁在某些瀏覽器上看起來無效,這會是最須要注意到的

<h2>23.writing-mode</h2>立馬來看Amos實際完成的 <div class="box box23"> <div class="content"> <div class="txt"> 立馬來看Amos實際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/"> CSS3精美相冊效果 </a> 效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! 這個置中的想法來自於 Paul </div> </div> </div> h2{ text-align: center; } .box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; writing-mode: tb-lr; /* for ie11 */ writing-mode: vertical-lr; text-align: center; margin:0 auto; } .content{ width: 400px; background: #ccc; display: inline-block; /* for ie & edge */ width: 100%; writing-mode: lr-tb; margin: auto; text-align: left; } .box .txt{ width: 80%; margin: auto; }

PS:以上內容僅供參考,若有疑問敬請指正。

相關文章
相關標籤/搜索