01. Grid Container & Track Sizing (fr, minmax, auto-fit, auto-fill)
🟡 Intermediate
1. Definition
CSS Grid Layout is a 2-dimensional grid-based layout engine designed to align content into rows and columns simultaneously. grid-template-columns defines column tracks using the fraction fr unit, minmax(), and auto-placement keywords.
2. Why It Is Used
CSS Grid manages complex 2D dashboard layouts, photo galleries, and auto-responsive card grids that automatically reflow without media query breakpoints.
3. How It Works
display: grid creates a 2D layout context. repeat(auto-fit, minmax(250px, 1fr)) creates as many 250px-minimum columns as fit inside container width, stretching remaining free space with 1fr.
4. Syntax
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
5. Auto-fit vs Auto-fill Reference Table
| Keyword | Behavior when items do not fill row |
|---|---|
auto-fit | Collapses empty tracks and stretches existing items to fill full width |
auto-fill | Keeps empty tracks allocated as empty grid columns |
6. Beginner Example
.three-column-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 16px;
}
7. Intermediate Example
.responsive-card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 24px;
}
8. Advanced Grid Dashboard Structure Example
.dashboard-grid {
display: grid;
grid-template-columns: 260px 1fr;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
9. Line-by-Line Code Explanation
repeat(auto-fit, minmax(280px, 1fr)): Automatically generates responsive columns with minimum 280px width, expanding to fill free space.
10. Expected Terminal / Browser Output
A multi-column card layout that automatically adjusts column count seamlessly as screen width shrinks.
11. Common Mistakes & Pitfalls
- Using Grid for simple 1D single-row bars: Use Flexbox for 1D single-axis alignment, reserving CSS Grid for 2D layouts.
12. Important Rules & Limitations
gapproperty sets spacing between tracks without adding margins on outer grid borders.
13. Multilingual Explanation: English
CSS Grid is a 2D layout system. repeat(auto-fit, minmax()) creates auto-responsive card grids without media queries.
14. Multilingual Explanation: Hindi (Roman Script)
CSS Grid 2D layout system hai. Auto-responsive cards ke liye repeat(auto-fit, minmax(250px, 1fr)) ka use karein.
15. Multilingual Explanation: Marathi (Roman Script)
CSS Grid 2D layout (rows ani columns) sathi vaparla jaato.
16. Practice Questions
- What is the difference between
auto-fitandauto-fillin CSS Grid?
17. Practical Coding Challenge
Create an auto-responsive product grid using repeat(auto-fit, minmax(220px, 1fr)) and gap: 20px.