Home > Blockchain >  CSS Grid 2 cols and half height
CSS Grid 2 cols and half height

Time:08-26

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 >
  <div >Header</div>
  <div >Menu</div>
  <div >Main</div>  
  <div >Right</div>
  <div >Footer</div>
</div>

</body>
</html>

CodePudding user response:

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 >
    <div >Header</div>
    <div >Menu</div>
    <div >Main</div>
    <div >Right</div>
    <div >Footer</div>
  </div>

</body>

</html>

  • Related