简体   繁体   English

CSS-如何在第一个div溢出时将第二个div强制到另一行

[英]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.

相关问题 如果文本溢出 HTML/CSS,强制 div 滚动? - Force div to scroll if the text overflows HTML/CSS? 当浏览器响应时,强制第二个 Div 折叠在第一个 Div 之下 - Force Second Div To Collapse Beneath First Div When Browser Responds 如何根据CSS中第一行div中孩子的高度动态下推第二行div? - How to dynamically push down a second-row div based on the height of the child in the first row div in CSS? 如何在第一行制作 3 个 div,在第二行制作 2 个 div? - How can i make 3 div in first row and 2 div in second row? 如何将div在第二行中直接放在具有相同列大小但被第一行中另一列的高度阻塞的第一行div之下 - How to place div in second row directly under div in first row with same column size but blocked by the height of another column in first row CSS动画溢出了div - CSS animation overflows the div HTML / CSS Div溢出 - HTML/CSS Div Overflows 当另一个 Div 悬停在第一个 Div 之后时,如何更改 Div 的 CSS 属性 - How to chage CSS attributes of a Div when another Div is hovered that is located after the first Div CSS - 悬停在第二个 div 上时如何隐藏第一个 div(反之亦然)? - CSS - How do you hide first div when hovering over second div (and vice versa)? 如果同级 div 溢出,CSS 选择器? - CSS selector if sibling div overflows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM