【发布时间】:2012-11-12 06:09:33
【问题描述】:
我有一个背景颜色为绿色的容器。在那个绿色的<div> 里面,我有一个黑色的<div>,它的样式是一个盒子(50px x 50px)——这个<div> 浮动到左边。
现在,如果我在灰色 <div> 之后将 <p> 添加到我的标记中,我希望它会在灰色 <div> 周围浮动。它确实如此。但是,当我替换
与另一个样式为框(设置高度和宽度)的<div> 一起,它不再浮动,而是消失在浮动 div 后面。
即使我有正确浮动的第一段然后我添加了 div(框),这些段落也会停止浮动并出现在另一行上。
HTML:
<div id="greencontainer">
<div id="blackbox"></div> <!--this one is float:left;-->
<p>A paragraph that floats around the previous div</p>
<div id="anotherbox"></div> <!-- if I add another div with set width and height, it disappears behind the floated one, and even prevents the paragraph from floating around.-->
</div>
CSS:
#blackbox {
width:50px;
height:50px;
background-color:black;
float:left;
}
#p {
margin:0px;
padding:0px;
}
#anotherbox {
width:50px;
height:50px;
background-color:grey;
}
【问题讨论】:
-
您是否也尝试过
float:left;另一个div? -
在您的示例代码中,只有 #blackbox 具有 float:left - 如果您想让另一个 div 也浮动,您需要使其也浮动:left