CSS Grid Layout: Building Responsive Designs

CSS Grid is a two-dimensional layout system-rows and columns together-making it ideal for page layouts, dashboards, and card grids that Flexbox alone struggles with.

Defining a Grid

.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
  gap: 1rem;
}

.sidebar { grid-column: 1; grid-row: 2; }
.main    { grid-column: 2; grid-row: 2; }

Responsive Patterns

Use repeat(auto-fit, minmax(280px, 1fr)) for card layouts that reflow without media queries. Named grid areas improve readability:

.layout {
  display: grid;
  grid-template-areas:
    "header header"
    "nav    main"
    "footer footer";
}

Grid vs Flexbox

Use Grid for page-level structure; use Flexbox for aligning items inside a component (nav links, button groups). They complement each other.

Conclusion

Grid removed much of the hacky float-and-clearfix era. Invest time in fr units, minmax, and auto-fit-your responsive layouts will be simpler and more maintainable.