HTML/CSS - 为什么 float:left 呈现为“不可见”?
如果一个 div 中包含两个 div:
<div style="border:1px;">
<div style="float:left;background-color:red;width:20px;height:20px;">
<div style="float:left;background-color:red;width:20px;height:20px;">
</div>
两个内部 div 会呈现为“不可见”,因为容器 div 不会拉伸以允许它们填充,就好像它们不存在一样。这会产生难看的重叠/空白等。
您如何解决这个问题?
If you have two divs contained within a div:
<div style="border:1px;">
<div style="float:left;background-color:red;width:20px;height:20px;">
<div style="float:left;background-color:red;width:20px;height:20px;">
</div>
The two inner divs are rendered as 'invisible', as in the container div doesn't stretch to allow them to fill, as if they were not there. This creates ugly overlaps/whitespace etc.
How do you go about resolving this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
插入第三个div:
Insert the third div:
我认为这可能是因为您忘记关闭标签,请尝试以下操作:
I think it may be because you are forgetting to close the tags, try this:
尝试添加
(或clear left),这是赋予浮动元素生命力的常用方法。容器Try to add the
<br style="clear:both"/>
, (or clear left) it is a common method to give life to floated elements within a container父元素永远不会扩展以包含浮动子元素。虽然 IE<8 确实做到了这一点,但这是这个无能的浏览器中长期存在的错误(数百万个错误之一)。最好的解决方案是浮动父级、设置高度/宽度或在 CSS 中使用 Overflow:auto。
Parent elements are never to expand to contain floated children. While IE<8 does do this, that's a long standing bug (one of millions) in that inept browser. The best solutions are to float the parent, set the height/width, or use overflow:auto in the CSS.
外部 div 不是浮动的,因此除非您显式设置高度,否则在这种情况下它不会显示(边框除外)。将外部 div 的高度设置为 20px 或使其浮动。
The outer div isn't floated, so unless you explicitly set a height it won't display in this case (other than the border). Either set height of outer div to 20px or float it.
您没有/不能在 div 中放置任何内容是否有原因?由于没有内容,它们重叠/显示不可见。
将显示相邻的两个 div(已测试 IE6、Chrome 3、Firefox 3.5、IE7)
Is there a reason why you aren't/can't put any content in the divs? They overlap / are displayed invisible since there is no content.
Will show the two divs next to eachother (Tested IE6, Chrome 3, Firefox 3.5, IE7)