【问题标题】:Complex layout manipulation with flexbox使用 flexbox 进行复杂的布局操作
【发布时间】:2016-11-06 03:59:26
【问题描述】:

我正在使用 flexbox 并尝试进行一些复杂的布局操作。 我想创建一种简单的方法来进行移动优先布局,将一些 div 分配到桌面版本的侧边栏。

像这样:

Mobile    Desktop
A         D  A  B
B            E
C            C
D            F
E         
F         

经过一些测试,我得到了这个解决方案:http://codepen.io/matheusoj/pen/MbWxoV

main {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
  flex-wrap: wrap;
  height: 3000px;
  align-content: center;
}
div {
  width: 100%;
  height: 400px;
  align-self: flex-start;
}
.a {
  background-color: #fc0;
  height: 500px;
}
.b {
  background-color: #c00;
}
.c {
  background-color: #0c0;
}
.d {
  background-color: #aaf;
  height: 200px;
}
.e {
  background-color: #ccc;
}
.f {
  background-color: pink;
}
.clear {
  width: 0;
  flex-basis: 0%;
}
@media (min-width: 768px) {
  .left {
    order: 150;
    width: 200px;
  }
  .center {
    order: 250;
    width: 600px;
  }
  .right {
    order: 350;
    width: 200px;
  }
  .a {
    order: 201;
  }
  .e {
    order: 202;
  }
  .clear {
    width: 0;
    flex-basis: 100%;
  }
}
<main>
  <div class="a center">A</div>
  <div class="b right">B</div>
  <div class="c center">C</div>
  <div class="d left">D</div>
  <div class="e center">E</div>
  <div class="f center">F</div>

  <!--  ".clear" is used to brake the columns, it should always be at the end  -->
  <div class="clear left"></div>
  <div class="clear center"></div>
</main>

问题是&lt;main&gt; 元素需要一个固定的高度才能工作。我无法让它在流体高度下工作。

如果&lt;main&gt; 元素没有固定高度,是否可以实现相同的结果?

【问题讨论】:

  • 我猜你需要给一个固定的高度,因为你使用 flex-wrap

标签: html css flexbox


【解决方案1】:

您可以在 .left.right 弹性项目中使用 absolute 位置。


删除您在&lt;main&gt; 中设置的固定高度值并在您的媒体查询中使用它:

main {
    position: relative;
    padding-left: 20%; /* padding-left = .left class width */
    padding-right: 20%; /* padding-right = .right class width */
  }
  .left {
    /* order: 150; Removed. It is not needed because the element is not in the normal flow  of the document*/
    width: 20%;
    position: absolute;
    top: 0;
    left: 0;
  }
  .center {
    order: 250;
    /* width: 600px; Removed */
  }
  .right {
     /* order: 350; Removed. It is not needed because the element is not in the normal flow  of the document*/
    width: 20%;
    position: absolute;
    top: 0;
    right: 0;
  }

代码片段:

main {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: center;
}
div {
  width: 100%;
  height: 400px;
  align-self: flex-start;
}
.a {
  background-color: #fc0;
  height: 500px;
}
.b {
  background-color: #c00;
}
.c {
  background-color: #0c0;
}
.d {
  background-color: #aaf;
  height: 200px;
}
.e {
  background-color: #ccc;
}
.f {
  background-color: pink;
}
@media (min-width: 768px) {
  main {
    position: relative;
    padding-left: 20%;
    padding-right: 20%;
  }
  .left {
    width: 20%;
    position: absolute;
    top: 0;
    left: 0;
  }
  .center {
    order: 250;
  }
  .right {
    width: 20%;
    position: absolute;
    top: 0;
    right: 0;
  }
  .a {
    order: 201;
  }
  .e {
    order: 202;
  }
}
<main>
  <div class="a center">A</div>
  <div class="b right">B</div>
  <div class="c center">C</div>
  <div class="d left">D</div>
  <div class="e center">E</div>
  <div class="f center">F</div>
</main>

CodePen


或者你可以这样做:

main {
  display: flex;
  justify-content: center; /* Changed value to center */
  flex-wrap: wrap;
  align-content: center;
}

@media (min-width: 768px) {
  .left {
    order: -1;
    flex-basis: 20%;
  }
  .center {
    flex-basis: 60%;

  }
  .right {
    flex-basis: 20%;
  }
}

main {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  align-content: center;
}
div {
  width: 100%;
  height: 400px;
  align-self: flex-start;
}
.a {
  background-color: #fc0;
  height: 500px;
}
.b {
  background-color: #c00;
}
.c {
  background-color: #0c0;
}
.d {
  background-color: #aaf;
  height: 200px;
}
.e {
  background-color: #ccc;
}
.f {
  background-color: pink;
}
@media (min-width: 768px) {
  .left {
    order: -1;
    flex-basis: 20%;
  }
  .center {
    flex-basis: 60%;
  }
  .right {
    flex-basis: 20%;
  }
}
<main>
  <div class="a center">A</div>
  <div class="b right">B</div>
  <div class="c center">C</div>
  <div class="d left">D</div>
  <div class="e center">E</div>
  <div class="f center">F</div>
</main>

【讨论】:

  • 谢谢。这修复了我的示例,但如果我需要将更多 div 分配到侧边栏,则不起作用。
  • @matheusoj 所以基本上你想模拟 2 个靠边元素和一个居中的容器,类似于 Masonry 所做的,但使用单个 flex 容器?
猜你喜欢
  • 2020-09-28
  • 2021-05-19
  • 1970-01-01
  • 2014-01-11
  • 2018-01-13
  • 2017-05-23
  • 2020-02-27
  • 2016-02-24
  • 1970-01-01
相关资源
最近更新 更多