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

Discussion Overview

The discussion revolves around building a specific layout using CSS Grid, exploring various approaches and techniques. Participants share their code snippets, suggest alternative methods, and discuss the potential for nested grids.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a CSS Grid layout code and notes that the first layout requires different logic, mentioning the limitation of not using grid-template-areas for the entire grid.
  • Another participant suggests considering the layout as consisting of 5 columns instead of 4, proposing that certain rows span multiple columns.
  • A different approach is presented using Flexbox, with a nested structure for the main-top section, allowing for a more flexible layout.
  • Questions arise about the feasibility of using nested grids, with some participants recalling similar practices from Java gridbag layouts.
  • One participant mentions their attempt at creating nested grids but indicates they were unsuccessful, prompting suggestions for resources to troubleshoot their approach.
  • A later reply proposes a method to achieve the same layout by setting the main-top to display as grid and adjusting the width of its child elements.

Areas of Agreement / Disagreement

Participants express various approaches to the layout problem, with no consensus on a single method. Multiple competing views and techniques remain under discussion.

Contextual Notes

Some participants reference limitations in their approaches, such as challenges with nested grids and the need for specific adjustments to achieve desired layouts.

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: 146
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
3K
  • · 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