css - 使用 CSS Flexbox 构建堆叠布局
问题描述
我有一个工作面试的技术测试,我正在为应该是一个简单的布局而苦苦挣扎!
我需要一个两列设计,其中 div 按以下方式堆叠在一起:
然后对于较小的屏幕将更改为:
我以为我很了解 flexbox,但事实证明我显然不知道,我能得到的最接近的(这是不近的......)是
使用
<head>
<style>
.container{
width:600px;
min-height:300px;
background-color: blue;
display:flex;
flex-wrap: wrap;
}
.small{
width: 100px;
height:100px;
background-color: darkgoldenrod;
margin:20px;
}
.big{
width: 250px;
height:250px;
background-color: green;
margin:20px;
}
</style>
</head>
<body>
<div class="container">
<div class="big">Widget 1</div>
<div class="small">Widget 2</div>
<div class="small">Widget 3</div>
<div class="big">Widget 4</div>
<div class="small">Widget 5</div>
<div class="small">Widget </div>
</div>
</body>
我已经搜索了几个小时,但我无法接近任何东西!谁能指出我正确的方向?
解决方案
您可以使用flexbox order 属性对移动视图上的项目进行重新排序。对于桌面视图,请查看CSS 媒体查询,为不同的视口提供不同的样式。
.grid-container {
background: blue;
display: flex;
flex-direction: column;
padding: 10px;
}
.widget {
width: 100px;
height:100px;
margin: 10px;
background-color: darkgoldenrod;
}
.green {
background-color: green;
}
.item1 {
order: 1;
}
.item2 {
order: 2;
}
.item3 {
order: 4;
}
.item4 {
order: 3;
}
.item5 {
order: 5;
}
.item6 {
order: 6;
}
<div class="grid-container">
<div class="widget item1 green">1</div>
<div class="widget item2">2</div>
<div class="widget item3">3</div>
<div class="widget item4 green">4</div>
<div class="widget item5">5</div>
<div class="widget item6">6</div>
</div>
推荐阅读
- php - Laravel:将分组数据转换为一行
- react-native - 如何在 React-native 的 ScrollView 中更改 RefreshControl 向下滑动高度
- email - 如何使用脚本将 google 电子表格复制到 gmail?
- c# - Triangle.NET - 如何将顶点添加到现有的三角剖分?
- pyspark-dataframes - 使用 pandas 将 pyspark 数据帧存储到本地文件系统
- node.js - MongoDB 查询两个集合以添加/减去第一个集合中的字段与第二个集合中的匹配字段
- google-cloud-platform - 我需要将服务器从物理迁移到 GCP 云
- python - Cannon 卸载 pip3,但 pip3 --version 显示它存在
- io - 如何从价值约 800gb 的文本文件中查找和删除重复的字符串?
- mongodb - 当我升级 mongo (3.6 - 4.2) 时,我必须清除 data/db/ 文件夹。所以我丢失了所有数据库。有什么办法可以防止数据丢失?