鍍金池/ 問(wèn)答/HTML/ 父級(jí)相對(duì)定位 子級(jí)絕對(duì)定位

父級(jí)相對(duì)定位 子級(jí)絕對(duì)定位

這個(gè)是css代碼:

<style>
        .wrapper{
            position: relative;
            background-color:aquamarine;   
        }
        .header{
            border: 1px solid red;
            position: absolute;  
            background-color:aquamarine;
        }
        .baidulogo{
            margin-top: 200px;
            text-align: center;
            border: 1px solid black;
            background-color: #3388ff;          
        }
        baidulogo img{
            width: 270px;
            height: 129px;
        }
    </style>
    這個(gè)是html部分
    <div class="wrapper">
    <div class="header">
        <sapn class="nav">新聞</sapn>
    </div>
</div>
<div class="baidulogo">
    <img src="bd_logo1.png">
</div>
問(wèn)題:為什么改變.header的margin-top值.header的位置也跟著改變,是什么原理?
回答
編輯回答
笨小蛋

absolute定位與margin定位其實(shí)是沒(méi)有什么沖突的,無(wú)論absolute元素時(shí)候設(shè)置了left/top值,其margin屬性值都是可以起作用的。下面展示的是沒(méi)有l(wèi)eft/top值的absolute元素的margin定位。
可以看一下這一片文章:http://www.zhangxinxu.com/wor...

2017年9月2日 21:28
編輯回答
壞脾滊

由于header是絕對(duì)定位提升層級(jí),.baidulogo會(huì)擠上去造成 margin-top傳遞給父級(jí)。關(guān)于margin-top折疊可以參照 https://www.w3cplus.com/css/u...

2018年7月13日 14:52