【问题标题】:CSS Flexbox 2 columns with different heightsCSS Flexbox 2列具有不同的高度
【发布时间】:2018-10-02 18:35:59
【问题描述】:

我有以下代码:

.wrapper {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.a {
  background-color: red;
  width: 65%;
}

.b {
  background-color: green;
  width: 35%;
}

.c {
  background-color: blue;
  width: 65%;
  height: 100px;
}

.d {
  background-color: orange;
  width: 35%;
}

.e {
  background-color: teal;
  width: 65%;
}

.f {
  background-color: purple;
  width: 35%;
}
<div class="wrapper container">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
  <div class="f">F</div>
</div>

我试图让 F 在 D 的正下方,像这样:

我这样做而不是 2 个单独的列的主要原因是因为我希望以后能够使用 order 在移动设备中排列这些列。这在 Flexbox 中是否可行,还是有其他方法?

【问题讨论】:

  • 目前的html结构不可能,flexbox方式。

标签: html css flexbox css-grid


【解决方案1】:

flex-flow: wrap; 添加到您的弹性容器将解决您的问题。

flex-flow 是一个简写属性,因此请务必阅读documentation

.d 元素中添加了max-height,这样高度就不会填满空白区域。

希望这会有所帮助:)

.wrapper {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  flex-flow: wrap;
}

.a {
  background-color: red;
  width: 65%;
}

.b {
  background-color: green;
  width: 35%;
}

.c {
  background-color: blue;
  width: 65%;
  height: 100px;
}

.d {
  background-color: orange;
  width: 35%;
  max-height: 18px;
}

.e {
  background-color: teal;
  width: 65%;
}

.f {
  background-color: purple;
  width: 35%;
}
<div class="wrapper container">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
  <div class="f">F</div>
</div>

【讨论】:

  • @GeraldoBLANCO 这似乎扭曲了 D 元素的高度。有没有办法在将 D 保持在其默认高度(如 OP 的图像中)的同时做到这一点?
  • 不,需要像我的第二张照片一样。我开始认为 Flexbox 不适合做这个!
  • 编辑帖子。你所说的“和我一样大”是指发布的图片吗?
  • 你介意html结构改变吗?我的意思是 div 重新排列
【解决方案2】:

为了让 flex 列能够换行,您需要在容器上定义一个固定高度。否则,如果没有断点,列就没有理由换行。它只会将容器扩展为一列。

这里更详细地解释了这个问题:

使用网格解决方案可能会更好。

.wrapper {
  display: grid;
  grid-template-columns: 65% 35%;
  grid-template-areas: " a b "
                       " c d "
                       " c f "
                       " c . "
                       " c . "                       
                       " e . " ;}

.a { grid-area: a; background-color: red;    }
.b { grid-area: b; background-color: green;  }
.c { grid-area: c; background-color: blue; height: 100px; }
.d { grid-area: d; background-color: orange; }
.e { grid-area: e; background-color: teal;   }
.f { grid-area: f; background-color: purple; }
<div class="wrapper">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
  <div class="f">F</div>
</div>

Browser support is now pretty strong for CSS Grid.

此外,order 属性适用于 Grid 和 Flex 布局。但是您可能不需要 Grid 中的order 来重新安排移动屏幕的布局。您可以简单地使用网格属性。

【讨论】:

  • 这确实产生了我的图片的样子,但是每当我向 div A 添加更多内容时,B 就会随之扩展。我查看了您发布的线程,现在我很肯定如果没有 Javascript,就不可能完成我正在寻找的东西。谢谢!
  • 发布一个带有更完整示例的新问题。内容是否动态添加到每个网格项?如果您事先知道高度,则无需编写脚本就可以进行布局。详情见我的回答:stackoverflow.com/q/44377343/3597276
猜你喜欢
  • 2020-06-20
  • 2023-02-22
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
相关资源
最近更新 更多