为什么不让它们向左浮动呢? (并调整你的容器大小以适应它们..)
这样您可以更好地控制边距,因为 font-size 不会影响它们(空格也不)
#mainparent
{
width:480px;
height:200px;
background:#ccc;
}
.boxes
{
width:50px;
height:50px;
float:left;
margin:5px;
background:#000;
}
http://jsfiddle.net/JxhP8/32/的演示
更新
如果您希望元素彼此保持对齐但在其容器中居中对齐,您将需要中间的另一个元素和一些 javascript..
这是一个jquery实现
html
<div id="mainparent">
<div class="centerized">
<div class="boxes"></div>
<div class="boxes"></div>
<div class="boxes"></div>
..
..
<div class="boxes"></div>
<div class="boxes"></div>
<div class="boxes"></div>
</div>
</div>
css
#mainparent
{
background:#ccc;
}
.centerized{
overflow:hidden; // to grow according to the boxes
margin:0 auto; // to be centered in container
}
jquery
$(function(){
// cache the repeatedly used elements
// and fixed values
var main = $('#mainparent'),
centerized = main.find('.centerized'),
itemWidth = main.find('.boxes').outerWidth(true);
$(window).resize(function(){
var fitItems = (main.width() / itemWidth) | 0;
centerized.width(fitItems * itemWidth);
}).trigger('resize');
});
演示地址 http://jsfiddle.net/JxhP8/34/