简体   繁体   English

在Flexbox中控制包装行为

[英]Controlling wrapping behavior in flexbox

I have three divs inside a flex container. 我在flex容器中有三个div。 They are intended to exist in two rows: 它们打算存在两行:

  • First row: 第一排:
    • one with fixed width 一个固定宽度
    • other should stretch on remaining width 其他应在剩余宽度上伸展
  • Second row: 第二行:
    • one item 一个项目

But div 1 stretches to 100% width (and pushes div 2 to the next line); 但是div 1会拉伸到100%的宽度(并将div 2推到下一行);

 *{ box-sizing: border-box; } .parent{ display: flex; flex-flow: row wrap; } .parent > div{ flex: 2 1 auto; border: 1px solid gray; text-align: center; } .parent > div.child1{ background: lightblue; } .parent > div.child2{ flex: 0 1 200px; background: lightgreen; } .parent > div.child3{ flex-basis: 100%; background: lightcoral; } .parent > div.child1-1{ background: lightblue; flex-basis: 80%; max-width: 80%; } .parent > div.child2-1{ flex: 0 1 20%; background: lightgreen; } 
 <div class="parent"> <div class="child1">first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first </div> <div class="child2">second</div> <div class="child3">third</div> </div> <br><br><br> <div class="parent"> <div class="child1-1">Should be like this, but with fixed width. <br>first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first </div> <div class="child2-1">second</div> <div class="child3">third</div> </div> 

Here is an example: codepen.io 这是一个示例: codepen.io

Applying flex:0 0 yourwidth to the first block seems to be what you are after. flex:0 0 yourwidth应用于第一个块似乎就是您所追求的。

 * { box-sizing: border-box; } .parent { display: flex; flex-flow: row wrap; } .parent>div { border: 1px solid gray; text-align: center; } .parent>div.child1 { background: lightblue; flex: 0 0 70%; /* your required width here*/ } .parent>div.child2 { flex: 1; background: lightgreen; } .parent>div.child3 { flex-basis: 100%; background: lightcoral; } 
 <div class="parent"> <div class="child1">first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first </div> <div class="child2">second</div> <div class="child3">third</div> </div> 

 .parent { display: flex; flex-flow: row wrap; } .parent > div.child1 { flex: 1; /* 1 */ background: lightblue; } .parent > div.child2 { flex: 0 0 200px; /* 2 */ background: lightgreen; } .parent > div.child3 { flex: 0 0 100%; /* 3 */ background: lightcoral; } .parent > div { border: 1px solid gray; text-align: center; } * { box-sizing: border-box; } 
 <div class="parent"> <div class="child1">first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first first </div> <div class="child2">second</div> <div class="child3">third</div> </div> 

Notes: 笔记:

  1. The first item is forced to remain on the first line by setting the flex-basis to 0, not the default auto . 通过将flex-basis设置为0,而不是默认的auto ,可以将第一项保留在第一行。 With this adjustment, flex-grow: 1 uses the free space on the line, without regard for the content size. 进行此调整后, flex-grow: 1使用行上的可用空间,而不考虑内容大小。 Read more. 阅读更多。
  2. The second item is fixed width. 第二项是固定宽度。 It can't grow or shrink. 它不能增长或收缩。 It remains 200px. 它仍然是200px。
  3. The third item is set to always exist on its own line. 第三项设置为始终存在于自己的行中。

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

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