我知道如何让2个divs并排浮动,简单地浮动一个到左边,另一个到右边。
但是如何用3个div来做这个,或者我应该只用表来做这个目的吗?
我知道如何让2个divs并排浮动,简单地浮动一个到左边,另一个到右边。
但是如何用3个div来做这个,或者我应该只用表来做这个目的吗?
当前回答
我更喜欢这种方法,浮动在旧版本的IE中支持很差(真的吗?…)
.column-left{ position:absolute; left: 0px; width: 33.3%; background: red; }
.column-right{position:absolute; left:66.6%; width: 33.3%; background: green; }
.column-center{ position:absolute; left:33.3%; width: 33.3%; background: yellow; }
更新: 当然,要使用这种技术,由于绝对定位,你需要在容器上封装div,并做后处理来定义if的高度,类似这样:
jQuery(document).ready(function(){
jQuery('.main').height( Math.max (
jQuery('.column-left').height(),
jQuery('.column-right').height(),
jQuery('.column-center').height())
);
});
这不是世界上最神奇的东西,但至少不会砸到老的ie。
其他回答
三次跳水都向左飘。像在这里:
.first-div {
width:370px;
height:150px;
float:left;
background-color:pink;
}
.second-div {
width:370px;
height:150px;
float:left;
background-color:blue;
}
.third-div {
width:370px;
height:150px;
float:left;
background-color:purple;
}
但它在Chrome上能正常工作吗?
浮动每个div并设置为清除;都是针对行。不需要设置宽度,如果你不想。适用于Chrome 41,Firefox 37, IE 11
点击获取JS小提琴
HTML
<div class="stack">
<div class="row">
<div class="col">
One
</div>
<div class="col">
Two
</div>
</div>
<div class="row">
<div class="col">
One
</div>
<div class="col">
Two
</div>
<div class="col">
Three
</div>
</div>
</div>
CSS
.stack .row {
clear:both;
}
.stack .row .col {
float:left;
border:1px solid;
}
这与你为两个div所做的方式相同,只是将第三个div浮动到左或右。
<style>
.left{float:left; width:33%;}
</style>
<div class="left">...</div>
<div class="left">...</div>
<div class="left">...</div>
@Leniel这个方法很好,但是你需要为所有浮动div添加宽度。我会说,让它们宽度相等或分配固定宽度。类似的
.content-wrapper > div { width:33.3%; }
您可以为每个div分配类名,而不是添加内联样式,这不是一个好的实践。
请确保使用clearfix div或clear div,以避免下面的内容仍然低于这些div。
你可以在这里找到如何使用clearfix div的详细信息
现代的方法是使用CSS flexbox,参见支持表。
.container { 显示:flex; } .container > div { flex: 1;/ * * / } < div class = "容器" > < div >左div < / div > 中产div < div > < / div > < div >正确的div < / div > < / div >
也可以使用CSS网格,参见支持表格。
.container { 显示:网格; Grid-template-columns: 1fr 1fr 1fr;/ * * / } < div class = "容器" > < div >左div < / div > 中产div < div > < / div > < div >正确的div < / div > < / div >