Grid Container
Grid Container
Basic Setup
.container {
display: grid;
}
grid-template-columns
Define column structure:
.container {
/* Fixed columns */
grid-template-columns: 200px 1fr 200px;
/* Repeat syntax */
grid-template-columns: repeat(3, 1fr);
/* Auto-fill */
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
/* Auto-fit */
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
grid-template-rows
Define row structure:
.container {
grid-template-rows: 100px auto 100px;
grid-template-rows: repeat(3, minmax(100px, auto));
}
gap
Space between items:
.container {
gap: 20px; /* Both row and column */
row-gap: 20px; /* Row gap only */
column-gap: 10px; /* Column gap only */
}
grid-template
Shorthand for columns and rows:
.container {
grid-template: 100px 1fr 100px / 200px 1fr 200px;
}
Grid Lines
.container {
grid-template-columns: [col-start] 200px [col-end] 1fr [col-end];
grid-template-rows: [row-start] 100px [row-end] auto [row-end];
}
Grid Items
Grid Items
Placing Items
.item1 {
grid-column: 1 / 3; /* Span columns 1 to 3 */
grid-row: 1 / 2; /* Span rows 1 to 2 */
}
.item2 {
grid-column: span 2; /* Span 2 columns */
grid-row: span 3; /* Span 3 rows */
}
grid-area
Name or place items:
.item1 {
grid-area: header;
}
.item2 {
grid-area: 1 / 1 / 2 / 3; /* row-start / col-start / row-end / col-end */
}
Alignment
Self Alignment
.item {
justify-self: start; /* Align to start */
justify-self: end; /* Align to end */
justify-self: center; /* Center */
justify-self: stretch; /* Stretch to fill */
align-self: center; /* Vertical alignment */
}
All Items
.container {
justify-items: center; /* All items */
align-items: center;
}
grid-auto-rows / grid-auto-columns
Size for implicit tracks:
.container {
grid-auto-rows: minmax(100px, auto);
grid-auto-columns: 1fr;
}
Template Areas
Template Areas
Named Grid Areas
.layout {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"sidebar content aside"
"footer footer footer";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
Responsive Template Areas
.layout {
grid-template-areas:
"header"
"content"
"footer";
}
@media (min-width: 768px) {
.layout {
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
}
}
@media (min-width: 1024px) {
.layout {
grid-template-columns: 200px 1fr 200px;
grid-template-areas:
"header header header"
"sidebar content aside"
"footer footer footer";
}
}
Visual Grid
+-------------------------------------+
| header |
+--------+----------------+-----------+
| sidebar| content | aside |
+--------+----------------+-----------+
| footer |
+-------------------------------------+
Common Grid Patterns
Common Grid Patterns
Responsive Card Grid
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
Dashboard Layout
.dashboard {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 60px 1fr;
grid-template-areas:
"sidebar header"
"sidebar main";
height: 100vh;
}
Photo Gallery
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 5px;
}
.gallery img {
width: 100%;
height: 100%;
object-fit: cover;
}
Centered Content
.page {
display: grid;
grid-template-columns: 1fr min(65ch, 100%) 1fr;
}
.page > * {
grid-column: 2;
}
Holy Grail with Grid
.layout {
display: grid;
grid-template:
"header header header" 60px
"nav main aside" 1fr
"footer footer footer" 60px
/ 200px 1fr 200px;
min-height: 100vh;
}
Auto-Fit vs Auto-Fill
/* auto-fit: items stretch to fill space */
.grid { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
/* auto-fill: items keep their size */
.grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
Grid vs Flexbox
| Feature | Grid | Flexbox |
|---|---|---|
| Dimension | 2D | 1D |
| Layout | Page-level | Component-level |
| Alignment | Both axes | Main axis only |
| Overlap | Yes (grid-area) | No |
| Naming | Yes (template areas) | No |
Practice Problems
Create a reusable React component implementing CSS Grid. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for CSS Grid using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize CSS Grid for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What is the main difference between Grid and Flexbox?
2. What does grid-template-areas allow you to do?
3. What does 1fr mean in grid?
4. What is the difference between auto-fit and auto-fill?
5. How do you create a responsive card grid with Grid?
Flashcards
Question
What does 1fr mean in CSS Grid?
Click to reveal answer
Answer
1 fraction of available space. Multiple fr units divide space proportionally.
Question
When should you use Grid vs Flexbox?
Click to reveal answer
Answer
Grid for page-level two-dimensional layouts. Flexbox for component-level one-dimensional layouts.
Question
What is grid-template-areas?
Click to reveal answer
Answer
A CSS Grid feature that lets you name sections of your layout to create visual grid templates.
Question
How do you create a responsive grid without media queries?
Click to reveal answer
Answer
Use repeat(auto-fit, minmax(min-width, 1fr)) to automatically adjust columns.
Question
What is the gap property in Grid?
Click to reveal answer
Answer
Sets the spacing between grid items. Replaces the old grid-gap property.
Revision Notes
Key Takeaways
- 1.CSS Grid is two-dimensional, handling rows and columns
- 2.fr units divide available space proportionally
- 3.template-areas create visual, named layouts
- 4.auto-fit with minmax creates responsive grids without media queries
- 5.Grid is best for page-level layouts, Flexbox for components
Interview Tips
- •Explain when to use Grid vs Flexbox
- •Know the difference between auto-fit and auto-fill
- •Be able to create a responsive grid layout
- •Understand template areas for complex layouts
Cheat Sheet
CSS Grid Cheat Sheet
Container Properties
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
gap: 20px;
grid-template-areas: "header header" "sidebar main" "footer footer";
Item Properties
grid-column: 1 / 3;
grid-row: span 2;
grid-area: header;
justify-self: center;
align-self: center;
Responsive Grid
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
Units
fr- fraction of spacepx- fixed pixels%- percentage of containerminmax()- min and max sizeauto- based on content
Template Areas
grid-template-areas:
"header header"
"sidebar main"
"footer footer";