【问题标题】:Re-arranging flexbox layout for mobile view为移动视图重新安排 flexbox 布局
【发布时间】:2019-07-01 23:13:21
【问题描述】:

我有 4 个 div 正在使用 flexbox 对齐。在桌面尺寸下,有 3 个等宽的列,第一个和最后一个 div 占据了容器的整个高度。第二个和第三个 div 垂直堆叠在中间,每个占高度的 50%。效果很好。

在移动尺寸下,我希望最后一个 div 位于顶部并拉伸容器的整个宽度。然后我希望第一个 div 在顶部 div 下方左对齐,并占据容器宽度和剩余高度的 50%。

我遇到的问题是我希望第二个和第三个 div 在顶部 div 的正下方对齐并占据剩余 50% 的宽度,但垂直堆叠,因此每个占据剩余高度的 50%。

我已尝试更改媒体查询中的flex-direction 以及我能想到的所有其他内容,但它不起作用。

* {
  margin: 0;
  padding: 0;
}

.box-wrapper {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 70vh;
  align-items
}

.boxa {
  background: red;
  flex: 0 0 100%;
  width: 33%;
}

.boxb {
  background: orange;
}

.boxc {
  background: lightgreen;
}

.boxb,
.boxc {
  flex: 0 0 50%;
  width: 33%;
}

.boxd {
  background: grey;
  flex: 0 0 100%;
  width: 33%;
}

@media (max-width: 700px) {
  .boxa {
    order: 2;
    width: 50%;
    flex: 0 0 75%;
  }
  .boxb {
    order: 3;
    width: 50%;
    flex: 0 0 37.5%;
  }
  .boxc {
    order: 4;
    width: 50%;
    flex: 0 0 37.5%;
  }
  .boxd {
    order: 1;
    flex: 0 0 25%;
    width: 100%;
  }
}
<div class="box-wrapper">
  <div class="boxa"></div>
  <div class="boxb"></div>
  <div class="boxc"></div>
  <div class="boxd"></div>
</div>

【问题讨论】:

    标签: html css flexbox css-grid


    【解决方案1】:

    使用 flexbox 很难实现您想要的布局,因为 flexbox 不太适合二维网格。它擅长一维网格(将弹性项目放置在行列中),但在二维网格中容量有限(将弹性项目放置在行列中)。

    由于您想要的布局涉及必须跨越行和列线的项目,因此 flex 不是您的最佳选择。使用 CSS Grid,您的布局简单易行。

    jsFiddle demo

    .box-wrapper {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;  /* 3 equal width columns */
      grid-template-rows: 1fr 1fr;         /* 2 equal height rows */
      height: 70vh;
      grid-column-gap: 5px;
      grid-row-gap: 5px;
      padding: 5px;
      grid-template-areas: " first second last " 
                           " first  third last ";
    }
    
    .boxa { grid-area: first; background: red; }
    .boxb { grid-area: second; background: orange; }
    .boxc { grid-area: third; background: lightgreen; }
    .boxd { grid-area: last; background: grey; }
    
    @media ( max-width: 700px) {
      .box-wrapper {
        grid-template-columns: 1fr 1fr;
        grid-template-rows: 1fr 1fr 1fr;
        grid-template-areas: "  last   last  "
                             " first  second " 
                             " first   third ";
      }
    }
    
    * { margin: 0; padding: 0; }
    
    /* for placing and styling numbers only */
    .box-wrapper > div {
      font-size: 1.5em; display: flex; align-items: center; justify-content: center; }
    <div class="box-wrapper">
      <div class="boxa">1</div>
      <div class="boxb">2</div>
      <div class="boxc">3</div>
      <div class="boxd">4</div>
    </div>

    更多信息:

    【讨论】:

    • 迈克尔,感谢您的回复。我确实通过嵌套弹性盒想出了一个解决方法,但你的答案是一个更好的解决方案。
    • 不客气。 Browser support for CSS Grid 在 2019 年相当强劲。
    • 嵌套容器是绕过 flexbox 网格限制的常用方法。但它会使您的 HTML 变得臃肿,并消除您在弹性项目上使用 order 属性的能力,因为它们不再是兄弟姐妹。
    猜你喜欢
    • 2015-04-11
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多