根本不要浮动右列,只要给它足够大的边距以容纳左列。浮动元素从正常的文档流中移除,并且对它们的父元素的高度没有任何贡献;所以,如果你浮动左右两列,你的红色 #box 元素最终没有高度,你看不到它;如果你停止浮动右列,那么它真的会确定#box 的高度。如果您根本不浮动#right_column,那么它将扩展以使用#box 中的所有可用宽度。
类似这样的:
<div id="container">
<div id="box">
<div id="left_column">
<p>details stuff yada yada yada</p>
</div>
<div id="right_column">
<p>other stuff yada yada yada test test test test test stuff stuff content content content content content stuff stuff example stuff test stuff content content stuff content example</p>
</div>
</div>
</div>
CSS:
#container {
width: 400px;
}
#box {
background-color: red;
}
#left_column {
width: 200px;
background-color: blue;
float: left;
}
#right_column{
margin: 0 0 0 200px;
background-color: green;
}
更新小提琴:http://jsfiddle.net/ambiguous/eDTdQ/
或者,您可以将width: 200px 添加到#right_column 并让它保持浮动,然后将overflow: hidden 添加到#box 以便#box 扩展以包含其浮动的孩子:
#box {
background-color: red;
overflow: hidden;
}
#right_column{
background-color: green;
float: left;
width: 200px;
}
此方法的实时版本:http://jsfiddle.net/ambiguous/eDTdQ/2/
如果您希望右列自动拉伸并且您希望两列都是全高,那么您可以绝对定位左列而不是浮动它:
#box {
background-color: red;
position: relative;
}
#left_column {
width: 200px;
background-color: blue;
position: absolute;
top: 0;
left: 0;
bottom: 0;
}
直播:http://jsfiddle.net/ambiguous/3Cxe3/