我在flex容器中有三个div。它们旨在分为两行:
但是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>
&#13;
以下是一个示例:codepen.io
答案 0 :(得分:1)
将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>
答案 1 :(得分:1)
.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;
}
&#13;
<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>
&#13;
注意:
flex-basis
设置为0而不是默认auto
,强制第一项保留在第一行。通过此调整,flex-grow: 1
使用行上的可用空间,而不考虑内容大小。 Read more.