coder
coder

Reputation: 357

Element does not expand to fill parent flex div

html,
body {
  margin: 0;
  height: 100%;
}

.navbar {
  position: fixed;
  top: 0;
  width: 100%;
  height: 50px;
  background-color: #2D4256;
}

.nav-centre {
  margin: 0 auto;
  width: 40%;
  height: 100%;
}

.nav-container {
  display: flex;
  height: 100%;
  align-items: center;
  /* vertically centre */
}

.nav-item {
  color: white;
  width: 40px;
  text-align: center;
}

.main-content {
  height: calc(100% - 50px);
  width: 100%;
  position: absolute;
  bottom: 0;
  overflow-y: overlay;
  display: flex;
  justify-content: center;
}

.main-wrap {
  width: 40%;
  border-left: 1px solid black;
  border-right: 1px solid black;
}

.box {
  width: 200px;
  height: 200px;
  background: white;
  border: 1px solid black;
  margin: 10px;
}
<body>
  <div class="navbar">
    <div class="nav-centre">
      <div class="nav-container">
        <div class="nav-item">1</div>
        <div class="nav-item">2</div>
        <div class="nav-item">3</div>
        <div class="nav-item">4</div>
      </div>
    </div>
  </div>
  <div class="main-content">
    <div class="main-wrap">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </div>
</body>

The main-wrap div is not expanding to fill the parent main-content div, how can I get the main-wrap element to expand to the full height of the parent?

https://codepen.io/woooof/pen/VwBLprj

Upvotes: 0

Views: 57

Answers (2)

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

I am not a huge fan of making the size of one element (navbar) determine the position of the second element main-content (margin-top). where you have height: calc(100% - 50px); I would rather if the style of the first changes. Say for example we increase navbar font size, you would not need to adjust the second manually.

Here in this example I set the font-size on an ancestor block to change the nav buttons size and not have to change the content. font-size: 1.5rem; Change it even larger; again no change to the content CSS;

I put a lot of comments in and some borders just to show where things line - that can and should all be removed for a production version.

html,
body {
  margin: 0;
  height: 100%;
  /* stack the nav and the content blocks */
  display: grid;
  grid-template-rows: 1fr auto;
}

.navbar {
  /* put the navbar at the top */
  position: sticky;
  top: 0;
  background-color: #2D4256;
  /* flex, default vertical/horizontal centers nav-centre in the flex */
  display: flex;
}

.nav-centre {
  margin: 0 auto;
  display: flex;
}

.nav-container {
  display: flex;
  /* again these are the default here 
  flex-direction: row;
  align-items: center;
  justify-content: center;
  */
  /* how much space above and below the yellow border nav container */
  margin-top: 0.5rem;
  margin-bottom: 0.5rem;
}

.nav-item {
  color: white;
  /* 2 times font-size for cyan border items */
  width: 2em;
  text-align: center;
}

.main-content {
  display: grid;
  justify-content: center;
}

.main-wrap {
  border-left: 1px solid black;
  border-right: 1px solid black;
}

.box {
  width: 200px;
  height: 200px;
  background: white;
  border: 1px solid black;
  margin: 10px;
}


/* below here is just for visual clarification and can be removed */

.navbar {
  /* just to show you can style and not effect content block *
  /* this can be on any of the three containers */
  font-size: 1.5rem;
  font-family: Verdana, Arial, Helvetica, sans-serif;
}

.nav-centre {
  border: 1px solid magenta;
  padding: 2px;
}

.nav-container {
  border: 1px solid yellow;
}

.nav-item {
  border: 1px solid cyan;
  /* you can space out the nav buttons */
  margin: 0 0.25rem;
}

.main-content {
  /* just to show it is below the navbar and separate */
  border: solid red 1px;
  margin-top: 0.25rem;
  margin-left: 0.5rem;
  margin-right: 0.5rem;
}

.box {
  background-color: #ffffdd;
}
<body>
  <div class="navbar">
    <div class="nav-centre">
      <div class="nav-container">
        <div class="nav-item">1</div>
        <div class="nav-item">2</div>
        <div class="nav-item">3</div>
        <div class="nav-item">4</div>
      </div>
    </div>
  </div>
  <div class="main-content">
    <div class="main-wrap">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </div>
</body>

Upvotes: 1

Alvin
Alvin

Reputation: 875

The .main-wrapper is getting by default display:block, which doesn't match with the display:flex parent.

To get the value from the parent, you can use display: inherit. Once done, the elements inside won't respect their width. To fix that, you must wrap the elements, and for making it total height, You can use max-content.

.main-wrapper {
  display: inherit;
  flex-wrap: wrap;
  height: max-content;
}

Result:

html,
body {
  margin: 0;
  height: 100%;
}

.navbar {
  position: fixed;
  top: 0;
  width: 100%;
  height: 50px;
  background-color: #2D4256;
}

.nav-centre {
  margin: 0 auto;
  width: 40%;
  height: 100%;
}

.nav-container {
  display: flex;
  height: 100%;
  align-items: center;
  /* vertically centre */
}

.nav-item {
  color: white;
  width: 40px;
  text-align: center;
}

.main-content {
  height: calc(100% - 50px);
  width: 100%;
  position: absolute;
  bottom: 0;
  overflow-y: overlay;
  display: flex;
  justify-content: center;
}

.main-wrap {
  width: 40%;
  border-left: 1px solid black;
  border-right: 1px solid black;
  display: inherit;
  flex-wrap: wrap;
  height: max-content;
}

.box {
  width: 200px;
  height: 200px;
  background: white;
  border: 1px solid black;
  margin: 10px;
}
<body>
  <div class="navbar">
    <div class="nav-centre">
      <div class="nav-container">
        <div class="nav-item">1</div>
        <div class="nav-item">2</div>
        <div class="nav-item">3</div>
        <div class="nav-item">4</div>
      </div>
    </div>
  </div>
  <div class="main-content">
    <div class="main-wrap">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </div>
</body>

Upvotes: 1

Related Questions