我知道如何让2个divs并排浮动,简单地浮动一个到左边,另一个到右边。

但是如何用3个div来做这个,或者我应该只用表来做这个目的吗?


当前回答

但它在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;

}

其他回答

只要给它们一个宽度和float: left;,这里有一个例子:

<div style="width: 500px;">
 <div style="float: left; width: 200px;">Left Stuff</div>
 <div style="float: left; width: 100px;">Middle Stuff</div>
 <div style="float: left; width: 200px;">Right Stuff</div>
 <br style="clear: left;" />
</div>

你可以为它们设置float: left,并将宽度设置为33.333%

<style>
.left-column
{
float:left;
width:30%;
background-color:red;
}
.right-column
{
float:right;
width:30%;
background-color:green;
}
.center-column
{
margin:auto;
width:30%;
background-color:blue;
}
</style>

<div id="container">
<section class="left-column">THIS IS COLUMN 1 LEFT</section>
<section class="right-column">THIS IS COLUMN 3 RIGHT</section>
<section class="center-column">THIS IS COLUMN 2 CENTER</section>
</div>

这种方法的优点是你可以设置每一列的宽度,只要你保持在100%以下,如果你使用3 x 30%,剩下的10%被分割为5%的分隔空间列之间

<br style="clear: left;" />

有人在上面发布的代码,成功了!! 当我在关闭容器DIV之前粘贴它时,它有助于清除所有后续DIVs与我在顶部并排创建的DIVs重叠!

<div>
<div class="left"></div>
<div class="left"></div>
...
...
<div class="left"></div>
<!--  then magic trick comes here  -->
<br style="clear: left;" />
</div>

tadaa ! !:)

下面是我如何在<footer>元素中做类似的事情:

<div class="content-wrapper">

    <div style="float:left">
        <p>&copy; 2012 - @DateTime.Now.Year @Localization.ClientName</p>
    </div>

    <div style="float:right">
        <p>@Localization.DevelopedBy <a href="http://leniel.net" target="_blank">Leniel Macaferi</a></p>
    </div>

    <div style="text-align:center;">
        <p>☎ (24) 3347-3110 | (24) 8119-1085&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;✉ @Html.ActionLink(Localization.Contact, MVC.Home.ActionNames.Contact, MVC.Home.Name)</p>
    </div>

</div>

CSS:

.content-wrapper
{
    margin: 0 auto;
    max-width: 1216px;
}