π What Are Grid Container Properties?
Grid Container Properties are the CSS rules applied to the parent elementthat activates and controls the entire grid layout. These properties determine how rows, columns, gaps, alignment, and auto-placement behave.
ποΈ 1. display: grid / inline-grid
This property transforms an element into a grid container.
Activating Grid
.container {
display: grid; /* Block-level grid */
}
.inline-container {
display: inline-grid; /* Inline-level grid */
}Note
π 2. grid-template-columns
Defines the width of each column in the grid. Accepts values like px, %, fr, auto, minmax(), and repeat().
Column Definitions
grid-template-columns: 200px 1fr 2fr;
grid-template-columns: repeat(3, 1fr);
grid-template-columns: minmax(150px, 1fr) 2fr;π 3. grid-template-rows
Defines the height of rows in a similar way to columns.
Row Definitions
grid-template-rows: 100px auto 80px;
grid-template-rows: repeat(4, minmax(100px, auto));πΊοΈ 4. grid-template-areas
Creates named layout regions that make grid structures easy and visual.
Named Areas Example
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
π 5. grid-auto-flow
Controls how auto-placed items fill the grid. Options: row | column | dense
Auto Flow Examples
grid-auto-flow: row; /* default */
grid-auto-flow: column;
grid-auto-flow: row dense; /* fills gaps smartly */Note
π¦ 6. grid-auto-rows & grid-auto-columns
Specifies sizes for rows or columns created implicitly (not defined in template).
Implicit Track Sizes
grid-auto-rows: 150px;
grid-auto-columns: minmax(100px, auto);π§© 7. gap, row-gap, column-gap
Defines spacing between grid items. Formerly known as grid-gap.
Gap Usage
gap: 20px; /* row + column */
row-gap: 10px;
column-gap: 20px;ποΈ 8. justify-items / align-items / place-items
Controls item alignment inside their grid cells.
Align Items Within Cells
justify-items: start | center | end | stretch;
align-items: start | center | end | stretch;
place-items: center; /* shorthand */π§ 9. justify-content / align-content / place-content
Aligns the entire grid within the container.
Align Whole Grid
justify-content: center;
align-content: space-between;
place-content: center;π§ Understanding Explicit vs Implicit Grid
Explicit Grid
Defined using grid-template-rows, grid-template-columns, or grid-template-areas.
Implicit Grid
Automatically generated tracks when items overflow the defined grid. Controlled using grid-auto-rows and grid-auto-columns.
π Grid Container Properties Overview Table
| Property | Purpose |
|---|---|
| display | Enables grid layout |
| grid-template-columns | Defines column tracks |
| grid-template-rows | Defines row tracks |
| grid-template-areas | Creates named layout regions |
| grid-auto-flow | Controls auto-placement behavior |
| grid-auto-rows | Size of implicit rows |
| grid-auto-columns | Size of implicit columns |
| gap | Spacing between items |
| justify-items | Align items horizontally |
| align-items | Align items vertically |
| justify-content | Align grid horizontally |
| align-content | Align grid vertically |
π― Summary
Grid Container Properties define the full structure and behavior of a grid layout. By mastering these, you control spacing, alignment, auto-placement, and layout flow with precision and elegance.
Learn more:MDN Grid Docs π