浮动的元素在文档中是脱离正常文档流的位置问题

按照标准来解释,如果容器层没有浮动,而它的内部元素浮动的话,它是没有高度的,因为它内部元素脱离的文档流,所以容器层的高度是不会被撑开的。


<div id="content" style="border: solid 1px black">
<div id="subcontent" style="float:left;height:500px;width:500px;background:red;">&nbsp;</div>
&nbsp;
</div>

解决这种问题的办法:
1.可以在容器层的最后添加一个额外的清除层。即加入“<div style="clear:both"></div>”

<div id="content" style="border: solid 1px black">
<div id="subcontent" style="float:left;height:500px;width:500px;background:red;">&nbsp;</div>
&nbsp;
<div style="clear:both"></div>
</div>

2.让容器层也浮动

<div id="content" style="float:left;border: solid 1px black">
<div id="subcontent" style="float:left;height:500px;width:500px;background:red;">&nbsp;</div>
&nbsp;
</div>

posted on 2008-11-20 09:24  asum  阅读(388)  评论(0)    收藏  举报

导航