我有一个工作代码在这里:http://jsfiddle.net/WVm5d/(你可能需要使结果窗口更大,以看到对齐中心效果)
问题
代码工作正常,但我不喜欢有display: table;
。这是唯一能让包装类对齐居中的方法。我认为如果有一种使用display: block;
或display: inline-block;
的方法会更好。是否可以用另一种方法解决对齐中心问题?
在容器中添加固定的with对我来说不是一个选项。
我也将粘贴我的代码在这里,如果JS提琴链接在未来被打破:
body {
background: #bbb;
}
.wrap {
background: #aaa;
margin: 0 auto;
display: table;
overflow: hidden;
}
.sidebar {
width: 200px;
float: left;
background: #eee;
}
.container {
margin: 0 auto;
background: #ddd;
display: block;
float: left;
padding: 5px;
}
.box {
background: #eee;
border: 1px solid #ccc;
padding: 10px;
margin: 5px;
float: left;
}
.box:nth-child(3n+1) {
clear: left;
}
<div class="wrap">
<div class="sidebar">
Sidebar
</div>
<div class="container">
<div class="box">
Height1
</div>
<div class="box">
Height2<br />
Height2
</div>
<div class="box">
Height3<br />
Height3<br />
Height3
</div>
<div class="box">
Height1
</div>
<div class="box">
Height2<br />
Height2
</div>
<div class="box">
Height3<br />
Height3<br />
Height3
</div>
</div>
<div class="sidebar">
Sidebar
</div>
</div>