CSS Grid Layout is a powerful two-dimensional layout system for the web. It lets you arrange items into rows and columns with precise control, making it easier to create complex page layouts, dashboards, galleries, and responsive designs compared to older techniques like floats or manual positioning.
A Grid Container is any element where you apply display: grid or display: inline-grid. It creates a new grid context for its direct children (the grid items).
grid or inline-grid.justify-content and align-content.Grid Items are the direct children of a grid container. Each item can be placed, resized, and aligned independently using grid item properties.
align-self and justify-self.Typically, you have one element acting as the grid container and several child elements as grid items:
<div class="grid-container">).display: grid to that wrapper.grid-template-columns and grid-template-rows.grid-column and grid-row.
/* Define a simple 3-column grid with equal-width tracks */
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
/* Basic grid item styling */
.grid-item {
padding: 12px;
background: #e5e7eb;
text-align: center;
font-weight: 600;
}
<!-- HTML: 6 items inside the grid container -->
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
Below is a simple live preview of a 3-column grid, similar to the code above:
Each box is a grid item. The grid container uses grid-template-columns: repeat(3, 1fr), so the available width is split into three equal tracks, and items automatically flow into rows.
You can make items span multiple columns or rows using the grid-column and grid-row properties.
/* Grid where some items span multiple columns */
.grid-span-demo {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
}
/* Item that spans 2 columns */
.item-span-2 {
grid-column: span 2;
}
/* Item that spans full width from line 1 to 4 (1 / 4) */
.item-full {
grid-column: 1 / 4;
}
The first item below spans two columns, and the last item spans all three columns:
Named grid areas make it easy to describe your layout in a more visual and semantic way. You can map sections like header, sidebar, and content directly in CSS.
/* Two-column layout using named grid areas */
.layout {
display: grid;
grid-template-areas:
"header header"
"sidebar content";
grid-template-columns: 1fr 3fr;
gap: 16px;
}
/* Map each element to a named area */
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
}
<!-- HTML structure mapped to areas -->
<div class="layout">
<header class="header">Header</header>
<aside class="sidebar">Sidebar</aside>
<main class="content">Main Content</main>
</div>
minmax() to create flexible tracks, e.g., minmax(150px, 1fr).auto-fit or auto-fill with repeat() for responsive grids.grid-auto-rows and grid-auto-columns control the size of implicitly created tracks.align-items and justify-items.
/* Responsive card grid that wraps automatically */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}
grid-template-areas with semantic HTML elements (<header>, <main>, <aside>) for better readability.fr units for flexible tracks instead of fixed pixel widths.gap instead of margins to separate grid items cleanly.grid-column: 1 / -1 to make a header span the full width of the grid.grid-template-areas with areas like header, sidebar, content, and footer.repeat(auto-fit, minmax()) so cards wrap nicely on smaller screens.