[英]CSS - How to force second div to another row when first div overflows
I have two divs placed next to each other of equal width. 我有两个div并排放置,它们的宽度相等。 When the text in the first div overflows to a second line, I would like the second div to be placed below the first div- doubling the width of each div. 当第一个div中的文本溢出到第二行时,我希望将第二个div放置在第一个div下方,从而将每个div的宽度加倍。 Is this possible in CSS? CSS有可能吗?
I have tried using flexboxes but I don't know where to go from here. 我已经尝试过使用flexboxes,但是我不知道从这里去哪里。
#container { display: flex; width: 500px; border: 1px solid black; } .cell { flex: 1; }
<div id="container"> <div class="cell" style="background-color:pink;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit </div> <div class="cell" style="background-color:lightblue;"> Ut enim ad minim veniam, quis nostrud exercitation ullamco </div> </div>
You can turn flex-wrap: wrap;
您可以打开flex-wrap: wrap;
on the container and flex-grow: 1;
在容器上flex-grow: 1;
on the items. 在项目上。 (view snippet in full screen and adjust browser width) (全屏查看代码段并调整浏览器宽度)
#container { display: flex; border: 1px solid black; flex-wrap: wrap; } .cell { flex-grow:1; }
<div id="container"> <div class="cell" style="background-color:pink;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit </div> <div class="cell" style="background-color:lightblue;"> Ut enim ad minim veniam, quis nostrud exercitation ullamco </div> </div>
I figured it out. 我想到了。 Both divs have to be min-width: 50%
and the last div has to be flex: 1
. 两个div都必须为min-width: 50%
,最后一个div必须为flex: 1
。 The parent div should have flex-wrap: wrap
. 父div应该具有flex-wrap: wrap
。
#container { display: flex; flex-wrap: wrap; width: 500px; border: 1px solid black; } #cell1 { background-color: pink; min-width: 50%; } #cell2 { background-color: lightblue; min-width: 50%; flex: 1; }
<div id="container"> <div id="cell1"> Lorem ipsum dods Ut eniasdfasdf </div> <div id="cell2"> Ut enim ad minim veniam, quis nostrud exercitation ullamco </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.