How to build this using CSS Grid?

In summary, the author suggests that instead of using grid-template-areas, you could use grid-template-columns and grid-template-rows. Additionally, they suggest that you use nested grids to achieve the desired layout.
  • #1
shivajikobardan
674
54
TL;DR Summary
CSS Grids example
1668247200793.png

I built these using CSS Grids very easily.
1668247258675.png


1668247276721.png

Source: https://www.quackit.com/css/grid/tutorial/create_a_website_layout.cfm

But the first one is different and requires different logic.
I can't use grid-template-areas there for whole grid.

This is my code for the last layout(I've to edit height and width there).

HTML:
HTML:
<div class="container">
      <div class="header">header</div>
      <div class="leftsidebar">leftsidebar</div>
      <div class="main-top">main top</div>
      <div class="rightsidebar">right side bar</div>
      <div class="bottom-left">bottom left</div>
      <div class="bottom-right">bottom right</div>
      <div class="footer">footer</div>
    </div>

CSS:
Code:
.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-areas:
    "header header header header"
    "leftsidebar main-top main-top rightsidebar"
    "leftsidebar bottom-left bottom-right rightsidebar"
    "footer footer footer footer";
}

.header {
  background-color: skyblue;
  grid-area: header;
}
.leftsidebar {
  background-color: green;
  grid-area: leftsidebar;
}
.main-top {
  background-color: red;
  grid-area: main-top;
}
.rightsidebar {
  background-color: pink;
  grid-area: rightsidebar;
}
.bottom-left {
  background-color: purple;
  grid-area: bottom-left;
}
.bottom-right {
  background-color: yellow;
  grid-area: bottom-right;
}
.footer {
  background-color: blue;
  grid-area: footer;
}
 

Attachments

  • Xc2Bpx4J4-jJMMSw6agRP4iBdbufUVDGbEAhM4KipAVVeVHsfw.png
    Xc2Bpx4J4-jJMMSw6agRP4iBdbufUVDGbEAhM4KipAVVeVHsfw.png
    1.6 KB · Views: 92
Technology news on Phys.org
  • #2
shivajikobardan said:
But the first one is different and requires different logic.
One trick for the layout you a trying to achieve is to count consider it to consist of 5 columns instead of 4 and let the 2nd row orange and 3rd row blue and green all span two columns.

Alternatively you can let the two rows span a single column and put two more (independent) grids or similar in each row.
 
  • #3
 
Last edited:
  • #4
I would do it with the help of flex display:

HTML:
<div class="container">
      <div class="header">header</div>
      <div class="leftsidebar">leftsidebar</div>
      <div class="main-top">
        <div class="top-left">top left</div>
        <div class="top-center">top center</div>
        <div class="top-right">top right</div>
      </div>
      <div class="bottom-left">bottom left</div>
      <div class="bottom-right">bottom right</div>
      <div class="footer">footer</div>
    </div>

CSS:
.container {
  height: 90vh;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-areas:
    "header header header"
    "leftsidebar main-top main-top"
    "leftsidebar bottom-left bottom-right"
    "leftsidebar footer footer";
}

.header {
  background-color: limegreen;
  grid-area: header;
}
.leftsidebar {
  background-color: wheat;
  grid-area: leftsidebar;
}
.main-top {
  grid-area: main-top;
  display: flex;
}
.top-left,
.top-center,
.top-right {
  width: calc(100% / 3);
}
.top-left {
  background-color: lightskyblue;
}
.top-center {
  background-color: indianred;
}
.top-right {
  background-color: moccasin;
}
.bottom-left {
  background-color: deepskyblue;
  grid-area: bottom-left;
}
.bottom-right {
  background-color: yellowgreen;
  grid-area: bottom-right;
}
.footer {
  background-color: darkgoldenrod;
  grid-area: footer;
}

grid-flex.png
 
  • Like
Likes Filip Larsen
  • #5
Can you do grid within grid?

That’s what we would do in Java gridbag layouts when things didn’t work quite right.

We would create a complex component with one grid layout and insert the component into a bigger grid layout.
 
  • #6
jedishrfu said:
Can you do grid within grid?

That’s what we would do in Java gridbag layouts when things didn’t work quite right.

We would create a complex component with one grid layout and insert the component into a bigger grid layout.
nested grids do exist, I tried a bit but could not make it work.
 
  • #8
Using the following CSS with the same HTML as in post #4, I get the same result by having main-top with display set as grid and removing the width of the top containers:

CSS:
.container {
  height: 90vh;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-areas:
    "header header header"
    "leftsidebar main-top main-top"
    "leftsidebar bottom-left bottom-right"
    "leftsidebar footer footer";
}

.header {
  background-color: limegreen;
  grid-area: header;
}
.leftsidebar {
  background-color: wheat;
  grid-area: leftsidebar;
}
.main-top {
  grid-area: main-top;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}
.top-left {
  background-color: lightskyblue;
}
.top-center {
  background-color: indianred;
}
.top-right {
  background-color: moccasin;
}
.bottom-left {
  background-color: deepskyblue;
  grid-area: bottom-left;
}
.bottom-right {
  background-color: yellowgreen;
  grid-area: bottom-right;
}
.footer {
  background-color: darkgoldenrod;
  grid-area: footer;
}
 

FAQ: How to build this using CSS Grid?

How do I create a grid using CSS Grid?

The first step in creating a grid using CSS Grid is to define a container element and set it to display as a grid using the display: grid property. This will allow you to then specify the number of rows and columns in your grid using the grid-template-rows and grid-template-columns properties.

How do I add items to my grid using CSS Grid?

To add items to your grid, you can use the grid-column and grid-row properties to specify where you want the item to be placed in the grid. You can also use the grid-area property to give the item a name and then use that name in your grid-template-areas property to create a visual layout of your grid.

Can I use CSS Grid to create responsive layouts?

Yes, CSS Grid is a great tool for creating responsive layouts as it allows you to easily change the layout of your grid based on different screen sizes. You can use the grid-template-rows and grid-template-columns properties to specify different sizes for your grid cells, and you can also use the grid-template-areas property to rearrange your grid items for different screen sizes.

How do I add spacing between grid items using CSS Grid?

To add spacing between grid items, you can use the grid-gap property. This will allow you to specify the amount of space you want between each row and column in your grid. You can also use the grid-row-gap and grid-column-gap properties to add specific spacing between rows and columns.

Can I use CSS Grid with older browsers?

While CSS Grid is not supported by older browsers, you can use a polyfill like css-grid-polyfill to add support for older browsers. However, it is recommended to use a fallback layout for these browsers to ensure your website still looks and functions correctly.

Similar threads

Replies
3
Views
1K
Replies
5
Views
485
Replies
5
Views
591
Replies
1
Views
1K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
9
Views
2K
Replies
1
Views
3K
Back
Top