定位

靜態(tài)定位

position:static;??


相對定位

?position:relative? ? ? left:100px

①相對定位 以自己的左上角為基準(zhǔn)點(diǎn) 移動?

②相對定位移動后,位置仍然保留,是標(biāo)準(zhǔn)流,不脫標(biāo)

絕對定位

position:absolute;? top:0px;? ?right:0px;? bottom:0px;? left:0px;?

①以當(dāng)前可見窗口左上角為基準(zhǔn)點(diǎn)移動

②父親有定位,以父親的左上角為基準(zhǔn)點(diǎn)移動

③父親沒有定位,爺爺有定位。以爺爺?shù)淖笊辖菫榛鶞?zhǔn)點(diǎn)移動


注意:

加了定位和加了浮動的盒子 ,margin:0 auto;(居中對齊)就失效了,因為定位和浮動都是左對齊。

實(shí)現(xiàn)居中對齊的方法:

left:50%;(父盒子的一半)? margin-left:盒子的一半(負(fù)值)

或者

top:50%? ?(父盒子的一半) margin-top:盒子的一半(負(fù)值)

例如:

<!doctype html>

<html lang="en">

<head> <meta charset="UTF-8">

<title>Document</title>

<style>

.father { width: 300px; height: 300px; background-color: pink; margin: 100px auto; position: relative; }

.son { width: 50px; height: 50px; background-color: red; position: absolute; left: 50%; margin-left: -25px; }

</style>

</head>

<body>

? ????<div class="father">

????????????<div class="son"></div>

????</div>

</body>

</html>



<!doctype html>

<html lang="en">

<head> <meta charset="UTF-8">

<title>Document</title>

<style>

.father { width: 300px; height: 300px; background-color: pink; margin: 100px auto; position: relative; }

.son { width: 50px; height: 50px; background-color: red; position: absolute; left: 50%; margin-left: -25px; top: 50%; margin-top: -25px;}

</style>

</head>

<body>

? ????<div class="father">

????????????<div class="son"></div>

????</div>

</body>

</html>?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 知識點(diǎn) 文檔流 浮動作用 文本繞圖 制作導(dǎo)航 網(wǎng)頁布局 清除浮動的三種方式 額外標(biāo)簽法 父元素添加:overflo...
  • 定位 相對定位 什么是相對定位? 對定位就是相對于自己以前在標(biāo)準(zhǔn)流中的位置來移動 格式: position: re...
    Jackson_yee_閱讀 371評論 0 0
  • 1.文檔流的概念指什么?有哪種方式可以讓元素脫離文檔流? 將窗體自上而下分成一行行, 并在每行中按從左至右的順序排...
    26d608950683閱讀 502評論 0 0
  • 我相信很多初入大學(xué)的人都跟我有同樣的感受。 在我到大學(xué)之前,在我的想象中,大學(xué)是美好的,特別的,是自己向往的,但是...
    吃嶼閱讀 416評論 2 1
  • 不知道大家有沒有過這樣的經(jīng)歷。就是剛出生幾個月的孩子如果別人抱在手上哭鬧得很兇,但是到媽媽懷里,就會哭的沒那么厲害...
    艾米本人閱讀 2,714評論 0 0

友情鏈接更多精彩內(nèi)容