您看到您描述的行为的原因是因为这些是您给予DIV的指示。
让我们来分析一下:
<div style="border: solid 1px navy; float: left;">
这依赖于默认的行为之外的所有边界,并通过浮动的,那你就告诉框本身引脚到左边距(它的父,这里假设是身体),不管其他什么属于那里。 DIV的默认宽度是父对象宽度的100%(仍然是body标签)。默认位置将成为流中的下一个块元素 - 由于没有其他任何块元素,因此它将与顶部边距垂直对齐。
然后你问另一位DIV做到这一点:
<div style="background-color: blue; float:left;">
基本上是同样的事情。在这里,你还没有给出DIV一个新的宽度或任何额外的指令,说明它们现在应该布局的位置,所以它将自己固定到左边界,并且它的顶部边距找到最近的块元素来钉住(仍然是主体)。
要得到这些并排排队边,请执行下列操作:
<style type="text/css">
.leftBox, .rightBox
{
width: 48%; /*leave some room for varying browser definitions */
border: 1px solid navy;
float: left;
display: inline; /* follow the semantic flow of the page and don't break the line */
clear: left; /* don't allow any other elements between you and the left margin */
}
.rightBox
{
border: none;
background-color: blue;
clear: right;
}
</style>
<div class="leftBox">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
<div class="rightBox">
<p>
some other text</p>
</div>
这改变了第二次DIV的宽度。请参阅其他答案替代解决方案:http://stackoverflow.com/questions/356835/working-with-divs-css#356877 – 2008-12-10 18:14:30