咱們都知道,固定高寬的div在網頁中垂直居中很簡單,相信你們也很容易的寫出來,可是不是固定高寬的div如何垂直居中呢?咱們在網頁佈局,特別是手機等web端網頁常常是不固定高寬的div,那麼這些div如

咱們都知道,固定高寬的div在網頁中垂直居中很簡單,相信你們也很容易的寫出來,可是不是固定高寬的div如何垂直居中呢?咱們在網頁佈局,特別是手機等web端網頁常常是不固定高寬的div,那麼這些div如何垂直居中呢?這篇文章,我總結一下。css

固定高寬div垂直居中

enter image description here

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

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

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

方法一:css3

用一個「ghost」僞元素(看不見的僞元素)和 inline-block / vertical-align 能夠搞定居中,很是巧妙。可是這個方法要求待居中的元素是 inline-block,不是一個真正通用的方案。web

html以下:瀏覽器

<div class="block" style="height: 300px;"> <div class="centered"> <h1>haorooms案例題目</h1> <p>haorooms案例內容,haorooms案例內容haorooms案例內容haorooms案例內容haorooms案例內容haorooms案例內容haorooms案例內容haorooms案例內容haorooms案例內容</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%; }

方法二:post

能夠用table佈局方法,可是這種方法也有侷限性!flex

寫法以下:ui

<table style="width: 100%;"> <tr> <td style="text-align: center; vertical-align: middle;"> Unknown stuff to be centered. </td> </tr> </table>

因爲table寫法比較費時,你也能夠用div代替table,寫法以下:spa

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()

demo以下:

<!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只是針對webkit內核的瀏覽器,其餘內核瀏覽器寫法以下:

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

關於每一個瀏覽器的寫法,能夠看看我以前的一篇文章:http://www.haorooms.com/post/css_common

有些彈出層的樣式,也能夠用這個方法居中

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%);

css3不定寬高水平垂直居中

只要三句話就能夠實現不定寬高水平垂直居中。

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

在父級元素上面加上上面3句話,就能夠實現子元素水平垂直居中

相關文章
相關標籤/搜索