如何控制flexbox中每列的项目数? [英] How can I control the number of items per column in flexbox?
问题描述
在flex容器中,我有5个列方向的项目,但是在一定的宽度下,我想每列显示3个,并强制其他列包装
有没有固定高度的方法?
我的代码:
In a flex container I have 5 items with a column direction but in a certain width I want to display 3 per column and force the others to wrap
is there any way to do that without fixed height ?
my code :
<div class="container">
<div class="item-1 item">Item 1</div>
<div class="item-2 item">Item 2</div>
<div class="item-3 item">Item 3</div>
<div class="item-4 item">Item 4</div>
<div class="item-5 item">Item 5</div>
</div>
.container {
display: flex;
flex-flow: column wrap;
}
@media (min-width: 30em) {
}
js Bin: http://jsbin.com / fesujifelu / edit?html,css,output
推荐答案
在flexbox中,商品的高度/宽度限制为了包装的容器。否则,它们将没有断点,并且将沿着同一行继续。
In flexbox, items need a height / width limit on the container in order to wrap. Otherwise, they have no breakpoint and will continue along the same line.
但是您的布局在CSS网格布局中不是问题:
But your layout is not a problem in CSS Grid Layout:
http://jsbin.com/lapafidejo/1/edit ?html,css,output
/* =================================
Flexbox
==================================== */
.container {
display: grid;
grid-template-columns: 1fr;
}
/* =================================
Media Queries
==================================== */
@media (min-width: 30em) {
.container {
grid-template-rows: 1fr 1fr 1fr;
grid-auto-columns: 1fr;
grid-auto-flow: column;
}
}
/* =================================
Page Styles
==================================== */
* {
box-sizing: border-box;
}
body {
font-size: 1.35em;
font-family: 'Varela Round', sans-serif;
color: #fff;
background: #e8e9e9;
padding-left: 5%;
padding-right: 5%;
}
.container {
padding: 10px;
background: #fff;
border-radius: 5px;
margin: 45px auto;
box-shadow: 0 1.5px 0 0 rgba(0, 0, 0, 0.1);
}
.item {
color: #fff;
padding: 15px;
margin: 5px;
background: #3db5da;
border-radius: 3px;
}
<div class="container">
<div class="item-1 item">Item 1</div>
<div class="item-2 item">Item 2</div>
<div class="item-3 item">Item 3</div>
<div class="item-4 item">Item 4</div>
<div class="item-5 item">Item 5</div>
</div>
对CSS网格的浏览器支持
- Chrome-截至2017年3月8日(版本57)全面支持
- Firefox-截至2017年3月6日(版本52)全面支持
- Safari-截至2017年3月26日(版本10.1)全面支持
- Edge-截至2017年10月16日(版本16)全面支持
- IE11-不支持当前规范;支持过时的版本
- Chrome - full support as of March 8, 2017 (version 57)
- Firefox - full support as of March 6, 2017 (version 52)
- Safari - full support as of March 26, 2017 (version 10.1)
- Edge - full support as of October 16, 2017 (version 16)
- IE11 - no support for current spec; supports obsolete version
以下是完整图片: http://caniuse.com/#search=grid
资源:
- https://css-tricks.com/snippets/css/complete-guide-grid/
- https://gridbyexample.com/examples/
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout
- https://css-tricks.com/snippets/css/complete-guide-grid/
- https://gridbyexample.com/examples/
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout
这篇关于如何控制flexbox中每列的项目数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!