純css實現垂直水平居中
6種方案
1、絕對定位+margin:auto
<style type="text/css">
.wrp {
background-color: #b9b9b9;
width: 240px;
height: 160px;
}
.box {
color: white;
background-color: #3e8e41;
width: 200px;
height: 120px;
overflow: auto;
}
.wrp1 { position : relative; }
.box1 {
margin: auto;
position: absolute;
left: 0; right: 0; top: 0; bottom: 0;
}
</style>
<div class="wrp wrp1">
<div class="box box1">
<h3>完全居中層1:</h3>
<h3>開發工具 【 WeX5 】: 高效能輕架構、開源免費、跨端、視覺化</h3>
</div >
</div>
效果:
實現原理:利用css定位規則,設定左右、上下方向定位為0,margin為auto,讓css根據定位計算margin值,用hack的方式實現居中。居中塊(綠色)的尺
寸需要可控,因為css計算margin時也需要參考尺寸值,由於四周為0,所以自動計算的尺寸是與父容器一樣的。無論是設定width、height或者是 max-
height、max-width,都是讓尺寸不會擴大到與父級一樣。
2、絕對定位+margin反向偏移
<style type="text/css">
.wrp2 { position: relative; }
.box2 {
position: absolute;
top: 50%; left: 50%;
margin-left: -100px; /* (width + padding)/2 */
margin-top: -75px; /* (height + padding)/2 */
}
</style>
<div class="wrp wrp2">
<div class="box box2">
<h3>完全居中方案二:</h3>
<h3>開發工具 【 WeX5 】: 高效能輕架構、開源免費、跨端、視覺化</h3>
</div>
</div>
效果:
實現原理:由於top、left偏移了父物件的50%高度寬度,所以需要利用margin反向偏移居中塊的50%寬高。而margin中不能使用百分比,因為百分比是針對
父物件的,所以需要手動計算定值指定margin值。這個方案需要固定尺寸值,以此來計算margin反向偏向值,所以方案2比方案1稍差!
3、絕對定位+transform反向偏移
<style type="text/css">
.wrp3 { position: relative; }
.box3 {
margin: auto;
position: absolute;
top: 50%; left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
<div class="wrp wrp3">
<div class="box box3">
<h3>完全居中方案三:</h3>
<h3>開發工具 【 WeX5 】: 高效能輕架構、開源免費、跨端、視覺化</h3>
</div>
效果:
實現原理:方案3與方案2原理一樣!不同點是使用了transform來代替margin做反向偏移,由於transform的計算基準是元素本身,所以這裡可以用50%來做反向偏移。這個方案也需要固定尺寸值,瀏覽器以此為基準來計算定位!
4、display:tabel
<style type="text/css">
.wrp4 { display: table; }
.subwrp4 {
display: table-cell;
vertical-align: middle;
}
.box4 {
margin: auto;
overflow-wrap: break-word;
height: auto;
max-height: 80%;
max-width: 80%;
}
</style>
<div class="wrp wrp4">
<div class="subwrp4">
<div class="box box4">
<h3>完全居中方案四:</h3>
</div>
</div>
</div>
效果:
實現原理:方案4是實現效果比較好的,居中塊的尺寸可以做包裹性,缺點是增加了一層table-cell層來實現垂直居中。方案4的居中塊可以設定 max-
height、max-width,而且居中塊是可以具有垂直方向的包裹性的。水平方向由於是在table-cell裡面的,所以會直接顯示max-width,也就是寬度趨大。
5、display: inline-block
<style type="text/css">
.wrp5 {
text-align: center;
overflow: auto;
}
.box5 {
display: inline-block;
vertical-align: middle;
width: auto;
height: auto;
max-width: 90%;
max-height: 90%;
}
.wrp5:after {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
margin-left: -0.25em;
/* To offset spacing. May vary by font */
}
</style>
<div class="wrp wrp5">
<div class="box box5">
<h3>完全居中方案五:</h3>
<h3>開發工具 【 WeX5 】: 高效能輕架構、開源免費、跨端、視覺化</h3>
</div>
</div>
效果:
實現原理:原理:利用inline-block的vertical-align: middle去對齊after偽元素,after偽元素的高度與父物件一樣,就實現了高度方向的對齊。方案5實現效果更加好,居中塊的尺寸可以做包裹性、自適應內容,相容性也相當好。缺點是水平居中需要考慮inline-block間隔中的留白(程式碼換行符遺留問題。)。方案4的居中塊可以設定 max-height、max-width,而且居中塊是可以具有水平垂直兩個方向的自適應。
6、display: flex-box
<style type="text/css">
.wrp6 {
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-box;
display: flex;
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
}
.box6 {
width: auto;
height: auto;
max-width: 90%;
max-height: 90%;
}
</style>
<div class="wrp wrp6">
<div class="box box6">
<h3>完全居中方案六:</h3>
<h3>開發工具 【 WeX5 】: 高效能輕架構、開源免費、跨端、視覺化</h3>
</div>
</div>
效果:
實現原理: flexbox佈局。此乃佈局終極大法,專治各種佈局定位難題!優點:能解決各種排列布局問題,實現方式符合人類認知。缺點:PC端某些舊瀏覽器支援度不高。
原文出處:http://www.codeceo.com/article/css-vertical-6-ways.html相關推薦
純css實現垂直水平居中
6種方案 1、絕對定位+margin:auto <style type="text/css"> .wrp { background-color: #b9b9b9; width: 240px; heig
css實現垂直水平居中的方法(個數不限)?
實現 容易 css3 pre height blog pad 絕對定位 fresh 方法一:使用絕對定位 大家都知道margin:0 auto;能夠實現水平居中,但卻不知道margin:0 auto;也是可以實現垂直居中的; 給居中元素添加如下樣式:
css實現垂直水平居中
otto div bsp 不能 水平 margin 垂直居中 auto absolut 我們經常用margin:0 auto來實現水平居中,而一直認為margin:auto不能實現垂直居中……實際上,實現垂直居中僅需要聲明元素高度和下面的CSS: .Absolute-Ce
css實現垂直水平居中的5種方法
進行 posit tran cto ans otto data -c ive css實現垂直水平居中的5種方法 給父元素設置table-cell,text-align,vertical-align #big{ width: 200px;
css 實現垂直水平居中常用方法
html <div class="outer"> <div class="inner"></div> </div> 基本樣式 .outer { background: #ddd; width: 500px;
CSS實現垂直水平居中的方法
在網上查到了很多種居中方法,但是都有一些侷限,如通過line-height實現的情況不能設定100%。今天突然靈光一閃想到了一個辦法,試了試感覺效果不錯。下面直接上程式碼。 html: <div class="body"> <div class
css 實現垂直水平居中
height 水平居中 ble none clas 居中 tty osi webkit <html> <head> <meta name="viewport" content="width=device-width, min
純CSS實現圖片水平垂直居中於DIV(圖片未知寬高)
分享 分享圖片 gin pos pic ble 居中 實現圖 class .demo{border:1px #ddd solid;width:267px;height:267px;overflow:hidden;text-align:center;display:tabl
純CSS完美實現垂直水平居中的6種方式
前言 由於HTML語言的定位問題,在網頁中實現居中也不是如word中那麼簡單,尤其在內容樣式多變,內容寬高不定的情況下,要實現合理的居中也是頗考驗工程師經驗的。網上講居中的文章很多,但是都不太完整,所以小茄今天就來總結下純CSS實現居中的各種方案。學疏
純CSS實現垂直居中的幾種方法
right lin 兼容 bar 實現 syntax info pro ems 垂直居中是布局中十分常見的效果之一,為實現良好的兼容性,PC端實現垂直居中的方法一般是通過絕對定位,table-cell,負邊距等方法。有了css3,針對移動端的垂直居中就更加多樣化。 方法1:
純CSS實現垂直居中
總結歸納一下現在學到的純CSS實現水平垂直居中的方法: (1)如果元素的寬度是已知的,那麼可以利用父類元素設定成position:relative,子類設定為position:absolute 然後定位距離上margin為50%,左50%,再減去元素自身的寬度就可以實現,例子程式碼: <
css(css3)實現垂直水平居中的那些事
都說“金三銀四求職季”,本人也選擇了在這個時候成為這浩浩蕩蕩的求職大軍中的一員,幾次面試下來,發現面試官都愛問的一個問題就是:“說說垂直水平居中都有哪些實現方式吧?” 面試過程巴拉巴拉一大堆,現在終於有時間把這些知識點總結歸納起來,希望能幫自己鞏固下,也希望可以
實現垂直水平居中的五種方法
1.基於表格樣式 將要使內容居中的外層容器元素的display設定成table,內層內容塊使用table-cell,然後分別設定水平和垂直居中: /*表格方案*/ #table-father{
盤點8種CSS實現垂直居中水平居中的絕對定位居中技術
其他 margin phone ie10 logs html mar over 防止 1.絕對定位居中(Absolute Centering)技術 我們經常用margin:0 auto;來實現水平居中,而一直認為margin:auto;不能實現垂直居中......實際上
CSS實現垂直居中水平居中
增加 position 大小 -a 添加 abs 100% 布局 table 1、絕對定位居中(子元素需設置寬高) > 原理:元素在過度受限情況下,將margin設置為auto,瀏覽器會重算margin的值,過度受限指的是同時設置top/bottom與height或
html css實現文字水平垂直居中顯示
這幾天在工作中遇到了一個小問題:文字內容怎麼能在div裡水平垂直居中顯示呢?同時群裡的小夥伴恰巧也有提問這個問題的,所以我就總結了一下我知道的方法。 一、利用行高(line-height)和vertical-align配合實現 具體做法如下: html: <div clas
CSS全面實現內容水平居中,垂直居中
CSS全面實現內容水平居中,垂直居中 概述 水平居中(內容) 行級元素inline/inline-block 塊級元素(block) 一行多個塊級元素 垂直居中(內容) 行級元
css 實現元素水平垂直居中的幾種方法
在做網頁的時候經常會需要讓某一個元素水平垂直居中,而要實現水平垂直居中有很多種方法,不知道你們有沒遇到這種情況:方法倒是知道,甚至還知道許多種,可一到真正用到的時候,卻突然在努力地回想,這或許就是人們經常說的「腦子短路」,所以,這篇文章和大家一起來溫故而知新,梳
CSS實現垂直居中水平居中的絕對定位居中技術
Ⅰ.絕對定位居中(Absolute Centering)技術 我們經常用margin:0 auto來實現水平居中,而一直認為margin:auto不能實現垂直居中……實際上,實現垂直居中僅需要宣告元素高度和下面的CSS:
3行Css實現div水平、垂直居中
HTML部分: <div class="container"> <!-- This is your content ... --> </div> Css部分: 就像下面,只需要給.containe