Html 为什么背景色对这个DIV没有影响? 你好 世界

Html 为什么背景色对这个DIV没有影响? 你好 世界,html,css,Html,Css,为什么背景色不显示为黑色?我无法设置宽度和浮动,没有它们可以吗?由于外部div仅包含浮动div,因此它以0高度渲染。为其指定高度或将其溢出设置为隐藏。将其更改为: <div style="background-color:black; overflow:hidden;" onmouseover="this.bgColor='white'"> <div style="float:left">hello</div> <div style="float:rig

为什么背景色不显示为黑色?我无法设置宽度和浮动,没有它们可以吗?

由于外部div仅包含浮动div,因此它以0高度渲染。为其指定高度或将其溢出设置为隐藏。

将其更改为:

<div style="background-color:black; overflow:hidden;" onmouseover="this.bgColor='white'">
<div style="float:left">hello</div>
<div style="float:right">world</div>
</div>

你好
世界
基本上,外部div只包含浮点数。浮子从正常流量中移除。因此,外部div实际上不包含任何内容,因此没有高度。它确实是黑色的,但你就是看不见

overflow:hidden属性基本上使外部div包围浮点数。另一种方法是:

<div style="background-color:black" onmouseover="this.bgColor='white'">
<div style="float:left">hello</div>
<div style="float:right">world</div>
<div style="clear:both></div>
</div>

你好
世界

浮动没有高度,因此包含的div的高度为零

<div style="background-color:black; overflow:hidden;zoom:1" onmouseover="this.bgColor='white'">
<div style="float:left">hello</div>
<div style="float:right">world</div>
</div>

你好
世界
溢出:隐藏清除大多数浏览器的浮动


zoom:1清除IE的浮动。

这是一个非常老的问题,但值得补充的是,我刚刚遇到了一个类似的问题,在我的案例中,
页脚
元素上的背景色没有显示。我添加了一个
职位:relative
,这很有效。

将您的标题改为信息量更大的标题。其他人将来可能也会遇到同样的问题,所以让他们很容易找到你的问题:)我不知道这个问题的答案到底是什么。虽然Lobstroity的答案很好,但这是一个更好的解决方案。你不必显式声明高度(你可能不知道)。我还喜欢使用
@cletus,如果我们有动态的内部div,并且我们没有使它们浮动的选项,我们应该怎么做,