css - 响应式 flexbox 布局 : reordering elements

标签 css flexbox

我正在尝试实现如图所示的响应式布局,左侧是移动版,右侧是桌面版。如果我可以为包装器设置固定高度,使用 flexbox 会相对容易,但因为内容是动态的,所以这是不可能的。

Layout

另一种解决方案是在元素 C 上使用绝对位置,但这看起来很 hacky,我希望找到更优雅的解决方案。

代码框架如下:

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

@media(min-width: 800px) {
  .wrapper {
    flex-direction: row;
    flex-wrap: wrap;
  }
}

.section {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 36px;
}

.section-a {
  background: green;
  height: 200px;
}

@media(min-width: 800px) {
  .section-a {
    flex-basis: 33%;
  }
}

.section-b {
  background: yellow;
  height: 400px;
}

@media(min-width: 800px) {
  .section-b {
    flex-basis: 66%;
  }
}

.section-c {
  background: blue;
  height: 200px;
}

@media(min-width: 800px) {
  .section-c {
    flex-basis: 33%;
  }
}
<div class="wrapper">
  <div class="section section-a">A</div>
  <div class="section section-b">B</div>
  <div class="section section-c">C</div>
</div>

感谢您的帮助!

最佳答案

您可以使用 grid 来实现这一点。我已经简化了您的代码并删除了不需要的 css

.wrapper {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: [row1-start] auto [row2-start] auto [row2-end];
}

.section {
  padding: 20px;
  display: flex;
  align-items: center;
}
.section-a {
  height: 50px;
  background-color: green;
}  
.section-b {
  grid-row: row1-start / row2-end;
  grid-column: 2/-1;
  background-color: yellow;
}
.section-c {
  background-color: blue;
  height: 180px;
}
<div class="wrapper">
  <div class="section section-a">A</div>
  <div class="section section-b">B</div>
  <div class="section section-c">C</div>
</div>

工作 fiddle here

关于css - 响应式 flexbox 布局 : reordering elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50484294/

相关文章:

css - Flexbox 在 Safari 中不堆叠

html - 如何用 flexbox 填充垂直空间?

javascript - 有没有办法将一个属性的值附加到另一个属性?

javascript - css div 破坏容器并掉落到下面的另一个

html - 如何更改水平 li 列表的顺序?

html - 如何在 flexbox 布局中将 div 包装在父 div 中?

html - Firefox 中的 Flexbox inline-flex 错误

CSS flexbox 。在 Safari 6 上实现这个网格

css - 如何制作一个列表,每行分为两列,第一列增长到其内容的大小?

css - Text-overflow 在 IE8 和 firefox 中的兼容性