[英]Controlling wrapping behavior in flexbox
I have three divs inside a flex container. 我在flex容器中有三个div。 They are intended to exist in two rows: 它们打算存在两行:
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: 笔记:
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. 阅读更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.