Table of Contents
- What is CSS Grid?
- Core Concepts: Grid Terminology
- Grid Container
- Grid Items
- Grid Tracks (Rows & Columns)
- Grid Lines
- Grid Cells
- Grid Areas
- Setting Up a Grid Container
- Defining Grid Tracks (Rows & Columns)
- Units: px, %, fr, auto, and minmax()
- Grid Lines and Numbering
- Grid Cells and Areas
- Naming Areas with
grid-template-areas
- Naming Areas with
- Spacing with Gaps
- Placing Items on the Grid
- Explicit Placement with
grid-columnandgrid-row - The
spanKeyword - Implicit vs. Explicit Tracks
- Explicit Placement with
- Alignment in CSS Grid
- Container Alignment:
justify-contentandalign-content - Item Alignment:
justify-items,align-items,justify-self, andalign-self
- Container Alignment:
- Auto-Fit and Auto-Fill: Responsive Grids Without Media Queries
- Responsive Grids with Media Queries
- Common Grid Patterns
- Card Grid
- Magazine-Style Layout
- Dashboard Layout
- CSS Grid vs. Flexbox: When to Use Which?
- Conclusion
- References
What is CSS Grid?
CSS Grid Layout (or “Grid”) is a two-dimensional layout system designed for laying out items in rows and columns. Unlike Flexbox (which is one-dimensional, focusing on either rows or columns), Grid lets you control both axes at once, making it ideal for overall page layouts, complex UIs, and designs with irregular item placement.
Grid is fully supported in all modern browsers (Chrome, Firefox, Safari, Edge), so you can use it in production with confidence.
Core Concepts: Grid Terminology
Before diving into code, let’s define key Grid terms to avoid confusion:
Grid Container
The parent element with display: grid or display: inline-grid applied. All direct children of the container become grid items.
Grid Items
Direct children of the grid container. Non-direct children (e.g., nested elements) are not grid items by default.
Grid Tracks
The rows and columns of the grid. Think of tracks as the “lines” that divide the grid (e.g., a grid with 3 columns has 3 column tracks).
Grid Lines
The dividing lines that form the boundaries of grid tracks. They are numbered starting from 1 (left-to-right for columns, top-to-bottom for rows).
Grid Cells
The smallest unit of the grid, formed by the intersection of a row and a column (like a cell in a spreadsheet).
Grid Areas
A rectangular region of the grid composed of one or more grid cells. Areas can be named and referenced to place items.
Setting Up a Grid Container
To start using Grid, first define a grid container by setting display: grid on a parent element. All direct children will automatically become grid items.
Example HTML:
<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
</div>
CSS to Create a Grid Container:
.grid-container {
display: grid; /* Enables Grid layout */
}
By default, the grid will have 1 column and as many rows as needed to fit all items (since no tracks are defined yet).
Defining Grid Tracks (Rows & Columns)
To create rows and columns, use grid-template-columns (for columns) and grid-template-rows (for rows). These properties accept a space-separated list of values, where each value defines the size of a track.
Units for Track Sizing
Grid supports multiple units for track sizing. Here are the most common:
px: Fixed pixel size (e.g.,100px).%: Percentage of the container’s size (e.g.,33%).fr: A flexible unit that distributes remaining space (e.g.,1fr).auto: Sizes the track to fit the content (e.g.,auto).minmax(min, max): Defines a track size with a minimum and maximum (e.g.,minmax(100px, 1fr)).
Example: 3 Equal Columns
To create a grid with 3 equal-width columns:
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 3 columns, each taking equal space */
gap: 1rem; /* Adds space between items (see "Spacing with Gaps" below) */
}
Result: 3 columns, each taking 1/3 of the container’s width.
Example: Mixed Units
You can mix units for more control:
.grid-container {
display: grid;
grid-template-columns: 200px 1fr 2fr; /* 1st column: 200px, 2nd: 1 part, 3rd: 2 parts */
grid-template-rows: 100px auto; /* 1st row: 100px, 2nd row: fits content */
}
Grid Lines and Numbering
Grid lines are the invisible lines that separate tracks. For a grid with n columns, there are n + 1 vertical grid lines (numbered 1 to n+1). Similarly, m rows have m + 1 horizontal grid lines.
Example: A 3-column grid has 4 vertical lines (1, 2, 3, 4).
Columns: [Line 1] Column 1 [Line 2] Column 2 [Line 3] Column 3 [Line 4]
You can reference these lines to place items (see Placing Items on the Grid).
Grid Cells and Areas
Grid Cells
A grid cell is the intersection of a row and column. For example, a grid with 2 rows and 2 columns has 4 cells.
Grid Areas
Grid areas let you group cells into named regions (e.g., “header”, “sidebar”, “content”). Use grid-template-areas to define these regions, then assign items to areas with grid-area.
Example: Page Layout with Named Areas
.grid-container {
display: grid;
grid-template-columns: 150px 1fr; /* Sidebar (150px) + Content (remaining space) */
grid-template-rows: auto 1fr auto; /* Header (auto) + Content (flex) + Footer (auto) */
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
height: 100vh; /* Full viewport height */
}
/* Assign items to areas */
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
HTML:
<div class="grid-container">
<header class="header">Header</header>
<aside class="sidebar">Sidebar</aside>
<main class="main">Main Content</main>
<footer class="footer">Footer</footer>
</div>
Result: A classic layout with header, sidebar, main content, and footer.
Spacing with Gaps
Use gap (shorthand for row-gap and column-gap) to add space between grid items. Unlike margins, gaps do not add space outside the grid container.
Syntax:
.grid-container {
gap: <row-gap> <column-gap>; /* Shorthand */
/* Or individual properties: */
row-gap: 1rem; /* Space between rows */
column-gap: 2rem; /* Space between columns */
}
Example:
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 columns */
gap: 1.5rem; /* 1.5rem gap between all rows and columns */
}
Placing Items on the Grid
Grid items can be explicitly placed using their start and end positions on the grid lines. Use grid-column (for columns) and grid-row (for rows) to control placement.
Explicit Placement with grid-column and grid-row
Each property takes a start / end value, where start is the grid line where the item begins, and end is where it ends.
Syntax:
.grid-item {
grid-column: <start-line> / <end-line>;
grid-row: <start-line> / <end-line>;
}
Example: Span 2 Columns
.grid-item:nth-child(2) {
grid-column: 2 / 4; /* Starts at column line 2, ends at line 4 (spans 2 columns) */
}
The span Keyword
Instead of specifying end lines, use span <number> to make an item span a certain number of tracks.
Example:
.grid-item {
grid-column: 1 / span 2; /* Starts at line 1, spans 2 columns */
grid-row: span 3; /* Spans 3 rows */
}
Implicit vs. Explicit Tracks
- Explicit Tracks: Defined with
grid-template-columns/grid-template-rows. - Implicit Tracks: Created automatically when there are more items than explicit tracks.
Control implicit track size with grid-auto-rows (for rows) and grid-auto-columns (for columns):
.grid-container {
grid-template-columns: repeat(2, 1fr); /* 2 explicit columns */
grid-auto-rows: 100px; /* Implicit rows will be 100px tall */
}
Alignment in CSS Grid
Grid provides powerful alignment tools to control how items are positioned within the grid container or their individual cells.
Container Alignment: justify-content and align-content
These properties align the entire grid within the container when the grid is smaller than the container (e.g., due to fixed track sizes).
justify-content: Aligns horizontally (along the inline/main axis).align-content: Aligns vertically (along the block/cross axis).
Values: start, end, center, space-between, space-around, space-evenly.
Example:
.grid-container {
display: grid;
grid-template-columns: repeat(3, 100px); /* Fixed 300px total width */
justify-content: center; /* Centers grid horizontally */
align-content: center; /* Centers grid vertically */
height: 500px; /* Container is taller than grid */
}
Item Alignment: justify-items, align-items, justify-self, and align-self
justify-items: Aligns all items horizontally within their cells (container-level).align-items: Aligns all items vertically within their cells (container-level).justify-self/align-self: Override alignment for individual items.
Values: start, end, center, stretch (default).
Example: Center Items in Their Cells
.grid-container {
justify-items: center; /* All items centered horizontally */
align-items: center; /* All items centered vertically */
}
/* Override for a single item */
.grid-item:nth-child(2) {
justify-self: end; /* Right-aligned */
align-self: start; /* Top-aligned */
}
Auto-Fit and Auto-Fill: Responsive Grids Without Media Queries
Use auto-fit or auto-fill with minmax() to create responsive grids that automatically adjust the number of columns based on screen size—no media queries needed!
auto-fill: Creates as many columns as possible (even empty ones).auto-fit: Collapses empty columns to fit the container.
Example: Responsive Card Grid
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); /* Columns min 200px, max 1fr */
gap: 1rem;
}
Result: Columns will be at least 200px wide. On larger screens, more columns will fit; on smaller screens, fewer columns (even 1).
Responsive Grids with Media Queries
For granular control, combine Grid with media queries to adjust track sizes or layout at specific breakpoints.
Example:
.grid-container {
display: grid;
gap: 1rem;
}
/* Mobile: 1 column */
@media (max-width: 600px) {
.grid-container {
grid-template-columns: 1fr;
}
}
/* Tablet: 2 columns */
@media (min-width: 601px) and (max-width: 900px) {
.grid-container {
grid-template-columns: repeat(2, 1fr);
}
}
/* Desktop: 4 columns */
@media (min-width: 901px) {
.grid-container {
grid-template-columns: repeat(4, 1fr);
}
}
Common Grid Patterns
1. Card Grid
Use auto-fit + minmax() for a responsive card layout:
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 2rem;
padding: 2rem;
}
2. Magazine-Style Layout
Combine named areas and track sizing for an asymmetrical design:
.magazine-layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto auto 1fr;
grid-template-areas:
"header header header"
"sidebar main main"
"sidebar footer footer";
gap: 1.5rem;
}
3. Dashboard Layout
Use grid-template-columns with fixed and flexible tracks:
.dashboard {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: 60px 1fr 1fr;
grid-template-areas:
"sidebar header header"
"sidebar content-1 content-2"
"sidebar content-3 content-4";
height: 100vh;
}
CSS Grid vs. Flexbox: When to Use Which?
Grid and Flexbox are complementary, not competitors! Use:
-
Grid for two-dimensional layouts (rows and columns), such as:
- Overall page layouts (header, sidebar, content).
- Complex UIs with irregular item placement.
- Grids of cards or images.
-
Flexbox for one-dimensional layouts (rows or columns), such as:
- Navigation menus (horizontal or vertical).
- Aligning items in a single row/column (e.g., buttons in a toolbar).
- Centering a single item.
Conclusion
CSS Grid is a game-changer for web layout, offering unprecedented control over two-dimensional designs. By mastering its core concepts—containers, tracks, lines, areas, and alignment—you can build responsive, flexible layouts with clean, maintainable code.
Start small: experiment with grid-template-columns, gap, and auto-fit for card grids, then move to complex layouts with grid-template-areas and media queries. The more you practice, the more intuitive Grid will become!
References
- MDN Web Docs: CSS Grid Layout
- CSS-Tricks: A Complete Guide to Grid
- W3C CSS Grid Specification
- Grid by Example (Rachel Andrew’s tutorials)
- Can I Use: CSS Grid (Browser support)