【发布时间】:2019-08-09 10:31:40
【问题描述】:
我有一个我想用 CSS 显示的项目列表。最初,一行中只有两个并排的项目,但现在我想让它响应更大的屏幕,所以我想让它在一行上显示 3 个项目。我的旧代码使用 justify-content:space-between 看起来像这样。显示奇数个项目看起来不错。
.flex-container-old{
margin-top: 50px;
background: magenta;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.box-old{
width: 40%;
border: 1px solid black;
margin-bottom: 20px;
height: 300px;
background: orange;
}
.wrapper{
margin: 0 auto;
width: 80%;
}
body{
background:#D3D3D3;
}
<div class="wrapper">
<div class="flex-container-old">
<div class="box-old">
</div>
<div class="box-old">
</div>
<div class="box-old">
</div>
<div class="box-old">
</div>
<div class="box-old">
</div>
</div>
</div>
所以很自然地,我通过修改 width 属性将其扩展到一行中的三个项目,最终得到以下结果。
.flex-container-new{
background: lightblue;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.box {
width: 30%;
border: 1px solid black;
margin-bottom: 20px;
height: 300px;
background: orange;
}
.wrapper{
margin: 0 auto;
width: 80%;
}
<div class="wrapper">
<div class="flex-container-new">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
</div>
在上面一行包含三个项目的代码中,我的问题是我希望最后一行中的最后一个项目被推到左边,与上面一行中的中间项目对齐。可悲的是,引导程序不是一种选择。这是出于学习目的。有没有办法只用 CSS 就可以实现上述目标?非常感谢。
【问题讨论】: