CSS Grid
CSS Grid is a two-dimensional layout system — it lets you control rows AND columns at the same time. While Flexbox is perfect for one direction, Grid is designed for full page layouts, complex galleries, and magazine-style designs.
Flexbox vs Grid — When to Use Which?
- Flexbox — one dimension (row OR column). Ideal for navbars, toolbars, card rows.
- Grid — two dimensions (rows AND columns). Ideal for page layouts, galleries, dashboards.
- Both can be combined in the same project.
Grid Container vs Grid Items
.grid {
display: grid;
}
/* HTML */
<div class="grid">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
grid-template-columns
Defines the number and width of columns.
/* Three equal columns */
.grid { grid-template-columns: 1fr 1fr 1fr; }
/* Fixed + flexible */
.grid { grid-template-columns: 200px 1fr 1fr; }
/* Using repeat() */
.grid { grid-template-columns: repeat(3, 1fr); }
/* Responsive auto-fit */
.grid { grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); }
/* Percentage + gap */
.grid { grid-template-columns: 30% 70%; }
1fr means "1 fraction of available space". repeat(auto-fit, minmax(250px, 1fr)) is the most powerful one-liner for responsive grids — it automatically adjusts columns based on screen width.
grid-template-rows
.grid {
grid-template-rows: 100px 200px auto;
}
/* With repeat */
.grid { grid-template-rows: repeat(4, 100px); }
gap — Spacing Between Grid Cells
.grid {
gap: 20px; /* Both row and column */
row-gap: 20px; /* Row only */
column-gap: 30px; /* Column only */
}
Placing Items — Grid Lines
Every grid has numbered lines. You can place items by line numbers.
.item {
grid-column: 1 / 3; /* Spans columns 1 to 2 */
grid-row: 1 / 2; /* First row only */
}
/* Span syntax */
.item {
grid-column: span 2; /* Span 2 columns */
}
grid-template-areas — Named Layout
The most readable way to build complex layouts.
.layout {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"sidebar main aside"
"footer footer footer";
gap: 16px;
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
justify-items & align-items
.grid {
justify-items: center; /* Align items horizontally */
align-items: center; /* Align items vertically */
}
/* Individual item */
.item {
justify-self: end;
align-self: start;
}
justify-content & align-content
Align the entire grid within its container when the grid is smaller than the container.
.grid {
justify-content: center; /* Horizontal */
align-content: center; /* Vertical */
}
Real-World Example 1 — Responsive Card Grid
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 24px;
}
.card {
padding: 24px;
background: white;
border-radius: 16px;
}
Real-World Example 2 — Holy Grail Layout
.page {
display: grid;
grid-template-rows: auto 1fr auto;
grid-template-columns: 250px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: main; }
footer { grid-area: footer; }
Real-World Example 3 — Dashboard
.dashboard {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 16px;
}
.widget-1 { grid-column: span 6; }
.widget-2 { grid-column: span 6; }
.widget-3 { grid-column: span 4; }
.widget-4 { grid-column: span 8; }
Responsive Grid with Media Queries
.gallery {
display: grid;
grid-template-columns: 1fr;
gap: 16px;
}
@media (min-width: 768px) {
.gallery {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 1024px) {
.gallery {
grid-template-columns: repeat(3, 1fr);
}
}
Build a magazine-style layout with Grid:
• Header (full width)
• Feature article (spanning 2 columns)
• Sidebar (1 column)
• 4 blog cards below
Use grid-template-areas for the top section and auto-fit for the card section.
Quick Revision
display: gridcreates a grid container.grid-template-columns/rowsdefines track sizes.frunit = fraction of available space.repeat(auto-fit, minmax(250px, 1fr))= responsive grid.gapspaces cells.grid-column/grid-rowplace items by line numbers.grid-template-areas= named layout (most readable).justify-items/align-itemsalign items in cells.justify-content/align-contentalign the whole grid.
Interview Questions
Q: Difference between Flexbox and Grid?
A: Flexbox is one-dimensional (row OR column) — perfect for content-driven layouts. Grid is two-dimensional (rows AND columns) — perfect for page-level layouts. Use both together where needed.
Q: What does 1fr mean?
A: 1fr means one fraction of the available free space. In grid-template-columns: 1fr 2fr, the second column gets twice the space of the first.
Q: What does repeat(auto-fit, minmax(250px, 1fr)) do?
A: It creates as many columns as can fit, each at least 250px wide, and distributes remaining space equally. When the container shrinks below 500px, it drops to 1 column automatically — a fully responsive grid without media queries.
Q: What is grid-template-areas?
A: A way to define a layout using named areas in a visual map. You then assign areas to items with grid-area. It's the most human-readable way to build complex layouts.
Want to Master Web Development?
Join our Web Development course in Varanasi — practical classes and career support at our Sarnath centre.