简体   繁体   English

1 个项目的 Flex 框是其他项目的两倍,但仍像网格系统一样保持所有内容内联

[英]Flex box with 1 item twice the size of other items, but still keep everything inline like a grid system

Ive got a flexbox layout that has all the items the same size, apart from one which is twice the width of the other items, but the same height.我有一个flexbox布局,所有项目的大小都相同,除了一个是其他项目宽度的两倍,但高度相同。

The issue im having at the moment is that the top row of the flexbox items are not lining up with the rest of the rows in the flexbox which all contain items of the same width.我目前遇到的问题是 flexbox 项目的顶行没有与 flexbox 中的其余行对齐,这些行都包含相同宽度的项目。

Ive made a jsfiddle of the issue here : http://jsfiddle.net/4e8c0w9z/我在这里做了一个 jsfiddle 的问题: http : //jsfiddle.net/4e8c0w9z/

Any idea if this can be achevied ?知道这是否可以实现吗? I was looking at grid system, but they all seem to work around a fixed number of columns per grid, with break points.我正在研究网格系统,但它们似乎都围绕每个网格固定数量的列工作,并带有断点。 Rather than a flexbox which allows for any number of items per row.而不是允许每行任意数量的项目的 flexbox。

I feel you have a little mathematically problem here.我觉得你在这里有一点数学问题。 Your .box2 is not exactly twice as big as .box .您的.box2并不完全是.box两倍。 Even though you use box-sizing: border-box;即使您使用box-sizing: border-box; you have to keep in mind the margin of the items if you want the gaps to line up properly.如果您希望间隙正确排列,则必须记住项目的margin Here is a demo of what is happening, when you set the width of .box2 to 180px (150 + 15 + 15)这是当您将.box2的宽度设置为 180px (150 + 15 + 15) 时发生的情况的演示

 body { box-sizing: border-box; height:100%; background: #336633; } .wrapper { display:flex; flex-direction:row; flex-wrap: wrap; } .box { width: 75px; height: 100px; background: white; margin: 15px; } .box2 { width: 180px; height: 100px; background: white; margin: 15px; }
 <div class="wrapper"> <div class="box2"></div> <!--THIS IS THE 2 width box--> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <!--wrapper-->

Here is a little addition from @JoshBurgess to make it a little bit more flex ible.这是@JoshBurgess 的一些补充,使其更加flex With this you don't set a specific width but make use of the flex-basis property .有了这个,您不必设置特定的宽度,而是使用flex-basis property

 body { box-sizing: border-box; height:100%; background: #336633; } .wrapper { display:flex; flex-direction:row; flex-wrap: wrap; } .box { flex: 0 1 75px; height: 100px; background: white; margin: 15px; } .box2 { flex: 0 1 180px; height: 100px; background: white; margin: 15px; }
 <div class="wrapper"> <div class="box2"></div> <!--THIS IS THE 2 width box--> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <!--wrapper-->

I see the first item of the flex box's width is double.我看到弹性框宽度的第一项是双倍。 But what if the width and height both are double than others.但是如果宽度和高度都是其他的两倍呢? how to solve that ?如何解决?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM