使用真棒display:table
。
<div style="background-color: aquamarine; margin:50px; display:table">
<div style="background-color: azure;width:25%;display:table-cell">
1
</div>
<div style="background-color: darkolivegreen;width:25%;display:table-cell">
2
</div>
<div style="background-color: darkorange;width:25%;display:table-cell">
3
</div>
<div style="background-color: bisque;width:25%;display:table-cell">
4
</div>
</div>
事实上,你甚至不需要指定内部div的宽度。使用table-layout:fixed
,浏览器将自动计算宽度并很好地布置。 :) 一定要在父div上指定一个宽度。
<div style="background-color: aquamarine; margin:50px; width:100%; display:table; table-layout:fixed">
<div style="background-color: azure;display:table-cell">
1
</div>
<div style="background-color: darkolivegreen;display:table-cell">
2
</div>
<div style="background-color: darkorange;display:table-cell">
3
</div>
<div style="background-color: bisque;display:table-cell">
4
</div>
</div>
伟大的代码。谢谢苛刻 – user1799345
不客气! –