Grid container
The parent with display: grid.
CSS Grid is a two-dimensional layout system. It controls rows and columns together and is built for real page structure.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}
Layout
When you set display: grid, the container creates grid tracks. Children are placed into rows and columns.
Grid is excellent for card layouts, dashboards, page shells, galleries and areas where both horizontal and vertical structure matter.
The power of grid is not just columns. It is track sizing, placement, gaps and responsive patterns like repeat(auto-fit, minmax()).
The parent with display: grid.
Rows and columns created by grid-template-columns and grid-template-rows.
Distributes available space as fractions.
Sets a track minimum and maximum for responsive behavior.
Visual model
Good layout CSS is not a pile of positions. It is a set of relationships: flow, axis, tracks, placement, layering, sticking and rhythm.
Three equal flexible columns.
Space between rows and columns.
Cards wrap without a breakpoint.
Rows grow based on content by default.
Examples
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1rem;
}
.card { float: left; width: 31%; margin-right: 2%; }
.card:nth-child(3n) { margin-right: 0; }
.cards::after { content: ""; clear: both; display: block; }
Rules that matter
Layout work is where real content, real devices and real users expose weak assumptions. The goal is a structure that explains itself in code and survives change.
If rows and columns both matter, grid is usually cleaner than flexbox.
Grid gap handles row and column spacing without margin math.
Fraction units distribute remaining space cleanly.
minmax() creates tracks that can shrink and grow.
Fixed grids break faster on mobile.
The grid container defines the layout system.
Production thinking
Grid makes complex layouts readable in code. It turns page structure into a clear system instead of width math.
Grid can visually rearrange items. Preserve meaningful source order unless there is a strong reason.
Use grid for dashboards, card groups, page shells and galleries. Document repeat/minmax patterns for reuse.
Grid keeps semantic HTML independent from layout, which helps maintain clean content structure.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these questions before moving on. If the answer is vague, change the lab layout and inspect the result.
Senior audit upgrade
Grid is strongest when rows and columns are part of the design. Flexbox is strongest when items mainly flow in one direction.
Articles and simple stacked content usually do not need grid.
Toolbars, nav rows and one-dimensional alignment are flex territory.
Dashboards, card grids and page regions with rows and columns are grid territory.