css - 如何将 flexbox 元素移动到另一行?
问题描述
该页面的元素在桌面上排成一行。在移动版中,中间的一个元素要换行,flex怎么能做到这一点呢?
.container {
display: flex;
}
.item {
width: 100px;
height: 100px;
display: block;
}
.item:nth-child(1) {
background: green;
}
.item:nth-child(2) {
background: red;
}
.item:nth-child(3) {
background: blue;
margin-left: auto;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
解决方案
您可以尝试如下:
.container {
display: flex;
}
.item {
width: 100px;
height: 100px;
display: block;
}
.item:nth-child(1) {
background: green;
}
.item:nth-child(2) {
background: red;
}
.item:nth-child(3) {
background: blue;
margin-left: auto;
}
@media screen and (max-width: 720px) {
.container {
flex-wrap: wrap;
}
.item:nth-child(2) {
order: 3;
}
.item:nth-child(3) {
margin-left: calc(100% - 200px); /* 200px is width of 2 boxes */
}
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
推荐阅读
- python - 调用用 Python 编写的 Google Cloud Function 时,Vue 客户端被 CORS 阻止
- python - Python枚举函数自动排序
- android - 扩展功能无法从库中导入
- mysql - 不适用于更新语句的情况
- verilog - 范围规范 Verilog
- c# - 从 Controller .Net Core 返回下载文件
- c++ - 这个函数在c++中执行了多少次
- php - 如何将 PrestaShop 升级到未发布的官方版本,如(1.7.8 或 1.7.7)?
- google-drive-api - 如何将谷歌驱动器菜单栏和/或谷歌幻灯片从英语更改为西班牙语
- flask - 无法在 Flask Socket IO 中访问应用程序上下文