Search code examples
csssasscss-grid

CSS Grid 2 cols and half height


Im trying to get my CSS grid into 2 columns with the same parent height but each smaller box half the height of the main box. See attached image. However I can't quite get the right layout and need some help. Please see my code so far.

enter image description here

.item1 { grid-area: header; }
.item2 { grid-area: menu; height:200px;}
.item3 { grid-area: main; height:100px;}
.item4 { grid-area: right; height:100px;}
.item5 { grid-area: footer; height:200px;}

.grid-container {
  display: grid;
  grid-template-areas:
    'header header'
    'menu right'
    'main footer';
  gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}

.grid-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}
<!DOCTYPE html>
<html>
<head>

</head>
<body>

<h1>Grid Layout</h1>

<div class="grid-container">
  <div class="item1">Header</div>
  <div class="item2">Menu</div>
  <div class="item3">Main</div>  
  <div class="item4">Right</div>
  <div class="item5">Footer</div>
</div>

</body>
</html>


Solution

  • There is a missing row in the grid template areas definition.

    .item1 {
      grid-area: header;
    }
    
    .item2 {
      grid-area: menu;
      height: 200px;
    }
    
    .item3 {
      grid-area: main;
      height: 100px;
    }
    
    .item4 {
      grid-area: right;
      height: 100px;
    }
    
    .item5 {
      grid-area: footer;
      height: 200px;
    }
    
    .grid-container {
      display: grid;
      grid-template-areas: 'header header' 'menu right' 'menu footer' 'main footer';
      gap: 10px;
      background-color: #2196F3;
      padding: 10px;
    }
    
    .grid-container>div {
      background-color: rgba(255, 255, 255, 0.8);
      text-align: center;
      padding: 20px 0;
      font-size: 30px;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
    
    </head>
    
    <body>
    
      <h1>Grid Layout</h1>
    
      <div class="grid-container">
        <div class="item1">Header</div>
        <div class="item2">Menu</div>
        <div class="item3">Main</div>
        <div class="item4">Right</div>
        <div class="item5">Footer</div>
      </div>
    
    </body>
    
    </html>