<body>
<div?class="div1">已知div元素</div>
<div?class="x">X元素</div>
</body>css:<style>
div{width:200px;margin:?0;padding:?0;}
div.div1{height:35px;background:?pink;position:?fixed;?}
div.x{background:?green;margin:?15px?0?0;height:?40px;}
</style>
5 回答

肥仔匯
TA貢獻6條經驗 獲得超5個贊
margin: 15px 0 0;
你這一句只設置了三個?
分別對應
上 15px
左右 0px
下 0px
div1脫離文檔流的
你需要在.x中加入position:absolute
這樣的話div1就不會有上邊距了
修改后代碼如下
<style> div{width:200px;margin:?0;padding:?0;} .div1{height:35px;background:?pink;position:?fixed;?} .x{background:?green;?margin:?15px?0?0;height:?40px; position:?absolute;} </style>

加入滾燙開水
TA貢獻5條經驗 獲得超9個贊
當absolute 或 fixed,沒給left top這些定位的話,會保持在原位置。
基本的定位大家都會,想深入理解可以看?http://www.xianlaiwan.cn/learn/192

小堯
TA貢獻3條經驗 獲得超0個贊
body默認沒有高度,它根據里面的內容撐開高度,div1脫離文檔流,x是標準流、上邊距為15px,body根據第一個標準流撐開高度,隨x上邊距為15px,fixed定位沒有設置top/bottom/left/right偏移量定位時,被父層body包含隨body位置,當設置了偏移量,則以html為基準偏移,樓主并沒有設置偏移量,因此div1隨body上邊距為15px。
- 5 回答
- 0 關注
- 2342 瀏覽
添加回答
舉報
0/150
提交
取消