1

I'm trying to create a visual layout using Flexbox.

In this layout, controls are arranged in rows. Some of the rows will have a predefined height, and some of the rows will be anchored to both the top and bottom of the screen, which means they'll stretch their height to occupy all available space.

I've got a working layout that will achieve this, but there seems to be an issue when the rows with predefined height need stretch because their contents no longer fit. They seem to not actually be stretching, which causes them to overlap with one another.

Anyone know how to get the rows to resize in height, properly?

You can have a look at the current status here:

https://jsfiddle.net/cgledezma1101/o9zwa7na/

I'm using the following CSS for my containers:

.main-container {
  display: flex;
  flex-flow: column nowrap;
  justify-content: flex-start;
  align-items: flex-start;
  align-content: flex-start;
  height: 100vh;
}

.row {
  flex: 1 1 auto;
  display: flex;
  flex-flow: row wrap;
  justify-content: flex-start;
  align-items: flex-start;
  align-content: flex-start;
  width: 100%;
}

.row2 {
  height: 200px;
  min-height: 200px;
}

.row1 {
  height: 100px;
  min-height: 100px;
}

.not-anchored {
  height: 100px;
}

.anchored {
  height: 100%;
}
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
cgledezma
  • 612
  • 6
  • 11

2 Answers2

1

A fixed height on the container means it cannot expand to accommodate a taller layout. Hence, flex items are forced to overlap on smaller screens.

Instead of height: 100vh, use min-height: 100vh.

Also, remove fixed heights on flex items. They override align-items: stretch.

revised fiddle

.main-container {
  display: flex;
  flex-flow: column nowrap;
  min-height: 100vh;
}

.row {
  display: flex;
  flex-flow: row wrap;
}

.row.anchored { flex: 1; }

body { margin: 0; }


.item12 {
  flex: 1 1 100px;
  height: 200px;
  min-width: 100px;
  background: tomato;
}

.item21 {
  flex: 1 1 200px;
  height: 100px;
  min-width: 100px;
  background: deepskyblue;
}

.item {
  text-align: center;
  color: white;
  font-weight: bold;
  font-size: 3em;
  border: 1px dashed black;
  line-height: 100px;
  margin-left: auto;
  margin-right: auto;
}

.item.anchored {
  flex: 1;
  /* height: 100%; */
  min-height: 150px;
  background: hotpink;
}
<body>
  <div class="main-container">
    <div class="row row2 not-anchored">
      <div class="item item12">
        1.1
      </div>
      <div class="item item12">
        1.2
      </div>
      <div class="item item12">
        1.3
      </div>
    </div>
    <div class="row anchored">
      <div class="item anchored">
        A.1
      </div>
    </div>
    <div class="row row1 not-anchored">
      <div class="item item21">
        2.1
      </div>
      <div class="item item21">
        2.2
      </div>
    </div>
    <div class="row anchored">
      <div class="item anchored">
        A.2
      </div>
    </div>
  </div>
</body>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

It seems the issue arises from giving the .main-container element a height: 100vh;. Changing that to height: 100%; seems to fix it.

.main-container {
  display: flex;
  flex-flow: column nowrap;
  justify-content: flex-start;
  align-items: flex-start;
  align-content: flex-start;
  height: 100%;
}

.main-container {
  display: flex;
  flex-flow: column nowrap;
  justify-content: flex-start;
  align-items: flex-start;
  align-content: flex-start;
  height: 100%;
}

.row {
  flex: 1 1 auto;
  display: flex;
  flex-flow: row wrap;
  justify-content: flex-start;
  align-items: flex-start;
  align-content: flex-start;
  width: 100%;
}

.row2 {
  height: 200px;
  min-height: 200px;
}

.row1 {
  height: 100px;
  min-height: 100px;
}

.not-anchored {
  height: 100px;
}

.anchored {
  height: 100%;
}

.item12 {
  flex: 1 1 100px;
  height: 200px;
  min-width: 100px;
  background: tomato;
}

.item21 {
  flex: 1 1 200px;
  height: 100px;
  min-width: 100px;
  background: deepskyblue;
}

.item {
  text-align: center;
  color: white;
  font-weight: bold;
  font-size: 3em;
  border: 1px dashed black;
  line-height: 100px;
  margin-left: auto;
  margin-right: auto;
}

.item.anchored {
  flex: 1 1 100%;
  height: 100%;
  min-height: 150px;
  background: hotpink;
}
<body>
  <div class="main-container">
    <div class="row row2 not-anchored">
      <div class="item item12">
        1.1
      </div>
      <div class="item item12">
        1.2
      </div>
      <div class="item item12">
        1.3
      </div>
    </div>
    <div class="row anchored">
      <div class="item anchored">
        A.1
      </div>
    </div>
    <div class="row row1 not-anchored">
      <div class="item item21">
        2.1
      </div>
      <div class="item item21">
        2.2
      </div>
    </div>
    <div class="row anchored">
      <div class="item anchored">
        A.2
      </div>
    </div>
  </div>
</body>

The following produces:

enter image description here

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184