Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
篇文章主要給大家介紹一下如何使用html+css實(shí)現(xiàn)元素的水平與垂直居中效果,這也是我們網(wǎng)頁(yè)在編碼制作中會(huì)經(jīng)常用到的問(wèn)題。
主要實(shí)現(xiàn)css代碼:
水平居中:text-align:center;
垂直居中:line-height:XXpx; /*line-height與元素的height的值一致*/
我們先來(lái)看這樣一個(gè)例子,加入我們這里有一個(gè)div,寬度和高度為300px,背景顏色為黑色,然后在div中有一行簡(jiǎn)短文字,我們只需要使用line-height:200px;就可以實(shí)現(xiàn)文字的居中效果,具體的代碼如下所示:
由上圖可以看出我們實(shí)現(xiàn)了單行文字的垂直居中效果,但是很多時(shí)候我們的文字并不知道具體有多少,可能有一行,也可能有很多行,那么遇到多行文字的這種問(wèn)題我們要如何處理呢。
對(duì)于多行文本的垂直居中我們有很多種實(shí)現(xiàn)方式,我們這里逐個(gè)的來(lái)看一下;
1、使用display:table來(lái)實(shí)現(xiàn)
主要實(shí)現(xiàn)代碼:
display: table使塊狀元素成為一個(gè)塊級(jí)表格;
display: table-cell;子元素設(shè)置成表格單元格;
vertical-align: middle;使表格內(nèi)容居中顯示,即可實(shí)現(xiàn)垂直居中的效果;
具體的html與css的代碼就如下所示:
2、使用absolute與transform配合實(shí)現(xiàn)
主要實(shí)現(xiàn)代碼:
position:absolute; 首先給文本絕對(duì)定位;
left:50%;top:50%;transform:translate(-50%,-50%); 讓文本距離盒子左邊和上邊分別為50%,再用transform向左(上)平移它自己寬度(高度)的50%,也就達(dá)到居中效果了。
具體的html與css的代碼就如下所示:
3、使用flex實(shí)現(xiàn)
主要實(shí)現(xiàn)代碼:
display: flex;設(shè)置 display 屬性的值為 flex 將其定義為彈性容器
align-items: center;定義項(xiàng)目在交叉軸(縱軸)上如何對(duì)齊,垂直對(duì)齊居中
justify-content: center; 定義了項(xiàng)目在主軸上的對(duì)齊方式,水平對(duì)齊居中
具體的html與css的代碼就如下所示:
好了,本篇文章就給大家說(shuō)到這里,大家自己動(dòng)手寫(xiě)一下看能不能寫(xiě)出一樣的頁(yè)面效果出來(lái),也可以找一些類(lèi)似的頁(yè)面自己練習(xí)一下,有需要源碼的可以直接私信【網(wǎng)站源碼】即可。
每日金句:了解別人心里想什么,你才能得到自己想要的。喜歡我的文章的小伙伴記得關(guān)注一下哦,每天將為你更新最新知識(shí)。
當(dāng)父div的行高等于自身高度時(shí),內(nèi)部的行內(nèi)元素會(huì)上下居中顯示。行內(nèi)塊沒(méi)有固定高度時(shí)也會(huì)上下居中顯示。所以需要對(duì)父div的 line-height 進(jìn)行調(diào)整。利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長(zhǎng)度為父div的高度(寬度)的100%。定位屬性top和bottom(或是left和right)值分別設(shè)置為0,但子div有固定高度(寬度),并不能達(dá)到上下(左右)間距為0,此時(shí)給子div設(shè)置 margin:auto 會(huì)使它居中顯示。
轉(zhuǎn)載自喜歡JS的無(wú)名小站
例如 一個(gè)父div(w:100%;h:400px)中有一個(gè)子div(w:100px;100px;)。讓其上下左右居中。
利用表格單元格的居中屬性。
父div外層配置一個(gè)div,同時(shí)設(shè)置為表格元素 (display: table),寬度為100%
父div配置為表格單元格元素 (display: table-cell)
父div配置居中屬性(vertical-align: middle),使子div上下居中
子div通過(guò)margin配置左右居中(margin-left:auto; margin-right:auto)
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .table {display: table; width: 100%;} .father {display: table-cell; vertical-align: middle;} .son {margin: auto;} </style> <body> <div class="table" > <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </div> </body>
注:
表格單元格比較特殊,如果只有一個(gè)單元格時(shí),它的寬度默認(rèn)會(huì)占父級(jí)(table|tr)寬度的100%;
table默認(rèn)寬度不會(huì)撐開(kāi),需要手動(dòng)配置width:100%;
當(dāng)父div的行高等于自身高度時(shí),內(nèi)部的行內(nèi)元素會(huì)上下居中顯示。行內(nèi)塊沒(méi)有固定高度時(shí)也會(huì)上下居中顯示。通過(guò)文本居中屬性text-align:center
,可以使內(nèi)部行內(nèi)元素或行內(nèi)塊元素左右居中顯示。
子div設(shè)定為行內(nèi)塊元素(display:inline-block);
父div設(shè)置行高(line-height)使子div上下居中
父div設(shè)置文本居中(text-align:center)使子div左右居中。
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {line-height: 500px; text-align: center; font-size: 0;} .son { display: inline-block; /* display: inline-flex; display: inline-grid; display: inline-table; */ } </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
注: 行高如果設(shè)置為當(dāng)前父div的高度(400px)的話(huà),有固定高度的子div并不會(huì)居中顯示的,問(wèn)題出在瀏覽器默認(rèn)將其當(dāng)做文本居中的,即把它當(dāng)做了一段文本(chrome默認(rèn)font-size:16px;hight:21px)進(jìn)行居中,沒(méi)把它當(dāng)做高度100px進(jìn)行居中。所以需要對(duì)父div的line-height
進(jìn)行調(diào)整。以font-size:0
(對(duì)應(yīng)的字體高度為0)為例子,則需要line-height增加一個(gè)子div的高度(400px + 100px;)。
利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長(zhǎng)度為父div的高度(寬度)的100%。
父div標(biāo)記下定位(position:relative|absolute|fixed);子div絕對(duì)定位(position:absolute)
子div上下居中:top:50%;margin-top:-h/2;
或是 bottom:50%;margin-bottom:-h/2;
;
子div左右居中: left:50%;margin-left:-w/2
或是 right:50%;margin-right:-w/2
;
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {position: relative;} .son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px; } </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
定位屬性top和bottom(或是left和right)值分別設(shè)置為0,但子div有固定高度(寬度),并不能達(dá)到上下(左右)間距為0,此時(shí)給子div設(shè)置margin:auto會(huì)使它居中顯示。
父div標(biāo)記下定位(position:relative|absolute|fixed|sticky);子div絕對(duì)定位(position:absolute)
子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto
子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {position: relative;} .son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto} </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
彈性盒子,自帶的一個(gè)居中功能
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {display: flex; align-items: center} .son {margin: auto} </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
flex兼容性,以及存在的已知問(wèn)題
方法二和方法三兼容性要比其它好些
Can I use
css-vertical-center-solution
CSS實(shí)現(xiàn)垂直居中的5種方法--前端觀察
<div id="parent">
<!-- 定義子級(jí)元素 -->
<div id="child">居中布局</div>
</div>
過(guò)以下CSS樣式代碼實(shí)現(xiàn)水平方向居中布局效果
.parent{position:relative;}
.child{position:absolute;left:50%;transform: translateX(-50%)}
優(yōu)點(diǎn):
父級(jí)元素是否脫離文檔流, 不影響子集元素水平居中效果
缺點(diǎn):transform屬性是CSS3中新增屬性, 瀏覽器支持情況不好
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。