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

krishankrishan
3 min read

πŸ‘‹ 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 grid

  • align-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)

FeatureGridFlexbox
Layout type2D (rows & columns)1D (row or column)
Use caseFull-page/layoutsComponents/widgets
AlignmentMore powerfulSimpler

πŸ’Ό Interview Questions (For 3+ Years Experience)

  1. What’s the difference between auto-fill and auto-fit in Grid?

  2. How does grid-template-areas improve layout development?

  3. What is the use of minmax() in CSS Grid?

  4. Difference between align-items, align-content, justify-items, and justify-content?

  5. What happens when a grid item spans more columns than defined?

  6. What are nested grids? When and why would you use them?

πŸ‘‰ Make sure you can demonstrate examples live in an interview!



0
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!