Html 带子div的div的背景色 div { 背景色:#ccc; } 这是div元素中的文本。 我们仍然在div元素中。
为什么背景色不显示在这两个div之间?Html 带子div的div的背景色 div { 背景色:#ccc; } 这是div元素中的文本。 我们仍然在div元素中。,html,background-color,Html,Background Color,为什么背景色不显示在这两个div之间? 浮动图元时,应提供浮动图元的宽度。否则,您可能会在不同浏览器之间遇到意外行为 ,css中有关于浮动的好信息。[链接已断开] 基本上,如果提供溢出:隐藏到容器div并为浮动元素提供宽度,您的问题将得到解决 <html> <head> <style type="text/css"> div { background-color:#ccc; } </style> </head> <body>
浮动图元时,应提供浮动图元的宽度。否则,您可能会在不同浏览器之间遇到意外行为 ,css中有关于浮动的好信息。[链接已断开] 基本上,如果提供
溢出:隐藏代码>到容器div并为浮动元素提供宽度,您的问题将得到解决
<html>
<head>
<style type="text/css">
div
{
background-color:#ccc;
}
</style>
</head>
<body>
<div>
<div style="float: left;">This is a text inside a div element.</div>
<div style="float: right;">We are still in the div element.</div>
</div>
</body>
</html>
此方法的优点是以下元素(本例中的第二个div)不需要固定的宽度。另外,您可以跳过第三个div(清除:两者;
)。这是可选的。我只是在浮动div的高度比第二个div长的情况下添加它,因为如果不添加它,父div将始终获得第二个div的高度。只需将容器div设置为溢出:隐藏代码>
如果将元素设置为浮动,它们将不再处于文档的正常“流”中
<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
s都是浮动的,所以包含div
的部分没有高度。如果要添加第三个子元素div
而它不是浮点,请将其高度设置为0
和清除:这两个元素都会显示背景色。浮点元素不会影响父元素的大小,除非父元素使用溢出
样式专门包含子元素
外部div的背景色与子div相同,但父div的高度为零,因此您看不到其背景。显示的空白是主体部分,您将背景色设置为div,但不在主体中。这就是身体部分是空的原因
要给空零件上色,应添加以下代码:
div
{
背景色:#ccc;
}
身体{
背景色:#ccc;
}
这是div元素中的文本。
我们仍然在div元素中。
可以通过更改“主体样式”中的背景色来更改主体背景色
讨厌!尽可能不要使用清除:两个攻击。几乎总是有(/总是?)更好的解决方案。看起来你今天学到了一些东西:)来完成我的评论并对你进行更多的教育;)使用溢出:隐藏时要小心代码>当你想使用CSS3(例如放置阴影)时进行黑客攻击。幸运的是,我们也有一个解决方案:
<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>
<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 { background: #ccc; overflow: hidden; }
<html>
<head>
<style type="text/css">
div
{
background-color:#ccc;
}
body{
background-color:#ccc;
}
</style>
</head>
<body>
<div>
<div style="float: left;">This is a text inside a div element.</div>
<div style="float: right;">We are still in the div element.</div>
</div>
</body>
</html>