Unknown
Unknown

Reputation: 919

Flex item content overflows container

I have setup the following layout. The content__item elements (which are commented below) are overflowing vertically outside main container.

.root {
  display: flex;
  height: 100vh;
  background-color: gray;
}

.nav {
  width: 16rem;
  background-color: red;
}

.main {
  flex-grow: 1;
  background-color: green;
}

.menu {
  height: 4rem;
  background-color: blue;
}

.content {
  display: flex;
  height: 100%;
  padding: 2rem;
}

.content__item {
  flex: 1;
  margin-left: 1rem;
  background-color: white;
}
<div className="root">
  <nav className="nav"></nav>
  <main className="main">
    <div className="menu"></div>
    <div className="content">
      <!-- Overflowing -->
      <div className="content__item"></div>
      <div className="content__item"></div>
      <div className="content__item"></div>
    </div>
  </main>
</div>

I am pretty sure its a flexbox bug. I tried using min-height: 0 on the container but it still doesn't work. I setup an environment here for reference.

Upvotes: 1

Views: 5912

Answers (2)

FluffyKitten
FluffyKitten

Reputation: 14312

The reason the content_items are overflowing is because height: 100% causes problems with flex. However if you remove that, the elements don't appear to fill the available height. This is because their parent (the content div) is not the child of a flex element, so it is in fact this element and not the content__item that isn't taking up the available height.

We can fix this by adding display:flex to the main div (the parent of content)... however now we have another problem! This makes the other child of content (the nav element) appears to the side. To fix this, we can use flex-direction: column;

The main changes you need to make this work as as follows:

.main {
  flex-grow: 1;            /* you already have this to allow the children grow */
  display: flex;           /* Add this so the content element can use the full height */
  flex-direction: column;  /* Add this to make the children stack one below another */
}
.content {
  display: flex;          /* you already had this */
  flex:1;                 /* Add this to make it take up the available height */
}
.content__item {
  flex: 1;                /* You don't actually need this now */
}

Working Example:

Also FYI, you need to set the body margin to 0 - otherwise the 100vh extends larger than the screen as it is getting added to the default margin.

body {  margin:0; }
.root {
  display: flex;
  height: 100vh;
  background-color: gray;
}

.nav {
  width: 16rem;
  background-color: red;
}

.main {
  flex-grow: 1;
  background-color: green;
  display:flex;
  flex-direction:column;
}

.menu {
  height: 4rem;
  background-color: blue;
}

.content {
  display: flex;
  padding: 2rem;
  flex:1;
}

.content__item {
  margin-left: 1rem;
  background-color: white;
}
<div class="root">
  <nav class="nav"></nav>
  <main class="main">
    <div class="menu"></div>
    <div class="content">
      <!-- Overflowing -->
      <div class="content__item">some text here</div>
      <div class="content__item">some text here</div>
      <div class="content__item">some text here</div>
    </div>
  </main>
</div>

Upvotes: 3

ksav
ksav

Reputation: 20840

body, html {
  margin: 0;
}

.root {
  display: flex;
  height: 100vh;
  background-color: gray;
}

.nav {
  width: 16rem;
  background-color: red;
}

.main {
  flex-grow: 1;
  background-color: green;
  
  display: flex;
  flex-direction: column;
}

.menu {
  height: 4rem;
  background-color: blue;
}

.content {
  display: flex;
  padding: 2rem;
  
    flex: 1;
}

.content__item {
  flex: 1;
  margin-left: 1rem;
  background-color: white;
}
<div class="root">
  <nav class="nav"></nav>
  <main class="main">
    <div class="menu"></div>
    <div class="content">
      <!-- Overflowing -->
      <div class="content__item">a</div>
      <div class="content__item">b</div>
      <div class="content__item">c</div>
    </div>
  </main>
</div>

Upvotes: 1

Related Questions