CSS Flex - 如果内容更宽,列会切换到行吗?

标签 css layout flexbox

我正在尝试实现以下布局:

<Column 1, 50%> <Column 2, 50%>

根据某些情况,第 2 列的内容可能会更长,在这种情况下,它现在应该被推到第二行并占据 100% 的宽度。这意味着现在两者都是行。

<Row 1 (was Column 1), 100%>
<Row 2 (was Column 2), 100%>

现在我卡在等宽的列中,随着内容的增加,第二列垂直增长。

最佳答案

只是一个带有 flex-wrap 的 flex-box 布局?

.my-layout {
  display: flex;
  flex-wrap: wrap;
}

.my-layout > * {
  flex: 1 0 auto;
  /* optional: determine the minimum width of the cell before it breaks into the next line */
  min-width: 45%;
}
<div class="my-layout">
  <div style="background-color: salmon;">First Line</div>
  <div style="background-color: steelblue;">Second Line</div>
</div>

<br>

<div class="my-layout">
  <div style="background-color: salmon;">First Line First Line First Line First Line First Line First Line First Line First Line </div>
  <div style="background-color: steelblue;">Second Line</div>
</div>

关于CSS Flex - 如果内容更宽,列会切换到行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59982865/

相关文章:

javascript - Silverlight 视频播放器重叠 web 元素

javascript - React-Native Styled-Components 尝试将两个不同的组件放在同一行中

javascript - Jquery Accordion 菜单 - 导航到新页面时保持打开状态

html - 在一行中使用 flexbox,左对齐和右对齐元素

html - 宽度 : 50% and flex: 50% in a CSS flexbox? 之间的差异

html - 为什么 Chrome 和 IE 显示的横幅尺寸与 Firefox 不同?

html - 菜单项中断文本

android - android中的数据绑定(bind)复杂列表

ios - 在 Swift 中以编程方式定位(Twitter Digits 的)按钮

java - 如何使用 JavaFX 将 "inputPane"居中