SS 是前端里面的基礎(chǔ)之一,也是非常重要的一部分,它往往決定了你所做出來的網(wǎng)頁頁面是否美觀。在設(shè)計(jì)網(wǎng)頁頁面的過程中,總會(huì)有將元素或者文字進(jìn)行水平垂直居中的要求。下面w3cschool編程獅就為大家介紹 CSS 中幾種常用到的水平垂直居中的方法。
當(dāng)元素有給定的高度以及寬度的時(shí)候,使用 margin: auto; 元素僅會(huì)水平居中,并不會(huì)進(jìn)行垂直居中。此時(shí)就需要設(shè)置元素的 position 為 absolute,父級元素的 position 為 relative,同時(shí)元素的上下左右都需要設(shè)置為 0。
HTML 代碼
<div class="box">
<div class="center1"></div>
</div>
CSS 代碼
.box{
width: 200px;
height: 200px;
background-color: #eee;
position: relative;
margin-top: 20px;
}
.center1{
width: 50px;
height: 50px;
background-color: #00ACED;
margin: auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
效果展示:
當(dāng)已經(jīng)知道了要進(jìn)行水平垂直居中的元素的寬高時(shí),就可以通過設(shè)置 position: absolute 來實(shí)現(xiàn)。但是,使用的同時(shí)還需要結(jié)合其他屬性才完整實(shí)現(xiàn)。因?yàn)椋瑔问窃O(shè)置 absolute,上左距離均為一半,就會(huì)出現(xiàn)下面這種情況。很顯然可以看到,元素并不是完全居中,僅只有左上角的位置在中心點(diǎn)
概念圖:
因此想要實(shí)現(xiàn)元素完全水平垂直居中,在設(shè)置了 absolute 定位后,可以設(shè)置 margin 值為負(fù),或者使用 calc 來計(jì)算,上左距離在 50% 的基礎(chǔ)上還要減去元素本身一半的寬高。
margin 值為負(fù)或者 calc 計(jì)算均是在已知元素寬高的情況下,假設(shè)不知道元素的寬高,那么怎么實(shí)現(xiàn)水平垂直居中呢?這里就可以使用 transform 屬性,通過坐標(biāo)位移來實(shí)現(xiàn)居中。
CSS 代碼
/* 結(jié)合 margin */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
margin-left: -25px;
margin-top: -25px;
}
/* 結(jié)合 calc 計(jì)算*/
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: calc(50% - 25px)
top: calc(50% - 25px);
}
/* 結(jié)合 transform */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
效果展示
03
PART
可以通過彈性布局來設(shè)置水平垂直居中,這里需要設(shè)置父級元素 display:flex; 還需要設(shè)置兩個(gè)屬性,水平布局 justify-content 以及垂直布局 align-items。
HTML代碼
<div class="box2">
<div class="center4"></div>
</div>
CSS代碼:
.box2{
background-color: #eee;
width: 200px;
height: 200px;
position: relative;
margin-top: 20px ;
display: flex;
justify-content: center;
align-items: center;
}
.center4{
width: 50px;
height: 50px;
background-color: #B39873;
}
效果展示:
前面介紹的是元素如何實(shí)現(xiàn)水平垂直居中,下面介紹的是如何將文字進(jìn)行水平垂直居中。這第一個(gè)方法也是最經(jīng)常用的,使用文本水平對齊 text-align 和行高 line-height 來實(shí)現(xiàn)的。
HTML 代碼
<div class="box3">
<div class="center5">文字居中</div>
</div>
CSS 代碼
.box3{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
}
.center5{
text-align: center;
line-height: 200px;
}
效果展示
05
PART
第二個(gè)方法可以通過網(wǎng)格布局 grid 來實(shí)現(xiàn)。而這里通過 grid 有兩種方式實(shí)現(xiàn),一種對元素本身屬性進(jìn)行設(shè)置,另一種在元素的父級元素中設(shè)置。兩者看上去內(nèi)容似乎差不多,不同的是在元素中設(shè)置的是 align-self 還要多了一個(gè) margin,父級元素中是 align-items。
相關(guān)代碼:
/* grid 元素中設(shè)置 */
.box4{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
}
.center6{
align-self: center;
justify-content: center;
margin: auto;
}
/* grid 父級元素中設(shè)置 */
.box5{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
align-items: center;
justify-content: center;
}
效果展示:
以上就是關(guān)于 CSS 如何將元素或者文字進(jìn)行水平垂直居中的幾種常用方法,大家還其他關(guān)于 CSS 實(shí)現(xiàn)水平垂直居中的方法嗎?請?jiān)谠u論區(qū)留下你的想法。
關(guān)注w3cschool編程獅訂閱更多IT資訊、技術(shù)干貨~
哥最近調(diào)整自己的一個(gè)網(wǎng)站時(shí),覺得在發(fā)布文章時(shí),手動(dòng)把圖片居中,實(shí)在有些麻煩。能不能讓文章內(nèi)容中的圖片自動(dòng)居中呢?是否可以通過樣式控制表來實(shí)現(xiàn)?
網(wǎng)上找了下答案,似乎有些過于復(fù)雜了。自己動(dòng)手實(shí)踐吧。
首先要調(diào)整的,是讓圖片獨(dú)占一行空間。
補(bǔ)充CSS知識(shí)點(diǎn):
圖像(<img>標(biāo)簽)默認(rèn)是行內(nèi)樣式(inline style),行內(nèi)樣式的大體意思是,它前邊或后邊的HTML標(biāo)簽元素會(huì)緊挨著排在它的前后(如影隨形),不會(huì)另換一行;而像DIV標(biāo)簽則默認(rèn)是塊級元素,塊級元素前后的標(biāo)簽元素會(huì)換行。因此需要把文章內(nèi)容中的圖片樣式由行內(nèi)樣式更換為block塊級元素,要不就易和文字內(nèi)容糾纏在一起。
簡單點(diǎn):
block元素將顯示為塊級元素,此元素前后會(huì)帶有換行符。
inline默認(rèn)。元素會(huì)被顯示為內(nèi)聯(lián)元素,元素前后沒有換行符。
在原有的控制圖像的樣式中增加:display: block。
.entry img {
max-width: 100%;
height: auto;
display: block;
}
圖像設(shè)為塊級元素后,需要讓圖像居中。居中用的同樣是網(wǎng)頁布局時(shí)整體居中的代碼,即margin:0 auto,0可以改為任何數(shù)據(jù),像素或EM;重要的是AUTO屬性,因?yàn)樗刂圃刈笥业木嚯x,AUTO則是用于居中的屬性。
實(shí)例:
.entry img {
max-width: 100%;
height: auto;
display: block;
margin:0 auto;
}
經(jīng)以上設(shè)置后,圖像已能自動(dòng)在文章內(nèi)容中居中。但在實(shí)踐中,發(fā)現(xiàn)一些舊的網(wǎng)頁文章,在發(fā)布時(shí)其圖像有行內(nèi)樣式,如:<img style=”margin:0;”>這樣的。由于行內(nèi)樣式的優(yōu)先(層疊)級別高于外部樣式表中的圖像自動(dòng)居中的定義,因此剛才定義的:margin:0 auto就失效了。
這也不是問題,給這個(gè)居中用!important指定為最優(yōu)先級別。最樣使用的樣式如下。
.entry img {
max-width: 100%;
height: auto;
display: block;
margin: 0 auto !important;
}
好記性不如爛筆頭,記下來方便再次用。
實(shí)例應(yīng)用可查看:起點(diǎn)通首頁點(diǎn)任一文章內(nèi)容頁,或肖運(yùn)華個(gè)人博客文章內(nèi)容頁。
. 元素高度聲明的情況下在父容器中居中:絕對居中法
<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.無需其他冗余標(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 居中:
*請認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。