Search code examples
csscss-grid

Nested CSS-grid implicitly fill remaining height from parent grid


I am trying to have a child CSS-grid respect the dimensions of the parent grid. How would I be able to achieve that?

I was initially writing something like this:

.site,
body,
html {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}

.parent-grid {
  display: grid;
  height: 300px;
  width: 300px;
  grid-template-rows: 30px 1fr;
  grid-template-columns: 1fr;
  grid-template-areas: 'nav' 'child';
  background-color: grey;
}

.nav {
  background-color: #0D47A1;
  grid-area: nav;
}

.child {
  grid-area: child;
}

.child-grid {
  display: grid;
  grid-template-rows: 1fr;
  grid-template-columns: 1fr;
  grid-template-areas: 'test';
  background-color: grey;
}

.content {
  background-color: red;
  overflow: auto;
  grid-area: test;
}

* {
  box-sizing: border-box;
}
<div class="parent-grid">
  <div class="nav">Sidebar</div>
  <div class="child">
    <div class="child-grid">
      <div class="content">
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
        <div>blah</div>
      </div>
    </div>
  </div>
</div>

I would like to have 'content' be a scrollable area that doesn't expand the height of the parent (so it should be '270px').


Solution

  • The grid item (.child) is given min-height: auto by default. Apply min-height: 0 to .child to overrule this.

    Then add height: 100% to .child-grid and overflow: auto to content...

    .site,
    body,
    html {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
    
    .parent-grid {
      display: grid;
      height: 300px;
      width: 300px;
      grid-template-rows: 10vh 1fr;
      grid-template-columns: 1fr;
      grid-template-areas: 'nav' 'child';
      background-color: grey;
    }
    
    .nav {
      background-color: #0D47A1;
      grid-area: nav;
    }
    
    .child {
      grid-area: child;
      min-height: 0;
    }
    
    .child-grid {
      display: grid;
      grid-template-rows: 1fr;
      grid-template-columns: 1fr;
      grid-template-areas: 'test';
      background-color: grey;
      height: 100%;
    }
    
    .content {
      background-color: red;
      overflow: auto;
      grid-area: test;
    }
    
    * {
      box-sizing: border-box;
    }
    <div class="parent-grid">
      <div class="nav">Sidebar</div>
      <div class="child">
        <div class="child-grid">
          <div class="content">
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
            <div>blah</div>
          </div>
        </div>
      </div>
    </div>