Therefore achieving what you are asking for is a bit more difficult.
(因此,实现您的要求会有点困难。)
In my experience, the "cleanest" way that doesn't use :first-child
/ :last-child
and works without any modification on flex-wrap:wrap
is to set padding:5px
on the container and margin:5px
on the children.
(以我的经验,不使用:first-child
/ :last-child
且不对flex-wrap:wrap
进行任何修改的“最干净”方式是在容器上设置padding:5px
,在子padding:5px
上设置margin:5px
。)
That will produce a 10px
gap between each children and between each children and their parent. (这将在每个孩子之间以及每个孩子与其父母之间产生10px
间隙。)
Demo
(演示版)
.upper { margin:30px; display:flex; flex-direction:row; width:300px; height:80px; border:1px red solid; padding:5px; /* this */ } .upper > div { flex:1 1 auto; border:1px red solid; text-align:center; margin:5px; /* and that, will result in a 10px gap */ } .upper.mc /* multicol test */ {flex-direction:column;flex-wrap:wrap;width:200px;height:200px;}
<div class="upper"> <div>aaa<br/>aaa</div> <div>aaa</div> <div>aaa<br/>aaa</div> <div>aaa<br/>aaa<br/>aaa</div> <div>aaa</div> <div>aaa</div> </div> <div class="upper mc"> <div>aaa<br/>aaa</div> <div>aaa</div> <div>aaa<br/>aaa</div> <div>aaa<br/>aaa<br/>aaa</div> <div>aaa</div> <div>aaa</div> </div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…