<div id="parent">
<!-- 定義子級元素 -->
<div id="child">居中布局</div>
</div>
過以下CSS樣式代碼實現(xiàn)水平方向居中布局效果
.parent{position:relative;}
.child{position:absolute;left:50%;transform: translateX(-50%)}
優(yōu)點:
父級元素是否脫離文檔流, 不影響子集元素水平居中效果
缺點:transform屬性是CSS3中新增屬性, 瀏覽器支持情況不好
/* HTML */
<div class='father'>
<div class='son'></div>
</div>
<style>
.father {
display: table-cell;
vertical-align: middle;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
width: 50px;
height: 50px;
background-color: aqua;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
background-color: aqua;
width: 50px;
height: 50px;
top: 0;
bottom: 0;
margin: auto;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
/* 負margin須是高度的一半 */
margin-top: -50px;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
/* 注意"-"兩邊要隔開 減去的須是高度的一半*/
top: calc(50% - 50px);
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
transform: translateY(-50%);
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
line-height: 300px;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
display: inline-block;
vertical-align: middle;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: flex;
align-items: center;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: grid;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
align-self: center;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: block;
}
.father::after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.son {
background-color: aqua;
width: 50px;
height: 50px;
display: inline-block;
vertical-align: middle;
}
</style>
復制代碼
<!-- HTML -->
<div class="father">
<div class="hide"></div>
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
background-color: aqua;
width: 50%;
height: 50%;
}
.hide {
width: 50px;
height: 25%;
}
</style>
復制代碼
<!-- HTML -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
writing-mode: vertical-lr;
text-align: center;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
writing-mode: horizontal-tb;
display: inline-block;
}
</style>
復制代碼
作者:soloplayer
鏈接:https://juejin.cn/post/6904138129612439560
來源:掘金
平居中
1) 若是行內(nèi)元素, 給其父元素設(shè)置 text-align:center,即可實現(xiàn)行內(nèi)元素水平居中.
2) 若是塊級元素, 該元素設(shè)置 margin:0 auto即可.
3) 若子元素包含 float:left 屬性, 為了讓子元素水平居中, 則可讓父元素寬度設(shè)置為fit-content,并且配合margin, 作如下設(shè)置:
.parent{ width: -moz-fit-content; width: -webkit-fit-content; width:fit-content; margin:0 auto; }
fit-content是CSS3中給width屬性新加的一個屬性值,它配合margin可以輕松實現(xiàn)水平居中, 目前只支持Chrome 和 Firefox瀏覽器.
4) 使用flex 2012年版本布局, 可以輕松的實現(xiàn)水平居中, 子元素設(shè)置如下:
.son{ display: flex; justify-content: center; }
5) 使用flex 2009年版本, 父元素display: box;box-pack: center;如下設(shè)置:
.parent { display: -webkit-box; -webkit-box-orient: horizontal; -webkit-box-pack: center; display: -moz-box; -moz-box-orient: horizontal; -moz-box-pack: center; display: -o-box; -o-box-orient: horizontal; -o-box-pack: center; display: -ms-box; -ms-box-orient: horizontal; -ms-box-pack: center; display: box; box-orient: horizontal; box-pack: center; }
6) 使用CSS3中新增的transform屬性, 子元素設(shè)置如下:
.son{ position:absolute; left:50%; transform:translate(-50%,0); }
7) 使用絕對定位方式, 以及負值的margin-left, 子元素設(shè)置如下:
.son{ position:absolute; width:固定; left:50%; margin-left:-0.5寬度; }
8) 使用絕對定位方式, 以及l(fā)eft:0;right:0;margin:0 auto; 子元素設(shè)置如下:
.son{ position:absolute; width:固定; left:0; right:0; margin:0 auto; }
垂直居中
單行文本
1) 若元素是單行文本, 則可設(shè)置 line-height 等于父元素高度
行內(nèi)塊級元素
2) 若元素是行內(nèi)塊級元素, 基本思想是使用display: inline-block, vertical-align: middle和一個偽元素讓內(nèi)容塊處于容器中央.
.parent::after, .son{ display:inline-block; vertical-align:middle; }
這是一種很流行的方法, 也適應(yīng)IE7.
元素高度不定
3) 可用 vertical-align 屬性, 而vertical-align只有在父層為 td 或者 th 時, 才會生效, 對于其他塊級元素, 例如 div、p 等, 默認情況是不支持的. 為了使用vertical-align, 我們需要設(shè)置父元素display:table, 子元素 display:table-cell;vertical-align:middle;
優(yōu)點
元素高度可以動態(tài)改變, 不需再CSS中定義, 如果父元素沒有足夠空間時, 該元素內(nèi)容也不會被截斷.
缺點
IE6~7, 甚至IE8 beta中無效.
4) 可用 Flex 2012版, 這是CSS布局未來的趨勢. Flexbox是CSS3新增屬性, 設(shè)計初衷是為了解決像垂直居中這樣的常見布局問題. 相關(guān)的文章如《彈性盒模型Flex指南》
父元素做如下設(shè)置即可保證子元素垂直居中:
.parent { display: flex; align-items: center;
優(yōu)點
內(nèi)容塊的寬高任意, 優(yōu)雅的溢出.
可用于更復雜高級的布局技術(shù)中.
缺點
IE8/IE9不支持
需要瀏覽器廠商前綴
渲染上可能會有一些問題
5) 使用flex 2009版.
.parent { display: box; box-orient: vertical; box-pack: center; }
優(yōu)點
實現(xiàn)簡單, 擴展性強
缺點
兼容性差, 不支持IE
6) 可用 transform , 設(shè)置父元素相對定位(position:relative), 子元素如下css樣式:
.son{ position:absolute; top:50%; -webkit-transform: translate(-50%,-50%); }
優(yōu)點
代碼量少
缺點
IE8不支持, 屬性需要追加瀏覽器廠商前綴, 可能干擾其他 transform 效果, 某些情形下會出現(xiàn)文本或元素邊界渲染模糊的現(xiàn)象.
元素高度固定
7) 設(shè)置父元素相對定位(position:relative), 子元素如下css樣式:
.son{ position:absolute; top:50%; height:固定; margin-top:-0.5高度; }
優(yōu)點
適用于所有瀏覽器.
缺點
父元素空間不夠時, 子元素可能不可見(當瀏覽器窗口縮小時,滾動條不出現(xiàn)時).如果子元素設(shè)置了overflow:auto, 則高度不夠時, 會出現(xiàn)滾動條.
8) 設(shè)置父元素相對定位(position:relative), 子元素如下css樣式:
.son{ position:absolute; height:固定; top:0; bottom:0; margin:auto 0; }
優(yōu)點
簡單
缺點
沒有足夠空間時, 子元素會被截斷, 但不會有滾動條.
總結(jié)
水平居中較為簡單, 共提供了8種方法, 一般情況下 text-align:center,marin:0 auto; 足矣
① text-align:center;
② margin:0 auto;
③ width:fit-content;
④ flex
⑤ 盒模型
⑥ transform
⑦ ⑧ 兩種不同的絕對定位方法
垂直居中, 共提供了8種方法.
① 單行文本, line-height
② 行內(nèi)塊級元素, 使用 display: inline-block, vertical-align: middle; 加上偽元素輔助實現(xiàn)
③ vertical-align
④ flex
⑤ 盒模型
⑥ transform
⑦ ⑧ 兩種不同的絕對定位方法
我們發(fā)現(xiàn), flex, 盒模型, transform, 絕對定位, 這幾種方法同時適用于水平居中和垂直居中.
希望對大家有所幫助.
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。