摘要:老生常談,水平垂直居中。為什么大家都愛水平垂直居中呢基本根據(jù)如上結(jié)構(gòu),通過實(shí)現(xiàn)水平垂直居中。絕對定位利用父元素相對定位和子元素絕對定位進(jìn)行水平垂直居中。
老生常談,水平垂直居中。為什么大家都愛水平垂直居中呢?基本HTML
根據(jù)如上結(jié)構(gòu),通過css實(shí)現(xiàn)水平垂直居中。
絕對定位利用父元素相對定位和子元素絕對定位進(jìn)行水平垂直居中。根據(jù)是否知道子元素寬高,使用數(shù)值或者百分比的方式進(jìn)行定位。
方法1.father { width: 100px; height: 100px; background-color: grey; position: relative; } .child { width: 50px; height: 20px; background-color: red; position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; }
通過設(shè)置四向?yàn)?和margin: auto實(shí)現(xiàn)。
方法2.father { width: 100px; height: 100px; background-color: grey; position: relative; } .child { width: 50px; height: 20px; background-color: red; position: absolute; left: 50%; top: 50%; margin: -10px -25px; }
通過設(shè)置left和top使child左上角位置移動到中間,然后再移動自身寬高一般使child中心至中間。
方法3.father { width: 100px; height: 100px; background-color: grey; position: relative; } .child { width: 50px; height: 20px; background-color: red; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }方法4
.father { width: 100px; height: 100px; background-color: grey; position: relative; } .child { width: 50px; height: 20px; background-color: red; position: absolute; left: 50%; top: 50%; transform: translate(-25px, -10px); }總結(jié)
這幾種方法使用了絕對定位,margin或者transform來使子元素水平垂直居中,根據(jù)是否知道具體寬高來使用margin或者transform。
彈性盒子 方法5.father { width: 100px; height: 100px; background-color: grey; display: flex; } .child { width: 50px; height: 20px; background-color: red; margin: auto; }方法6
.father { width: 100px; height: 100px; background-color: grey; display: flex; justify-content: center; align-items:center; } .child { width: 50px; height: 20px; background-color: red; }總結(jié)
這兩種使用了flex彈性盒子布局來實(shí)現(xiàn),隨著瀏覽器兼容性的普及,彈性盒子也越來流行了。
table-cell 方法7.father { width: 100px; height: 100px; background-color: grey; display: table-cell; text-align:center; vertical-align: middle; } .child { display:inline-block; width:50px; height:20px; background-color: red; }
使用了table-cell以及行內(nèi)塊元素來實(shí)現(xiàn)
行內(nèi)元素 方法8.father { width: 100px; height: 100px; background-color: grey; text-align:center; } .child { display:inline-block; width:50px; height:20px; background-color: red; vertical-align: middle; } .father:after{ content:""; width:0; height: 100%; display: inline-block; vertical-align: middle; }
利用偽元素?fù)伍_高度垂直居中。
方法9.father { width: 100px; line-height: 100px; background-color: grey; text-align: center; } .child { display: inline-block; width: 50px; height: 20px; background-color: red; vertical-align: middle; }
利用父元素line-height與inline-block子元素vertical-align垂直居中
相對定位 方法10是不是有點(diǎn)疑惑為啥1、2、3都要用absolute來定位,用relative不行嗎?
答案是可以的。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/114491.html
摘要:前幾天去一家互聯(lián)網(wǎng)公司面試,面試官問到了這個(gè)應(yīng)該算是比較簡單的問題,在我自認(rèn)為回答正確時(shí),才知道這道題的答案有很多種,下面就讓我們一起來探討一下這個(gè)問題思路絕對定位居中原始版這個(gè)是我回答出來的,也是被各位所熟知的一種方法,設(shè)外層相對定位,內(nèi) 前幾天去一家互聯(lián)網(wǎng)公司面試,面試官問到了這個(gè)應(yīng)該算是比較簡單的問題,在我自認(rèn)為回答正確時(shí),才知道這道題的答案有很多種,下面就讓我們一起來探討一下這...
摘要:在開發(fā)中經(jīng)常遇到這個(gè)問題,即讓某個(gè)元素的內(nèi)容在水平和垂直方向上都居中,內(nèi)容不僅限于文字,可能是圖片或其他元素。這篇文章就來總結(jié)一下都有哪些方法可以實(shí)現(xiàn)水平和垂直都居中。表示這些元素將相對于本容器水平居中,也是同樣的道理垂直居中。 在開發(fā)中經(jīng)常遇到這個(gè)問題,即讓某個(gè)元素的內(nèi)容在水平和垂直方向上都居中,內(nèi)容不僅限于文字,可能是圖片或其他元素。而且我們希望不要涉及寬度和高度,也就是說,我們不...
閱讀 1857·2021-11-11 16:55
閱讀 2655·2021-08-27 13:11
閱讀 3695·2019-08-30 15:53
閱讀 2362·2019-08-30 15:44
閱讀 1480·2019-08-30 11:20
閱讀 1101·2019-08-30 10:55
閱讀 992·2019-08-29 18:40
閱讀 3115·2019-08-29 16:13