這篇文章主要介紹了css如何讓div垂直居中,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、賀州網(wǎng)絡(luò)推廣、小程序開(kāi)發(fā)、賀州網(wǎng)絡(luò)營(yíng)銷(xiāo)、賀州企業(yè)策劃、賀州品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供賀州建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):13518219792,官方網(wǎng)址:jinyejixie.com
css讓div垂直居中的方法:1、使用絕對(duì)定位和負(fù)外邊距進(jìn)行居中;2、利用偽元素和inline-block、vertical-align進(jìn)行居中;3、利用table布局進(jìn)行居中;4、使用定位和transform。
本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。
我們都知道,固定高寬的div在網(wǎng)頁(yè)中垂直居中很簡(jiǎn)單,相信大家也很容易的寫(xiě)出來(lái),但是不是固定高寬的div如何垂直居中呢?我們?cè)诰W(wǎng)頁(yè)布局,特別是手機(jī)等web端網(wǎng)頁(yè)經(jīng)常是不固定高寬的div,那么這些div如何垂直居中呢?這篇文章,我總結(jié)一下。
如上圖,固定高寬的很簡(jiǎn)單,寫(xiě)法如下:
undefined
position: absolute; left: 50%; top: 50%; width:200px; height:100px; margin-left:-100px; margin-top:-50px;
方法一:偽元素和inline-block、vertical-align
用一個(gè)“ghost”偽元素(看不見(jiàn)的偽元素)和 inline-block / vertical-align 可以搞定居中,非常巧妙。但是這個(gè)方法要求待居中的元素是 inline-block,不是一個(gè)真正通用的方案。
html如下:
<div class="block" style="height: 300px;"> <div class="centered"> <h2>haorooms案例題目</h2> <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布局方法,但是這種方法也有局限性!
寫(xiě)法如下:
<table style="width: 100%;"> <tr> <td style="text-align: center; vertical-align: middle;"> Unknown stuff to be centered. </td> </tr> </table>
由于table寫(xiě)法比較費(fèi)時(shí),你也可以用div代替table,寫(xiě)法如下:
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的寫(xiě)法,想兼容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寫(xiě)法</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></div> </body> </html>
我上面的css只是針對(duì)webkit內(nèi)核的瀏覽器,其他內(nèi)核瀏覽器寫(xiě)法如下:
-webkit-transform: translateX(-50%) translateY(-50%); -moz-transform: translateX(-50%) translateY(-50%); -ms-transform: translateX(-50%) translateY(-50%); transform: translateX(-50%) translateY(-50%);
有些彈出層的樣式,也可以用這個(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%);
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“css如何讓div垂直居中”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
本文名稱(chēng):css如何讓div垂直居中
鏈接URL:http://jinyejixie.com/article4/gdgpoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、虛擬主機(jī)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)