中,是我們編碼過程中最常見的,那么,我們平時(shí)常見的居中方式,下面一一羅列出來(lái),有錯(cuò)誤的地方,望碼友多多包涵并加以矯正。
水平居中
1、多塊級(jí)元素,設(shè)置display:inline-block;使之在一行排列,在父級(jí)樣式里,設(shè)置text-align:center;就可以實(shí)現(xiàn)水平居中的效果
body {
text-align: center;
}
div{
width: 100px;
height: 100px;
border: 1px solid;
display: inline-block;
}
2、內(nèi)聯(lián)元素,利用text-align:center;可以實(shí)現(xiàn)塊級(jí)元素內(nèi)部的內(nèi)聯(lián)元素的水平居中
div {
border: 1px solid red;
width: 100px;
height: 100px;
text-align: center;
}
<div>
<span>塊級(jí)元素中的行內(nèi)元素的水平居中</span>
</div>
3、塊級(jí)元素,通過把固定寬高的塊級(jí)元素的margin-left和margin-right設(shè)置為auto,方可實(shí)現(xiàn)
div{
width: 100px;
height: 100px;
border: 1px solid;
margin: 0 auto;
}
<div></div>
4、利用彈性盒子(display: flex;)
給父級(jí)定寬定高,然后設(shè)置display: flex;以及justify-content: center;方可達(dá)到水平居中效果
body {
width: 500px;
height: 500px;
display: flex;
justify-content: center;
border: 1px solid red;
}
div {
width: 100px;
height: 100px;
border: 1px solid;
}
<body>
<div></div>
</body>
垂直居中
1、內(nèi)聯(lián)元素(單行)
通過設(shè)置元素的height和line-height,方可達(dá)到居中效果
2、多行元素,利用表布局(table)
通過給想要居中的元素的父級(jí)設(shè)置display: talbe-cell;以及vertical-align:enter;方可居中
3、彈性盒子(flex)
給父級(jí)設(shè)置display: flex;變成彈性盒子。
分兩種,
(1),主軸方向?yàn)樗?,直接設(shè)置 align-items: center;
(2),主軸方向?yàn)榇怪?,設(shè)置flex-direction: column;改變主軸方向,然后設(shè)置justify-content: center;
彈性盒模型主軸不同,居中的方式也不同,靈活應(yīng)用。
4、固定寬高的塊級(jí)元素
利用父相子絕的定位原理,實(shí)現(xiàn)垂直居中
position: absolute;
left: 50%;
top: 50%;
margin-left: (自身高度的一半);
5,未知寬高的塊級(jí)元素
利用transform: translateY(-50%);方可實(shí)現(xiàn)
position: absolute;
top: 50%;
transform: translateY(-50%);
水平垂直方向的居中
1、固定寬高
通過margin平移整體寬高的一半,實(shí)現(xiàn)水平垂直居中
{
position: absolute;
width: 100px;
height: 100px;
border: 1px solid;
left: 50%;
top: 50%;
margin-top: -50px;
margin-left: -50px;
}
2、未知寬高
利用transform中的translate()屬性實(shí)現(xiàn)
{
position: absolute;
border: 1px solid;
left: 50%;
top: 50%;
transform: translateY(-50%);
transform: translateX(-50%);
}
3、彈性盒子(flex)
通過display:flex,把父級(jí)變成彈性盒模型,利用align-items: center;justify-content: center;方可實(shí)現(xiàn)居中。
注意:彈性盒子容器中,多行項(xiàng)目的居中方式另加計(jì)算。
body {
border: 1px solid;
width: 300px;
height: 300px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
div {
border: 1px solid;
width: 100px;
height: 100px;
}
隨著學(xué)習(xí)的不斷深入,居中方式可以有很多種,我們要善于利用,更加明確什么情況下用怎樣的居中方式。
里是工作狂的聚集地 | ||||
職場(chǎng) | 學(xué)術(shù) | 新媒體 | 設(shè)計(jì) | 極客 |
專門治愈處女座強(qiáng)迫癥。
本文為CSS入門
翻譯 redman9
原載CSS-Trick
人們經(jīng)常抱怨在 CSS 中居中元素的問題,其實(shí)這個(gè)問題并不復(fù)雜,只是因?yàn)榉椒ū姸啵枰鶕?jù)情況從眾多方法中選取一個(gè)出來(lái)。接下來(lái),我們做一個(gè) "決定樹" 來(lái)幫我們把問題變的簡(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
類的元素▼通過inline-block
實(shí)現(xiàn)
.parent {
text-align: center;
}
.child {
display: inline-block;
text-align: left;
}
通過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
撐開高度 ,文字垂直居中。
.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; }
上又要到秋招的時(shí)候了,又有不少人打算換工作了。前端在面試中總會(huì)被問到的一道基礎(chǔ)題div居中方法,這里給大家總結(jié)一下都有哪些常用的方法。
還未偏移一半自身寬高
<style>
.parent {
position: relative;
width: 500px;
height: 500px;
border: solid red 1px;
}
.demo {
position: absolute;
width: 100px;
height: 100px;
border: solid blue 1px;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
</style>
<body>
<div class="parent">
<div class="demo"></div>
</div>
</body>
通過flex彈性布局設(shè)置垂直居中和水平居中
<style>
.parent {
width: 500px;
height: 500px;
border: solid red 1px;
display: flex;
// 垂直,水平居中
align-items: center;
justify-content: center;
}
.demo {
width: 100px;
height: 100px;
border: solid blue 1px;
}
</style>
<body>
<div class="parent">
<div class="demo"></div>
</div>
</body>
在子元素不知道自身寬高情況,使用transform進(jìn)行比偏移。
<style>
.parent {
position: relative;
width: 500px;
height: 500px;
border: solid red 1px;
}
.demo {
position: absolute;
border: solid blue 1px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<body>
<div class="parent">
<div class="demo">居中</div>
</div>
</body>
以上3種是常用的方法,當(dāng)然還有其他居中方法比如grid布局,table-cell布局等。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。