我有以下代码:HTML浮动问题
<div "background-color:green">
<div "float:left">something</div>
<div "float:right:>something else</div>
<div>
为什么背景色不会出现在这种情况下?需要做些什么才能使其出现 {为了理解而简化了代码,可能不在合适的语法中}
我有以下代码:HTML浮动问题
<div "background-color:green">
<div "float:left">something</div>
<div "float:right:>something else</div>
<div>
为什么背景色不会出现在这种情况下?需要做些什么才能使其出现 {为了理解而简化了代码,可能不在合适的语法中}
你需要清除DIV您可以在元素上使用clear: both
下,但我常常觉得这是比较容易:
<div style="background-color:green; overflow: hidden;">
<div style="float:left;">something</div>
<div style="float:right;">something else</div>
<div>
注意:溢出:隐藏。
当然,它只适用于你不需要元素离开其包含的元素。
浮动对象从其包含器中“升高”。外部div的底部边缘不再延伸到其内容。
一种选择是,以明确的添加元素(明确需要一个方向(或者left
,right
,或both
),并按下浮子下方本身它会触及:
<div style="background-color: green">
<div style="float: left">something</div>
<div style="float: right">something else</div>
<br style="clear: both;" />
<div>
你需要写在style属性
<div style="background-color:green;">
<div style="float:left;">something</div>
<div style="float:right;">something else</div>
<div>
为了简单起见,我假设他忽略了它。 – Tordek 2009-10-23 04:58:52
永远不要低估人...... – alex 2009-10-23 04:59:43
我其实希望我错了。 – ChaosPandion 2009-10-23 05:00:11
好的答案!它也适用于我。但你知道它为什么有效吗? – 2012-04-02 20:27:55
@ 50ndr33我曾经,但现在不记得了。 – alex 2012-04-02 21:39:14