.container {
width: 400px;
border: 2px solid red;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
}
.item {
padding: 20px;
border: 1px solid blue;
margin: 10px;
width: 70px;
}
<div class="container">
<div class=item>Box 1</div>
<div class=item>Box 2</div>
<div class=item>Box 3</div>
<div class=item>Box 4</div>
<div class=item>Box 5</div>
</div>
我的问题是,如何将Box 5
旁边的Box 4
移动到Box 5
旁边而又没有空格?目前,Box 4
移到了右端,public
则留在了左边。
答案 0 :(得分:1)
有很多方法可以实现这一目标。只需从样式中删除justify-content
属性即可。会没事的。
您还可以包装box 4
和box 5
,并提供根容器(在本例中为item-special-container)display: flex
。
.container {
width:400px;
border:2px solid red;
display:flex;
flex-flow:row wrap;
justify-content:space-between;
}
.item {
padding:20px;
border:1px solid blue;
margin:10px;
width:70px;
}
.item-special-container {
display: flex;
}
<div class="container">
<div class=item>Box 1</div>
<div class=item>Box 2</div>
<div class=item>Box 3</div>
<div class="item-special-container">
<div class=item>Box 4</div>
<div class=item>Box 5</div>
<div>
</div>
答案 1 :(得分:1)
在这种特定的布局中,如果3n + 2
子项.item
也是last-child
子项,则会出现问题:为此元素调整右页边距
.container {
width: 400px;
border: 2px solid red;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
}
.item {
padding: 20px;
border: 1px solid blue;
margin: 10px;
width: 70px;
}
.container :last-child:nth-child(3n + 2) {
margin-right: auto;
}
<div class="container">
<div class=item>Box 1</div>
<div class=item>Box 2</div>
<div class=item>Box 3</div>
<div class=item>Box 4</div>
<div class=item>Box 5</div>
</div>