当你浮动元素时,你应该提供浮动元素的宽度。否则,您可能会遇到不同浏览器的意外行为。
Check this tutorial,有漂浮在CSS的很好的信息。 [链接已死]
基本上,如果你提供一个overflow:hidden;
到容器div和提供宽度的浮动元素,你的问题将得到解决。
<div style="overflow: hidden;">
<div style="float:left; width: 300px;">Some text</div>
<div style="float:right; width: 300px;">Some text</div>
</div>
同样,无论你想正常化流量IKE可以添加另一个DIV这样的:
<div>
<div style="float:left; width: 300px;">Some text</div>
<div style="float:right; width: 300px;">Some text</div>
<div style="clear:both;"></div>
<div>This div will be at the same place
as if the previous elements are not floated</div>
</div>
双方将工作:)
编辑
另一种方法,我在这几天经常使用的是浮动第一个元素,并将margin-left
设置为以下元素。例如:
<div>
<div style="float: left; width: 300px;">Some text</div>
<div style="margin-left: 300px;">Some text</div>
<div style="clear: both;"></div>
</div>
该方法的优点是以下元素(在这种情况下的第二个div)不需要固定宽度。另外,你可以跳过第三格(clear: both;
)。这是可选的。我只是添加它,以防浮动div的高度比第二个div长,因为如果您不添加它,父div将始终获得第二个div的高度。
Yuck!尽可能不要使用'clear:both'黑客。几乎总是(/总是)更好的解决方案。 – PeeHaa
我并不知道“overflow:hidden”技巧。 – Gareth
看起来像你今天学到的东西:)为了完成我的评论和教育你更多;)当你想使用CSS3(例如拖放阴影)时,注意使用'overflow:hidden;'黑客。幸运的是,我们也有一个解决方案:http://fordinteractive.com/2009/12/goodbye-overflow-clearing-hack/ – PeeHaa