您可以使用并添加到es,而不是floates 。.boxdisplay: inline-blocktext-align: justify#container
但它仍然不能按预期工作,所以你把div你用于 clearfix 的额外部分#container放在标记中的底部。
然后将类的名称从更改为.clearfix类似.spacer; 间隔类将具有:display: inline-block和width: 100%。
您还需要提供.boxes vertical-align: top;,否则如果inline框中存在诸如文本之类的元素,则水平对齐可能会中断。
您现在会注意到,这些框之间的间距适当,并且一行中的第一个和最后一个框粘在边缘上。
JSFIDDLE
标记:
<div id="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box">the last box needs no margin right when full width and responsive</div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box">the last box needs no margin right when full width and responsive</div>
<div class='spacer'></div>
</div>
CSS:
* {
box-sizing: border-box; /* www.paulirish.com/2012/box-sizing-border-box-ftw/ */
}
#container {
width: 480px;
border:1px solid black;
text-align: justify;
}
.box {
display: inline-block;
vertical-align: top;
width: 100px;
height: 100px;
background:red;
margin-bottom:20px;
color:white;
}
.spacer {
display: inline-block;
width: 100%;
}
这是一篇文章,详细解释了这种方法http://www.barrelny.com/blog/text-align-justify-and-rwd/。