Lesson

CSS Grid

10 min reading
Free Course

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

display: grid

Makes the element a grid container

grid-template-columns

Defines column tracks (fr, px, minmax(), repeat())

grid-template-rows / grid-auto-rows

Defines explicit rows, or the size of rows created automatically

gap

Sets spacing between rows and columns

grid-template-areas

Names layout zones such as header and sidebar

Core Grid Units and Functions

  • fr (Fractional Unit): Divides remaining free space proportionally (1fr 2fr makes 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; or 16px 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

  1. 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.

  2. Resize the window, or use device mode, down to about 375px wide. Check that text is readable and nothing scrolls sideways.

  3. Put a long code block or a long unbroken string inside a grid item and confirm the layout holds (see the first mistake below).

  4. Press Tab through the page and confirm the focus order still makes sense

  5. 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 1fr without minmax(0, 1fr) when columns hold pre code blocks: Bare 1fr behaves like minmax(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 Tab order follow HTML source order, not visual order or grid-area placement.

  • Setting rigid grid-template-columns: repeat(4, 1fr) without mobile breakpoints: Four narrow columns crush text on 375px phones unless you use auto-fit or 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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
CSS Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum