Flexbox:当连续包装时,如何堆叠两个以上的元素而没有额外的标记?

时间:2018-08-20 20:09:40

标签: html css flexbox

更新: 因此,我能够使用Flexbox Codepen来完成这项工作。但是,正如社区中的一些人指出的那样,警告(例如固定高度)证明CSS网格是布局的最佳案例。

我试图依靠Flexbox行换行来获得尽可能多的布局。我一直遇到这样的情况:我想将两个或多个元素彼此堆叠(类似于浮动),但又不想添加会在此达到目的的标记。

我在任何地方都找不到任何示例,也许这不可能?

我创建了一个CodePen,以说明如何尝试堆叠两个元素,然后在另一行中堆叠3个项目。

body {
  display: flex;
  flex-wrap: wrap;
}

.full-width {
  flex: 100%;
}

.fifty {
  flex: 0 0 50%;
}

.one-third {
  flex: 0 0 33%;
}

.twnty-five {
  flex: 0 0 25%;
}

div.one-third:nth-child(6),
div.one-third:nth-child(7) {
  // margin: auto;
  flex: 0 0 33%;
  max-height: 50px;
  height: 45px;
  min-height: 45px;
}

div.twnty-five:nth-child(11),
div.twnty-five:nth-child(12),
div.twnty-five:nth-child(13) {
  // margin-left:auto;
  flex: 0 0 25%;
  height: 25px;
  min-height: 25px;
}

div {
  display: flex;
  min-height: 100px;
  align-items: center;
  justify-content: center;
  text-align: center;
  border: 1px solid #DFDFDF;
  box-sizing: border-box;
}

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
<div class="full-width">full-width</div>
<div class="fifty">fifty</div>
<div class="fifty">fifty</div>

<div class="one-third">one-third</div>
<div class="one-third">one-third</div>
<div class="one-third">one-third stack top</div>
<div class="one-third">one-third stack bottom</div>

<div class="twnty-five">twnty-five</div>
<div class="twnty-five">twnty-five</div>
<div class="twnty-five">twnty-five</div>
<div class="twnty-five">stack top</div>
<div class="twnty-five">stack middle</div>
<div class="twnty-five">stack bottom</div>

<!--
Copyright (c) 2018 by Ben Racicot (https://codepen.io/BRacicot/pen/bxGrYP)
Fork of an original work by Ben Racicot (https://codepen.io/BRacicot/pen/ZjrZQw)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

1 个答案:

答案 0 :(得分:0)

正如其他人所说,CSS Grid Layout可能会更容易和更清洁您的意图。 Flexbox旨在使其元素沿一个方向流动,并且不像桌子或表格那样起作用。

但是依靠Flexbox,我重新创造了我可以想到的最佳方式。除了确实涉及额外的标记(无法真正实现您的所有目标-Flexbox的局限性)。

它在div元素上设置了一个额外的类.flexcol,它将用作您要堆叠的元素的包装器。

.flexcol {
  flex-direction: column;
  align-items: stretch;
}

body {
  display: flex;
  flex-wrap: wrap;
}

.full-width {
  flex: 100%;
}

.fifty {
  flex: 0 0 50%;
}

.one-third {
  flex: 0 0 33%;
}

.twnty-five {
  flex: 0 0 25%;
}

div {
  display: flex;
  min-height: 100px;
  align-items: center;
  justify-content: center;
  text-align: center;
  border: 1px solid #DFDFDF;
  box-sizing: border-box;
}

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
<div class="full-width">full-width</div>

<div class="fifty">fifty</div>
<div class="fifty">fifty</div>

<div class="one-third">one-third</div>
<div class="one-third">one-third</div>
<div class="one-third flexcol">
  <div>one-third stack top</div>
  <div>one-third stack bottom</div>
</div>

<div class="twnty-five">twnty-five</div>
<div class="twnty-five">twnty-five</div>
<div class="twnty-five">twnty-five</div>
<div class="twnty-five flexcol">
  <div>stack top</div>
  <div>stack middle</div>
  <div>stack bottom</div>
</div>