2

UPDATE: So I was able to make this work with Flexbox Codepen However, as some in the community point out, the caveats (such as fixed heights) prove that CSS grid is the best case for layout.

I'm trying to rely on Flexbox row wrapping for as much layout as possible. I keep running into the scenario where I want to stack two or more elements on top of each other (similar to floating) but do not want to add markup which would defeat the purpose here.

I cannot find any examples of this anywhere, perhaps its impossible?

I've created a CodePen to show how I'm trying to stack two elements and then in another row stack 3 items.

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.
-->
Ben Racicot
  • 5,332
  • 12
  • 66
  • 130
  • 1
    Hmm cannot see this happening with flex. But with grid display on the other hand, it's very doable... – u-ways Aug 20 '18 at 20:31
  • 2
    That's not possible using Flexbox with the given markup. For it to work you need to wrap the ones that should share the vertical space (top/middle/bottom) – Asons Aug 20 '18 at 20:52
  • Codepen sample: https://codepen.io/anon/pen/vzYWyX – Asons Aug 20 '18 at 21:01
  • Thanks for the comments. Yes wrapping them or using floats or grid layout is very easy. If we could manage this with flexbox we could push FB into 2 dimensional layout. – Ben Racicot Aug 21 '18 at 02:09

1 Answers1

0

As others have said, the CSS Grid Layout would probably be easier and cleaner for your intents. Flexbox is designed for its elements to flow in one direction, and does not act like a table or grid.

But relying on Flexbox, I've recreated the best that I can think of using. Except that it does involve extra markup (can't really achieve all your goals - limitations of Flexbox).

It sets an extra class .flexcol on a div element, which will act as a wrapper for elements you want stacked.

.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>
Matthew Moore
  • 866
  • 7
  • 10