Skip to content
SoftwareSolutions.net
Intermediate

How to Build a Responsive Layout With CSS Grid

Create page layouts that adapt to phones, tablets and desktops using grid templates, fractional units and media queries.

Last updated 2026-11-28

What CSS Grid solves

CSS Grid is a two-dimensional layout system. It lets you define rows and columns on a container and place items into them, which makes page structures such as headers, sidebars and card galleries straightforward. Before grid, developers relied on floats and flexbox hacks that made complex layouts fragile and hard to maintain.

Grid works well alongside flexbox. Use grid for the overall page structure and card layouts, and flexbox for aligning items within a single row, such as a navigation bar or a group of buttons.

Define columns that adapt automatically

The most useful responsive pattern is repeat with auto-fit and minmax. A rule such as grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)) creates as many columns as will fit, with each at least 16 rem wide and sharing the remaining space equally. The layout adjusts without any media query, which keeps the CSS short and predictable.

Fractional units divide the available space in proportion. A value of 2fr for the main column and 1fr for a sidebar keeps the ratio constant as the viewport changes. Combine fractions with fixed gaps, and remember that the gap is applied between tracks, not around the edges.

Use named areas for page structure

Named grid areas make the structure readable. Define a template such as a header across the top, a main area and a sidebar in the middle, and a footer at the bottom, then assign each element a grid-area name. On small screens, change the template to a single column so the sidebar moves below the main content, which is a simple and reliable pattern.

Keep the source order of your HTML logical even when the visual order changes. Screen readers and keyboard users follow the document order, so the content should still make sense if CSS is disabled.

Add breakpoints only when content needs them

Start with a mobile-first layout that uses a single column, then add a media query such as min-width: 48rem when the design can benefit from more space. Base breakpoints on the content rather than on specific devices, because new phones and tablets appear constantly. Test with long headings and real text, not placeholder words, to see where the layout breaks.

Finally, check the layout at 200 percent zoom and with a narrow window. Responsive grids should never produce horizontal scrolling for normal text, and they should keep focus outlines visible when users navigate by keyboard.

Frequently asked questions

Is CSS Grid supported in all modern browsers?
Yes. Grid is supported in current versions of all major browsers, so it is safe for mainstream production use. Check your analytics if you need to support very old browsers.
When should I use grid instead of flexbox?
Use grid when you need to control rows and columns together, such as a page layout or a card gallery. Use flexbox when you are arranging items along a single line or aligning content inside one component.
Why do my grid items overlap?
Usually the grid has fewer tracks than the items need, or an item has an explicit placement that conflicts with another. Inspect the grid in your browser's developer tools to see the track lines and placements.