5

Parent has only display: flex and flex-wrap: wrap, while children have a fixed height and width of 150px. Which behaves like this:

enter image description here

The height of the parent container expands vertically to facilitate the wrapping children elements.

However, changing the parent to have flex-direction: column doesn't have the same behaviour horizontally:

enter image description here

Instead of the parent expanding to fit the wrapping content, it's the children elements that get rearranged to be facilitated within the parent element.

The behaviour I'd like to achieve with the vertically wrapping content looks like this:

enter image description here

Well almost like this. Imagine the parent fills the space vertically, and expands horizontally for the wrapping content. A horizontal version of the first gif.

Is this possible with flex-box?

barjo
  • 143
  • 1
  • 9
  • Note that in most cases elements are set, by default, to `height: auto`. This means the height of the element is the height of the content. But this behavior does not apply to the width of block-level elements, which is set, by default, to the full width of the parent (think: `width: 100%`). That's one why you're seeing different behaviors in `row` and `column` directions ([read more](https://stackoverflow.com/a/46546152/3597276)). For other reasons, see the duplicate posts. – Michael Benjamin Jan 27 '19 at 14:26

1 Answers1

2

You're looking for align-content: start (it deprecated flex-start so grid and flexbox get unified syntax):

div {
  height: 100vh;
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-content: start;
}
span {
  width: 150px;
  height: 150px;
  border: 2px solid #f66;
  background-color: #eee;
  color: #f66;
  font: italic 2.1em sans-serif;
  padding: .21em;
}
body {
  background-color: #f95b68;
}
body { margin: 0;}
* {box-sizing: border-box}
<div>
  <span>1</span>
  <span>2</span>
  <span>3</span>
  <span>4</span>
  <span>5</span>
  <span>6</span>
  <span>7</span>
  <span>8</span>
  <span>9</span>
  <span>10</span>
  <span>11</span>
  <span>12</span>
  <span>13</span>
  <span>14</span>
</div>
tao
  • 82,996
  • 16
  • 114
  • 150
  • Thanks, this was useful in fixing the gaps between elements, although the main issue I'm having is that the parent container doesn't adjust to facilitate the wrapping columns, like it does when wrapping rows. Edit: after further research I've found this is a deficiency in the flex layout, and not possible with plain css – barjo Jan 27 '19 at 19:52
  • 1
    https://stackoverflow.com/questions/33891709/when-flexbox-items-wrap-in-column-mode-container-does-not-grow-its-width The issue I'm having was addressed in this post. Simply the parent container doesn't expand in width when columns get wrapped (whereas when rows are wrapped, the parent container expands downwards with no problem). Quote: "This looks like a fundamental deficiency in flex layout. A flex container in column-direction will not expand to accommodate additional columns. (This is not a problem in flex-direction: row.)" – barjo Jan 27 '19 at 20:03
  • That's clearly not what I understood from your question. Glad you found your answer. I'll delete my answer tomorrow. Already reached the deletion quota for the day. Cheers! – tao Jan 27 '19 at 20:06
  • That's alright. Your explanation makes sense as to why this would be an issue. Thanks for the contribution. – barjo Jan 27 '19 at 20:09
  • Looking into it, I'd expect changing `
    `'s display value from `flex` to `inline-flex` to have the effect you're looking for. However, it remains at the width of 1 column. Looks more like a bug to me.
    – tao Jan 27 '19 at 20:16