自己是一名從事了多年開(kāi)發(fā)的web前端老程序員,目前辭職在做自己的web前端私人定制課程,今年年初我花了一個(gè)月整理了一份最適合2019年學(xué)習(xí)的web前端學(xué)習(xí)干貨,各種框架都有整理,送給每一位前端小伙伴,想要獲取的可以關(guān)注我的頭條號(hào)并在后臺(tái)私信我:前端,即可免費(fèi)獲取
Html5-CSS之五大居中方式
你是不是也對(duì)元素居中的知識(shí)點(diǎn)很是模糊?是不是苦于找不到一個(gè)總結(jié)的通俗易懂的說(shuō)明?是不是自己懶得去總結(jié)?恭喜你,搜到這篇博客! 這是鄙人在前端的學(xué)習(xí)與實(shí)踐中總結(jié)出的元素的五大居中方式,黏貼了代碼并對(duì)代碼做了解釋,希望對(duì)迷茫的有所幫助!
下面的居中示例中,統(tǒng)一使用了同一個(gè)div作為父元素和p作為子元素
設(shè)置一個(gè)div,并且設(shè)置了div的寬高邊框,div里面設(shè)置一個(gè)塊元素p,設(shè)置了它的寬高和背景色
css居中方式1
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中1</title> <style> *{margin:0;} div{width:200px;height:300px;border:2px solid #000;margin:200px auto; text-align:center;font-size:0; } div p{width:100px;height:100px;background:#666; display:inline-block;vertical-align:middle; } div:after{content:"";display:inline-block;height:100%;vertical-align:middle;} </style> </head> <body> <div> <p></p> </div> </body> </html>
這里利用了偽元素讓子元素p在div盒子里左右水平居中只需要在它的父元素div里加text-align:center;垂直方向居中需要在父元素后面加了一個(gè)偽元素,并使得樣式為inline-block;height:100%;就是和父元素一樣高,vertical-align:middle;垂直居中,也就是p元素相對(duì)與偽元素居中,由于偽元素和div一樣高,所以相當(dāng)于p元素在div里垂直居中。
css居中方式2
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中2</title> <style> *{margin:0;} div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;} p{position:absolute;left:0;bottom:0;right:0;top:0;margin:auto;width:100px;height:100px;background:#f99;} </style> </head> <body> <div> <p></p> </div> </body> </html>
這里利用了定位居中
子元素p設(shè)置position:absolute脫離文檔流,默認(rèn)以html作為父元素,所以我們給父元素div設(shè)置position:relative;使得p以div為父元素做位置的變動(dòng),left:0;tight:0;top:0;bottom:0;(只有設(shè)置了定位的元素才可以使用這種方式來(lái)移動(dòng)),最后margin:auto;就會(huì)水平和垂直都居中。
css居中方式3
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中3</title> <style> *{margin:0;} div{display:flex;justify-content:center;align-items:center;width:300px;height:400px;border:1px solid #000;margin:100px auto;} p{width:100px;height:100px;background:#f99;} </style> </head> <body> <div> <p></p> </div> </body> </html>
這里利用了彈性盒居中
父元素div設(shè)置成彈性盒樣式,justify-content:center;主軸居中
align-items:center;垂直居中(而且這兩個(gè)只能設(shè)置在父元素上,彈性盒知識(shí))
css居中方式4
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中4</title> <style> *{margin:0;} div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;} p{width:100px;height:100px;background:#f99;position:absolute; left:50%;top:50%;margin:-50px 0 0 -50px;} </style> </head> <body> <div> <p></p> </div> </body> </html>
利用定位線左上角居中,然后左移子元素寬度的一半,再上移子元素高度的一半。
css居中方式5
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中5</title> <style> *{margin:0;} div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;} p{position:absolute;width:100px;height:100px;background:#f99;left:50%;top:50%; transform:translate(-50%,-50%);} </style> </head> <body> <div> <p></p> </div> </body> </html>
利用動(dòng)畫(huà)移動(dòng)屬性transform
結(jié)語(yǔ)
相信看了上面的有關(guān)Html5、css的元素五大居中方式,你們就可以解決自己的小問(wèn)題了,但是也要養(yǎng)成一個(gè)總結(jié)的好習(xí)慣。好記性不如爛筆頭!以前留下來(lái)的話語(yǔ)總是有他的道理。Come on!
原文鏈接:https://blog.csdn.net/qq_38110274/article/details/102756968
里是工作狂的聚集地 | ||||
職場(chǎng) | 學(xué)術(shù) | 新媒體 | 設(shè)計(jì) | 極客 |
專門治愈處女座強(qiáng)迫癥。
本文為CSS入門
翻譯 redman9
原載CSS-Trick
人們經(jīng)常抱怨在 CSS 中居中元素的問(wèn)題,其實(shí)這個(gè)問(wèn)題并不復(fù)雜,只是因?yàn)榉椒ū姸啵枰鶕?jù)情況從眾多方法中選取一個(gè)出來(lái)。接下來(lái),我們做一個(gè) "決定樹(shù)" 來(lái)幫我們把問(wèn)題變的簡(jiǎn)單一點(diǎn)。首先你需要居中:
—— 水平 ——
?需要居中inline
或者inline-*
元素(如文字或者鏈接)?
? 需要居中block
類的元素?
? 需要居中多個(gè)block
元素?
—— 垂直 ——
?需要居中inline
或者inline-*
元素(如文字或者鏈接)?
?需要居中block
類的元素?
——既水平又垂直 ——
?固定寬高
?不固定寬高
?使用flexbox
● ● ●
水平居中
inline
或者inline-*
元素▼你可以輕松的在一個(gè)block
元素中水平居中一個(gè)inline
元素,以下代碼對(duì)inline
,inline-block
,inline-table
和inline-flex
等有效。
.parent {
text-align: center;
}
block
類的元素▼在block
元素被設(shè)定固定寬度的情況下,可以使用設(shè)置元素margin-left
和margin-right
的值為auto
的方法實(shí)現(xiàn)水平居中。
.child {
width: 400px;
margin: 0 auto;
}
block
類的元素▼通過(guò)inline-block
實(shí)現(xiàn)
.parent {
text-align: center;
}
.child {
display: inline-block;
text-align: left;
}
通過(guò)flexbox
實(shí)現(xiàn)
.parent {
display: flex;
justify-content: center;
}
● ● ●
inline
或者inline-*
元素▼inline/text
元素可以簡(jiǎn)單的用設(shè)置相同的上下padding
值達(dá)到垂直居中的目的。.center {
pading-top: 30px; padding-bottom: 30px;
}
如果因?yàn)槟撤N原因不能使用padding
的方法,你還可以設(shè)置line-height
等于height
來(lái)達(dá)到目的。
.center {
height: 100px; line-height: 100px; white-space: nowrap;
}
【多行】
相同的上下padding
也可以適用于此種情況,如果不能生效,你可以嘗試將該元素的父元素的display
設(shè)置為table
,同時(shí)該元素的display
設(shè)置為table-sell
,然后設(shè)置vertical-align
。
.parent {
display: table;
width: 200px; height: 400px;
} .child {
display: table-cell;
vertical-align: middle;
}
如果上述方法不能使用,你可以嘗試使用flexbox
,一個(gè)單獨(dú)的flexbox
子元素可以輕易的在其父元素中居中。謹(jǐn)記,這種方法需要父元素有固定的高度。
.parent {
display: flex; justify-content: center;
flex-direction: column; height: 400px;
}
如果上述兩種方式均不能使用,你可以使用“幽靈元素”技術(shù),這種方法采用偽元素::before
撐開(kāi)高度 ,文字垂直居中。
.parent {
position: relative;
} .parent::before {
content: " "; display: inline-block; height: 100%; width: 1%; vertical-align: middle;
} .child {
display: inline-block;
vertical-align: middle; }
垂直居中block
類的元素▼
【已知元素高度】
.parent {
position: relative; } .child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
}
【未知元素高度】
.parent {
position: relative; } .child {
position: absolute;
top: 50%;
transform: translateY(-50%); }
【使用flexbox
】
.parent {
display: flex;
flex-direction: column;
justify-content: center; }
● ● ●
.parent {
position: relative; } .child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -170px; }
.parent { position: relative; } .child { position: absolute; top: 50%; left: 50%;
transform: translate(-50%, -50%); }
【使用flexbox
】
.parent { display: flex; justify-content: center; align-items:center; }
. 元素高度聲明的情況下在父容器中居中:絕對(duì)居中法
<div class="parent">
<div class="absolute-center"></div>
</div>
.parent {
position: relative;
}
.absolute-center {
position: absolute;
margin: auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 70%;
width: 70%;
}
優(yōu)點(diǎn):
1.跨瀏覽器,包括 IE8-10
2.無(wú)需其他冗余標(biāo)記,CSS 代碼量少
3.完美支持圖片居中
4.寬度高度可變,可用百分比
缺點(diǎn):
1.必須聲明高度
2. 負(fù)外邊距:當(dāng)元素寬度高度為固定值時(shí)。設(shè)置 margin-top/margin-left 為寬度高度一 半的相反數(shù),top:50%;left:50%
<div class="parent">
<div class="negative-margin-center"></div>
</div>
.parent {
position: relative;
}
.negative-margin-center {
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -150px;
height: 300px;
width: 300px;
}
優(yōu)點(diǎn):
良好的跨瀏覽器特性,兼容 IE6-7
代碼量少
缺點(diǎn):
不能自適應(yīng),不支持百分比尺寸和 min-/max-屬性設(shè)置
內(nèi)容可能溢出容器
邊距大小域與 padding,box-sizing 有關(guān)
3. CSS3 Transform 居中:
<div class="parent">
<div class="transform-center"></div>
</div>
.parent {
position: relative;
}
.transform-center {
position: absolute;
left: 50%;
top: 50%;
margin: auto;
width: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
優(yōu)點(diǎn):
內(nèi)容高度可變
代碼量少
缺點(diǎn):
IE8 不支持
屬性需要瀏覽器廠商前綴
可能干擾其他 transform 效果
4. table-cell 居中:
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。