我想有3个div对齐在一个容器div,就像这样:

[[LEFT]       [CENTER]        [RIGHT]]

容器div是100%宽(没有设定宽度),中心div在调整容器大小后应该保持在中心。

所以我设置:

#container{width:100%;}
#left{float:left;width:100px;}
#right{float:right;width:100px;}
#center{margin:0 auto;width:100px;}

但它变成了:

[[LEFT]       [CENTER]              ]
                              [RIGHT]

任何建议吗?


当前回答

HTML:

<div id="container" class="blog-pager">
   <div id="left">Left</div>
   <div id="right">Right</div>
   <div id="center">Center</div>    
</div>

CSS:

 #container{width:98%; }
 #left{float:left;}
 #center{text-align:center;}
 #right{float:right;}

text-align:中心;给予完美的中心对齐。

JSFiddle演示

其他回答

最简单的解决方案是用3列组成一个表,并将该表居中。

html:

 <div id="cont">
        <table class="aa">
            <tr>
                <td>
                    <div id="left">
                        <h3 class="hh">Content1</h3>
                        </div>
                    </td>
                <td>
                    <div id="center">
                        <h3 class="hh">Content2</h3>
                        </div>
                 </td>
                <td>
                    <div id="right"><h3 class="hh">Content3</h3>
                        </div>
                </td>
            </tr>
        </table>
    </div>

css:

#cont 
{
  margin: 0px auto;    
  padding: 10px 10px;
}

#left
{    
  width: 200px;
  height: 160px;
  border: 5px solid #fff;
}

#center
{
  width: 200px;
  height: 160px;
  border: 5px solid #fff;
}

#right
{
  width: 200px;
  height: 160px;
  border: 5px solid #fff;
}
.processList
  text-align: center
  li
  .leftProcess
    float: left
  .centerProcess
    float: none
    display: inline-block
  .rightProcess
    float: right

html
ul.processList.clearfix
  li.leftProcess

li.centerProcess
li.rightProcess

有一些技巧可以用于对齐元素。

01. 使用桌子戏法

.container { 显示:表; } .left { 背景:绿色; 显示:表格单元; 宽度:33.33大众; } .center { 背景:黄金; 显示:表格单元; 宽度:33.33大众; } 铃声{ 背景:灰色; 显示:表格单元; 宽度:33.33大众; } < div class = "容器" > < div class = "左" > 左 < / div > < div class = "中心" > 中心 < / div > < div class = "正确" > 正确的 < / div > < / div >

02. 使用弹性技巧

.container { 显示:flex; justify-content:中心; 对齐项目:中心; } .left { 背景:绿色; 宽度:33.33大众; } .center { 背景:黄金; 宽度:33.33大众; } 铃声{ 背景:灰色; 宽度:33.33大众; } < div class = "容器" > < div class = "左" > 左 < / div > < div class = "中心" > 中心 < / div > < div class = "正确" > 正确的 < / div > < / div >

03. 使用浮动把戏

.left { 背景:绿色; 宽度:100 px; 浮:左; } .center { 背景:黄金; 宽度:100 px; 浮:左; } 铃声{ 背景:灰色; 宽度:100 px; 浮:左; } < div class = "容器" > < div class = "左" > 左 < / div > < div class = "中心" > 中心 < / div > < div class = "正确" > 正确的 < / div > < / div >

如果你不想改变你的HTML结构,你也可以添加text-align: center;到包装器元素和一个display: inline-block;到居中的元素。

#container {
    width:100%;
    text-align:center;
}

#left {
    float:left;
    width:100px;
}

#center {
    display: inline-block;
    margin:0 auto;
    width:100px;
}

#right {
    float:right;
    width:100px;
}

现场演示:http://jsfiddle.net/CH9K8/

你已经正确地做了,你只需要清除你的浮动。 简单的添加

overflow: auto; 

到容器类。