How to build this using CSS Grid?

  • Context: HTML/CSS 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Build Css Grid
Click For Summary
SUMMARY

This discussion focuses on building complex layouts using CSS Grid, specifically addressing the challenges of creating nested grids. The participants share various code snippets demonstrating how to structure a layout with multiple grid areas, including a main layout and a nested grid within the main area. Key techniques discussed include using grid-template-areas for layout definition and the use of flexbox for specific components. The conversation highlights the importance of adjusting grid properties to achieve desired visual results.

PREREQUISITES
  • Understanding of CSS Grid layout properties
  • Familiarity with flexbox for layout adjustments
  • Basic knowledge of HTML structure for grid implementation
  • Experience with responsive design principles
NEXT STEPS
  • Explore advanced CSS Grid techniques, including grid-template-areas and nested grids
  • Learn about flexbox integration within CSS Grid layouts
  • Investigate responsive design strategies using CSS Grid
  • Review practical examples and tutorials on CSS Grid from resources like FreeCodeCamp
USEFUL FOR

Web developers, UI/UX designers, and anyone looking to enhance their skills in creating responsive and complex layouts using CSS Grid.

shivajikobardan
Messages
637
Reaction score
54
TL;DR
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: 143
Technology news on Phys.org
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.
 
 
Last edited:
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   Reactions: Filip Larsen
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.
 
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.
 
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;
}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
10
Views
2K