因此,我將clear:left應用于div并嘗試更改其上邊距,但它沒有影響任何內容。這是代碼:<!DOCTYPE html><html><head> <title>Float</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .div1 { border: 2px solid black; float: left; } .div2 { clear: left; border: 2px solid red; width: 120px; height: 120px; margin-top: 320px; /* Why is margin not working?*/ } </style></head><body> <div class="div1">This is suppose to be a first div</div> <div class="div2">Div2</div></body></html>結果如下:現在的問題是,即使我添加或刪除上邊距,結果也是相同的。如果您能向我展示正確的答案并解釋為什么會發生這種情況,那就太好了。
1 回答

湖上湖
TA貢獻2003條經驗 獲得超2個贊
使用一個容器如下:
<div class="con" style="overflow: hidden"> <div class="div1">This is suppose to be a first div</div></div>
.div1 {
border: 2px solid black;
float: left;
}
.div2 {
border: 2px solid red;
width: 120px;
height: 120px;
margin-top: 100px;
}
<div class="con" style="overflow: hidden">
<div class="div1">This is suppose to be a first div</div>
</div>
<div class="div2">Div2</div>
- 1 回答
- 0 關注
- 184 瀏覽
添加回答
舉報
0/150
提交
取消