Table of Contents
- Overview of Grid-Specific Properties
grid-template-areas
: Named Layoutsgrid-auto-flow
: Control Auto Placementminmax()
,repeat()
, and Auto-Fit/Filljustify-items
,align-items
,place-items
justify-content
,align-content
,place-content
- Responsive Grids with Media Queries
- Real-World Layout Patterns
- Summary and Best Practices
1. Overview of Grid-Specific Properties
In the last module, we covered how to create basic grids. Now we go deeper into fine-tuning layouts using Grid’s powerful properties. This includes custom grid areas, alignment options, auto-placement strategies, and more.
Key Grid Properties:
grid-template-areas
grid-auto-flow
grid-auto-rows
/grid-auto-columns
grid-column
,grid-row
,grid-area
- Alignment properties (
justify-*
,align-*
,place-*
)
2. grid-template-areas
: Named Layouts
Named areas improve readability and maintainability of grid layouts by letting you define your layout in a visual, text-like format.
Example:
.container {
display: grid;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
grid-template-columns: 1fr 3fr;
grid-template-rows: auto 1fr auto;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer { grid-area: footer; }
This makes layouts intuitive and flexible to modify.
3. grid-auto-flow
: Control Auto Placement
Defines how auto-placed items are inserted into the grid:
.container {
grid-auto-flow: row; /* default */
grid-auto-flow: column; /* places items column-wise */
grid-auto-flow: dense; /* fills holes in the grid */
}
dense
is great for masonry-like layouts, but may break the logical order of elements.
4. minmax()
, repeat()
, and Auto-Fit/Fill
minmax(min, max)
Allows flexible sizing of rows or columns:
grid-template-columns: repeat(3, minmax(100px, 1fr));
repeat()
Helps avoid repetition:
grid-template-columns: repeat(4, 1fr);
auto-fit
vs auto-fill
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
- auto-fill: Fills the row with as many columns as possible.
- auto-fit: Similar to auto-fill, but collapses empty columns.
5. justify-items
, align-items
, place-items
These control the alignment of items within their grid cells.
.container {
justify-items: center; /* horizontal */
align-items: center; /* vertical */
place-items: center; /* shorthand for both */
}
6. justify-content
, align-content
, place-content
These control how the whole grid aligns within the container, when there is extra space.
.container {
justify-content: space-between; /* grid horizontally */
align-content: center; /* grid vertically */
place-content: center; /* shorthand */
}
7. Responsive Grids with Media Queries
You can make grids responsive using repeat()
, auto-fit
, and media queries:
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
Or change grid layout on smaller screens:
@media (max-width: 768px) {
.grid {
grid-template-columns: 1fr;
}
}
8. Real-World Layout Patterns
Two-Column Layout with Sidebar
.container {
display: grid;
grid-template-columns: 1fr 3fr;
gap: 20px;
}
Holy Grail Layout
.container {
display: grid;
grid-template-areas:
"header header"
"nav main"
"footer footer";
grid-template-columns: 1fr 4fr;
grid-template-rows: auto 1fr auto;
}
Card Grid
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 1rem;
}
This setup ensures responsive behavior with a minimum card width.
9. Summary and Best Practices
✅ Use grid-template-areas
for readable layouts
✅ Prefer repeat()
and minmax()
for DRY code
✅ Combine auto-fit
with minmax()
for responsive designs
✅ Use alignment properties to fine-tune layouts
✅ Don’t over-nest — Grid reduces the need for deeply nested HTML
Grid layouts make structuring web pages simpler and more maintainable, especially for responsive, modular, and scalable designs. Mastery of these properties sets a solid foundation for modern web development.