CSS Grid
CSS Grid: Rows, Columns, and Page Layouts
CSS Grid is a two-dimensional layout system that controls columns and rows at the same time. Flexbox arranges items along a single axis, such as a toolbar or a list. CSS Grid lets you build whole application shells, dashboard card grids, and magazine-style layouts with little markup and no extra wrapper elements.
This guide covers the core units and functions, two complete layout examples, and the mistakes that most often break grids on real devices.
What Is CSS Grid?
CSS Grid turns an element into a grid container by setting display: grid. Its direct children become grid items, and you place them into rows and columns that you define on the container. The lines between rows and columns are called grid lines, and the spaces between two lines are called tracks.
The main properties you will use:
Property | What it does |
| Makes the element a grid container |
| Defines column tracks ( |
| Defines explicit rows, or the size of rows created automatically |
| Sets spacing between rows and columns |
| Names layout zones such as |
Core Grid Units and Functions
fr(Fractional Unit): Divides remaining free space proportionally (1fr 2frmakes the second column twice as wide as the first).repeat(count, track): Avoids repeating identical tracks (repeat(3, 1fr)).minmax(min, max): Bounds track width safely so cards never crush narrower than readable widths.gap: Adds spacing between rows and columns (gap: 20px;or16px 24px).grid-column: span 2: Lets featured cards span across two grid columns cleanly.
.dashboard-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 20px;
}
.page-shell {
display: grid;
grid-template-columns: 240px minmax(0, 1fr);
grid-template-areas:
"header header"
"sidebar main";
gap: 20px;
}
.page-shell header { grid-area: header; }
.page-shell aside { grid-area: sidebar; }
.page-shell main { grid-area: main; }
CSS Grid vs. Flexbox
They work well together. A common pattern is to use CSS Grid for the overall page structure and Flexbox inside each card for things like aligning a button to the bottom or spacing an icon and a label.
Use Grid when you care about rows and columns together, or when items should line up in both directions.
Use Flexbox when you are arranging a single row or column of items and want the content to decide the sizes.
Neither replaces the other, and nesting a Flexbox container inside a grid item is completely normal.
How to Check That Your Grid Works
Open your browser DevTools and inspect the grid container. Chrome, Edge, and Firefox show a grid badge in the Elements (or Inspector) panel. Clicking it overlays the grid lines, track sizes, and area names on the page.
Resize the window, or use device mode, down to about 375px wide. Check that text is readable and nothing scrolls sideways.
Put a long code block or a long unbroken string inside a grid item and confirm the layout holds (see the first mistake below).
Press Tab through the page and confirm the focus order still makes sense
Test with fewer and more items than you expect. Auto-fit grids behave differently with one card than with twelve.
Common Mistakes to Avoid
Using
1frwithoutminmax(0, 1fr)when columns holdprecode blocks: Bare1frbehaves likeminmax(auto, 1fr), allowing wide code blocks to push columns off screen;minmax(0, 1fr)keeps tracks strictly bounded.Reordering visual grid cards away from HTML DOM order: Screen readers and keyboard
Taborder follow HTML source order, not visualorderorgrid-areaplacement.Setting rigid
grid-template-columns: repeat(4, 1fr)without mobile breakpoints: Four narrow columns crush text on 375px phones unless you useauto-fitor a media query.
Frequently Asked Questions
Can I nest a Flexbox container inside a CSS Grid item?
Yes! Combining CSS Grid for the overall 2D page skeleton and Flexbox inside each card (for aligned buttons or icons) is standard modern practice.
How does grid-template-areas simplify responsive changes?
You can define "header" "main" "sidebar" in mobile styles and switch to "header header" "sidebar main" inside a media query without touching a single HTML tag.
Conclusion
CSS Grid works best when you let it handle rows and columns together. Use repeat() with minmax() for flexible card grids, grid-template-areas for readable page shells, and minmax(0, 1fr) to keep wide content from breaking the layout. Keep the HTML in a logical reading order, test at phone widths, and combine Grid with Flexbox where each one fits best.
Looking for more free lessons on CSS, HTML, JavaScript, and PHP? Visit KoderSolution.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.