简体   繁体   English

如何包装 inline-flex div 内容,使包装部分占据前一个 inline-flex div 下面的空白空间?

[英]How to wrap inline-flex div content such that the wrapped portion occupies empty space below the previous inline-flex div?

 .container { height: 200px; width: 200px; border: 5px solid black; padding: 5px; }.button-group { display: inline-flex; flex-wrap: wrap; }.box { height: 45px; width: 45px; border-width: 5px; border-style: solid; }.red { border-color: red; }.green { border-color: green; }.blue { border-color: blue; }.yellow { border-color: yellow; }.pink { border-color: pink; }.brown { border-color: brown; }
 <div class="container"> <div class="button-group"> <div class="box red">R</div> <div class="box green">G</div> </div> <div class="button-group"> <div class="box blue">Bl</div> <div class="box yellow">Y</div> </div> <div class="button-group"> <div class="box pink">P</div> <div class="box brown">Br</div> </div> </div>

Actual:实际的:

实际产量

^ If there is only one box in the first button-group and two box in the second button-group then all three occupy the first line. ^ 如果第一个button-group中只有一个box ,第二个button-group组中只有两个box ,则所有三个都占据第一行。 However, as soon as you add a third box to the second button-group , all of its three box move to the next line.但是,一旦您将第三个box添加到第二个button-group ,它的所有三个box都会移动到下一行。 Instead of that happening, I want the box to occupy any available space on the lines, regardless of which button-group they belong to.而不是发生这种情况,我希望该box占据线上的任何可用空间,而不管它们属于哪个button-group

Expected:预期的:

预期产出

Overall expectation: There could be n number of inline-flex divs inside container and m number of box inside button-group .总体预期: container内可能有n个 inline-flex div, button-group内可能有mbox Any button-group or any box could be removed dynamically.可以动态删除任何button-group或任何box At the end, there must be three box on each line, or as many as possible w.r.t.最后,每行必须有三个box ,或者尽可能多 w.r.t。 to available width.到可用宽度。

Thank you.谢谢你。

If you remove the flex and make the button-groups display inline but their children display inline-block (so they both display inline and take up the given dimensions) you get the required result.如果您删除 flex 并使按钮组显示为内联,但它们的子项显示为内联块(因此它们都显示为内联并占用给定的尺寸),您将获得所需的结果。

UPDATE : to remove the space between the boxes, set and reset the font-sizes as below:更新:删除框之间的空间,设置和重置字体大小如下:

 .container { height: 200px; width: 200px; border: 5px solid black; padding: 5px; font-size: 0; }.button-group { display: inline; }.box { height: 45px; width: 45px; border-width: 5px; border-style: solid; display: inline-block; font-size: initial; }.red { border-color: red; }.green { border-color: green; }.blue { border-color: blue; }.yellow { border-color: yellow; }.pink { border-color: pink; }.brown { border-color: brown; }
 <div class="container"> <div class="button-group"> <div class="box red">R</div> <div class="box green">G</div> </div> <div class="button-group"> <div class="box blue">Bl</div> <div class="box yellow">Y</div> </div> <div class="button-group"> <div class="box pink">P</div> <div class="box brown">Br</div> </div> </div>

Remove the button-group using display:contents and move display:flex to the container使用display:contents删除按钮组并将display:flex移动到容器

 .container { height: 200px; width: 200px; border: 5px solid black; padding: 5px; display: flex; flex-wrap: wrap; align-content: start; }.button-group { display: contents; }.box { height: 45px; width: 45px; border-width: 5px; border-style: solid; }.red { border-color: red; }.green { border-color: green; }.blue { border-color: blue; }.yellow { border-color: yellow; }.pink { border-color: pink; }.brown { border-color: brown; }
 <div class="container"> <div class="button-group"> <div class="box red">R</div> <div class="box green">G</div> </div> <div class="button-group"> <div class="box blue">Bl</div> <div class="box yellow">Y</div> </div> <div class="button-group"> <div class="box pink">P</div> <div class="box brown">Br</div> </div> </div>

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

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