所以我在擺弄CSS和前幾天浮動,偶然發現了一個奇怪的行爲(坦率地說,我很驚訝,我以前沒有遇到過)。我對它的原因感到困惑(以及爲什麼我不知道......)。div不會漂浮在前面的非浮動div旁邊
如果你有一個div(讓我們稱他爲鮑勃),並嘗試下一個浮動他到另一個DIV(吉米),它如果
- 吉米浮動太
- 吉米·鮑勃後談到只能
因此,如果我們有:
<div class="container">
<div id="one">Main Content 1</div>
<div id="two">Sidebar 1</div>
</div>
與
.container {
overflow:hidden; /* this essentially clears the floats. You could remove it and add a clearfloat div instead */
margin-bottom:10px;
}
#one {
background-color:red;
margin-right:50px;
}
#two {
width:50px;
float:right;
background-color:blue;
}
我們得到; 但如果我們只是交換#one
和#two
,保持相同的CSS:
<div class="container">
<div id="two">Sidebar 2</div>
<div id="one">Main Content 2</div>
</div>
我們得到:
爲什麼?我相信這是一個簡單的東西(這讓我感到很蠢)與盒子模型和float的定義有關,但是什麼?
你可以用它here
編輯最好解釋它。 –
重新分配由於編輯而接受的答案並不酷,朋友。 – AlienWebguy
@AlienWebguy,我的編輯實際上只是添加了第一個例子的解釋。原始答案解釋了爲什麼第二個例子有效,這是主要問題。公平地說,我也是第一個答案。儘管如此,+1。 –