CSS Grid is the most powerful layout system in CSS, but it has specific requirements. When grid layouts don't work as expected, it's usually due to missing container setup, incorrect column/row definitions, or misunderstanding how grid items are placed.
/* ❌ Problem "” no column definition */
.container { display: grid; }
/* Items just stack in one column */
/* ✅ Solution "” define columns */
.container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
gap: 16px;
}
/* ✅ Responsive grid */
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 16px;
}
/* ❌ display:grid without columns = 1 column */
.grid { display: grid; }
/* All items stack vertically */
/* ✅ Define columns explicitly */
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */
/* or shorthand */
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
/* ❌ Area name mismatch */
.container {
grid-template-areas:
"header header"
"sidebar main"; /* "main" defined here */
}
.content { grid-area: content; } /* ❌ "content" doesn't exist! */
/* ✅ Names must match exactly */
.container {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"sidebar main";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; } /* ✅ Matches template */
/* ✅ Responsive grid "” no media queries needed */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 24px;
}
/* auto-fill: creates as many columns as fit
minmax(250px, 1fr): min 250px, max 1fr */
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
/* ✅ Span across columns */
.featured { grid-column: span 2; } /* Spans 2 columns */
.full-width { grid-column: 1 / -1; } /* Spans all columns */
/* ✅ Span rows */
.tall { grid-row: span 2; }
You need to define grid-template-columns. Without it, CSS Grid creates a single column by default. Add grid-template-columns: repeat(3, 1fr) or similar to create multiple columns.
auto-fill creates as many columns as fit, leaving empty columns if items don't fill the row. auto-fit collapses empty columns and stretches existing items to fill the space.
Use grid-column: span 2 to span 2 columns, or grid-column: 1 / -1 to span all columns. For rows, use grid-row: span 2.
fr (fraction) represents a fraction of the available space. 1fr 2fr 1fr creates 3 columns where the middle is twice as wide as the others. It's like flex-grow for grid.
Use browser DevTools "” in Chrome/Firefox, inspect a grid container and click the grid badge. This shows a visual overlay of grid lines, areas, and item placement.
Explore 500+ free tutorials across 20+ languages and frameworks.