首頁  >  文章  >  web前端  >  分享7種實現CSS垂直居中的技巧(附代碼)

分享7種實現CSS垂直居中的技巧(附代碼)

yulia
yulia原創
2018-09-15 16:18:031706瀏覽

網頁CSS的垂直居中需求始終沒有停過,而其困難度也始終沒有讓人輕鬆過,經過了每位開發先烈的研究後,據說CSS的垂直居中技巧已達到近十種之多,但始終鮮為人知,部分公司甚至將CSS的垂直居中技巧當成面試題,其重要性可見一斑,今天就帶著大家了解一下CSS的垂直居中的多種方式吧。

1、Line-height

適用情境:單行文字垂直居中技巧

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

<div class="content">Lorem ipsam.</div>
.content{
  width: 400px;
  background: #ccc;
  line-height:100px;
  margin: auto;
}

2、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;
}

 3、:before inline-block

適用情境:多重物件的CSS垂直置中技巧

: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:&#39;&#39;;
  display: inline-block;
  height: 100%;
  width: 0;
  vertical-align: middle;
}
.box .content{
  width: 400px;
  background: #ccc;
  display: inline-block;
  vertical-align: middle;
}

 4、absolute margin 負值

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

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

<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;
}

5、absolute margin auto

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

又是一個絕對定位的垂直居中的方案,這個方式比較特別一點,當元素設定為絕對定位後,假設它是抓不到整體可運用的空間範圍,所以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;
}

6、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;
}

 7、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;
}

總結:以上給大家介紹了7種CSS垂直居中技巧,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對PHP中文網路的支持!

以上是分享7種實現CSS垂直居中的技巧(附代碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn