css - 如何将 5 个盒子与 flex 对齐?
问题描述
我需要帮助根据线框预览对齐 5 个框: https ://projekty.freshynek.cz/images/hp-wireframe.png
1 号箱应占 50% 的宽度,其他仅占集装箱宽度的 25%,位于大箱的右侧。
.flex-container {
width: 1170px;
display: flex;
}
.text {
margin: auto;
}
.flex-item {
flex-direction: row;
}
.col-first {
width: 50%;
flex: 1;
}
.big {
border-radius: 10px;
width: 100%;
height: 300px;
background-color: pink;
}
.col-second {
width: 50%;
flex: 1;
}
.two,
.three,
.four,
.five {
border-radius: 10px;
width: 50%;
height: 150px;
background-color: turquoise;
}
.three {
border-radius: 10px;
width: 50%;
background-color: turquoise;
}
<div class="flex-container">
<div class="col-first">
<div class="big">
<div class="text">1. big</div>
</div>
</div>
<!-- .col-first -->
<div class="col-second">
<div class="two">
<p>2. small</p>
</div>
<div class="three">
<p>3. small</p>
</div>
<div class="four">
<p>4. small</p>
</div>
<div class="five">
<p>5. small</p>
</div>
</div><!-- .col-first -->
</div><!-- .flex-container -->
我尝试使用 flexbox 对齐所有 5 个框,但找不到正确的方法。感谢您的任何帮助。
解决方案
您需要将以下 css 属性添加到.col-second
类中。
display: flex
- 这是必要的,以便4 turquoise
可以使用 flexbox 控制您的盒子。应用display:flex
到后.col-second
,它将作为这些盒子的父容器。flex-wrap: wrap
- 这是必要的,以便当项目(总)超过父项的宽度时,它们会转移到下一行。没有这个,你的4 turquoise
盒子将保持在一行而不是两行。flex-wrap
仅当您拥有display:flex
同一项目时才有效。
附上工作片段。
.flex-container {
width: 1170px;
display: flex;
}
.text {
margin: auto;
}
.flex-item {
flex-direction: row;
}
.col-first {
width: 50%;
flex: 1;
}
.big {
border-radius: 10px;
width: 100%;
height: 300px;
background-color: pink;
}
.col-second {
width: 50%;
flex: 1;
display: flex;
flex-wrap: wrap;
}
.two,
.three,
.four,
.five {
border-radius: 10px;
width: 50%;
height: 150px;
background-color: turquoise;
}
.three {
border-radius: 10px;
width: 50%;
background-color: turquoise;
}
<div class="flex-container">
<div class="col-first">
<div class="big">
<div class="text">1. big</div>
</div>
</div>
<!-- .col-first -->
<div class="col-second">
<div class="two">
<p>2. small</p>
</div>
<div class="three">
<p>3. small</p>
</div>
<div class="four">
<p>4. small</p>
</div>
<div class="five">
<p>5. small</p>
</div>
</div><!-- .col-first -->
</div><!-- .flex-container -->
推荐阅读
- mysql - XAMPP:MySQL 意外关闭
- r - 将多个参数传递给 dplyr::summarise_if 中的函数
- mongodb - 如何在mongodb中使用聚合进行分组
- php - 在 SQL 查询中使用带计数的 DISTINCT 关键字
- python - 如何在 Python 中插入 2D 曲线
- javascript - Angular 6 表单在提交和重置后返回验证错误
- c# - request.getResponse() 出现错误
- ansible - 从剧本访问 local_tmp
- java - 将pdf文件从服务器发送到客户端
- python - Python MySQL,这是一个准备好的语句吗?