β Mastering CSS Grid: Beginner to Advanced with Real-World Examples + Interview Questions

π Introduction
CSS Grid is a powerful layout system that allows developers to create two-dimensional layouts easily. Whether you're building dashboards, cards, or full-page layouts β Grid is your friend.
In this blog, weβll go from basic to advanced, using practical examples, and wrap it up with interview questions and revision tips.
π° Step 1: Basic Grid Setup
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;
}
display: grid
β activates grid mode.grid-template-columns
β defines 3 equal columns.gap
β adds spacing between items.
π§± Step 2: Grid with Fixed and Flexible Columns
.container {
display: grid;
grid-template-columns: 200px 1fr 2fr;
}
First column = fixed
200px
Second = flexible
Third = 2x the second column
π§ Step 3: Using minmax()
for Responsive Layouts
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}
Makes items auto-fit across the screen.
Great for cards, galleries, etc.
π· Step 4: Grid Template Areas for Readable Layouts
.grid-layout {
display: grid;
grid-template-areas:
"header header"
"nav main"
"nav aside"
"footer footer";
grid-template-columns: 1fr 3fr;
gap: 10px;
}
.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
Visual layout using names β makes things super readable and easy to maintain.
π Alignment & Justification in Grid
.container {
display: grid;
place-items: center; /* shorthand for justify-items & align-items */
height: 100vh;
}
Or use individually:
justify-items: center | start | end
align-items: center | start | end
justify-content
β aligns the whole gridalign-content
β aligns the whole grid vertically
π Nested Grids (Advanced)
<div class="outer-grid">
<div class="inner-grid">
<div>Item 1</div>
<div>Item 2</div>
</div>
</div>
.outer-grid {
display: grid;
grid-template-columns: 1fr 1fr;
}
.inner-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
Useful when you want sub-layouts inside a parent grid.
π₯ Grid vs Flexbox (Quick Comparison)
Feature | Grid | Flexbox |
Layout type | 2D (rows & columns) | 1D (row or column) |
Use case | Full-page/layouts | Components/widgets |
Alignment | More powerful | Simpler |
πΌ Interview Questions (For 3+ Years Experience)
Whatβs the difference between
auto-fill
andauto-fit
in Grid?How does
grid-template-areas
improve layout development?What is the use of
minmax()
in CSS Grid?Difference between
align-items
,align-content
,justify-items
, andjustify-content
?What happens when a grid item spans more columns than defined?
What are nested grids? When and why would you use them?
π Make sure you can demonstrate examples live in an interview!
π Useful Links
π Bonus: GitHub & Social Links
Subscribe to my newsletter
Read articles from krishan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

krishan
krishan
π Hey, I'm a Frontend Developer passionate about building clean, user-friendly interfaces. π Learning and sharing everything from React, JavaScript, HTML/CSS to advanced topics like Data Structures, Algorithms & System Design. π» Documenting my journey from fundamentals to becoming a top-tier developer β one blog at a time. π Join me as I break down complex topics into easy, practical lessons β with GitHub repos, code snippets, and real-world examples. π Consistent growth, community learning, and aiming high!