1-css的多種垂直居中的方法

多種css垂直居中的方法

==css3不定寬高水平垂直居中==

-webkit-justify-content:center;
justify-content:center;//子元素水平居中
-webkit-align-items:center;
align-items:center;//子元素垂直居中
display:-webkit-flex;
display:flex;

1:固定高寬的垂直居中

如上圖,固定高寬的很簡單,寫法如下:

position: absolute;
left: 50%;
top: 50%;
width:200px;
height:100px;
margin-left:-100px;
margin-top:-50px;

2: 不固定高寬div垂直居中的方法

方法一:
用一個(gè)“ghost”偽元素(看不見的偽元素)和 inline-block / vertical-align 可以搞定居中,非常巧妙。但是這個(gè)方法要求待居中的元素是 inline-block,不是一個(gè)真正通用的方案。
html如下:

<div class="block" style="height: 300px;">

    <div class="centered">
        <h1>haorooms案例題目</h1>
        <p>haorooms案例內(nèi)容,haorooms案例內(nèi)容haorooms案例內(nèi)容haorooms案例內(nèi)容haorooms案例內(nèi)容haorooms案例內(nèi)容haorooms案例內(nèi)容haorooms案例內(nèi)容haorooms案例內(nèi)容</p>
    </div>

</div>
css如下:
/* This parent can be any width and height */
.block {
  text-align: center;
}

/* The ghost, nudged to maintain perfect centering */
.block:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */
}

/* The element to be centered, can
   also be of any width and height */ 
.centered {
  display: inline-block;
  vertical-align: middle;
  width: 50%;
}

方法二:table布局

<table style="width: 100%;">
  <tr>
     <td style="text-align: center; vertical-align: middle;">
          Unknown stuff to be centered.
     </td>
  </tr>
</table>
//由于table寫法比較費(fèi)時(shí),你也可以用div代替table,寫法如下:
//html:
<div class="something-semantic">
   <div class="something-else-semantic">
       Unknown stuff to be centered.
   </div>
</div>

css:

.something-semantic {
   display: table;
   width: 100%;
}
.something-else-semantic {
   display: table-cell;
   text-align: center;
   vertical-align: middle;
}

方法三: 終極解決方法

以上2中方法可能都有其局限性,我介紹的第三中方法是比較成熟的不是固定高寬div的垂直居中的方法!但是方法是css3的寫法,想兼容IE8的童鞋們,建議用上面的方法!
方法和我們固定高寬的差不多,但是不用margin我們用的是 translate()

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>haorooms不固定高度div寫法</title>

    <style>
.center {
  position: fixed;
  top: 50%;
  left: 50%;
  background-color: #000;
  width:50%;
  height: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
}


    </style>
</head>
<body>
    <div class="center"></div>
</body>
</html>

我上面的css只是針對(duì)webkit內(nèi)核的瀏覽器,其他內(nèi)核瀏覽器寫法如下:

-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);

關(guān)于每個(gè)瀏覽器的寫法,可以看看我之前的一篇文章:http://www.haorooms.com/post/css_common
有些彈出層的樣式,也可以用這個(gè)方法居中

position: fixed;
top: 50%;
left: 50%;
width: 50%;
max-width: 630px;
min-width: 320px;
height: auto;
z-index: 2000;
visibility: hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
運(yùn)用margin:auto進(jìn)行垂直居中
//實(shí)現(xiàn)html如下:(做一個(gè)簡單的垂直彈框)

<div class="father">
    <div class="son"></div>
</div>
//css代碼如下,很簡單,兼容性也蠻好,支持IE8+

.father{
    position:fixed;
    width:100%;
    height:100%;
    top;0;left:0;
    background-color:rgba(0,0,0,.7);
    }
.son{
    position: 
    absolute;top:0;left:0;bottom:0;right:0;
    width:50%;height:50%;
    margin:auto;
    background-color:red;
    }

margin的值設(shè)置為auto,可以讓我們對(duì)剩余空間進(jìn)行分配!我們知道,塊級(jí)元素設(shè)置為margin:0 auto;可以左右居中顯示!那有沒有辦法讓margin設(shè)置為margin:auto之后,上下左右都居中呢?上下左右都居中,就可以實(shí)現(xiàn)我們的垂直居中了!

==答案是有的,只要我們讓上下有足夠的空間,就可以讓margin的auto來分配上下空間。
我們可以利用定位的方式,讓margin上下左右都有足夠的空間!那么就可以用margin:auto來實(shí)現(xiàn)垂直居中了==

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

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

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,108評(píng)論 1 92
  • 收聽音頻,戳鏈接,舊號(hào)itclan已暫停使用,歡迎關(guān)注微信itclanCoder公眾號(hào)可收聽更多音頻 前言 關(guān)于網(wǎng)...
    itclanCoder閱讀 8,346評(píng)論 3 30
  • 一 外部式css樣式 (也可稱為外聯(lián)式)就是把css代碼寫一個(gè)單獨(dú)的外部文件中,這個(gè)css樣式文件以“.css...
    KunMitnic閱讀 1,119評(píng)論 0 1
  • 本文主要是起筆記的作用,內(nèi)容來自慕課網(wǎng). 認(rèn)識(shí)CSS樣式 CSS全稱為“層疊樣式表 (Cascading Styl...
    0o凍僵的企鵝o0閱讀 2,749評(píng)論 0 30
  • 只知孔子禮,不知莊子游,豈不笑哉
    奔跑的方便面閱讀 155評(píng)論 0 0

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