Building Responsive Layouts with CSS Grid

By Amitesh Maurya | Category: CSS | Read: 8 min read

Creating websites that look great on every device is non-negotiable in today’s web development world. CSS Grid is the gold standard for building flexible, modern, and responsive layouts with ease. In this post, you’ll master foundational techniques for harnessing the power of CSS Grid to craft layouts that adapt smoothly to any screen size.

Why CSS Grid?

CSS Grid is a two-dimensional layout system, meaning it can handle both columns and rows—allowing precise control over complex layouts. Unlike older methods (like floats or inline-block), Grid lets you express design intent directly in your CSS, keeping your markup clean and structure logical.

CSS Grid Layout Example
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

Your HTML might look like:

<div class="container">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
  <div>Item 5</div>
</div>

Making it Responsive

Media queries enable layouts to respond to different screen sizes. However, with auto-fit and minmax, you can often make layouts responsive without extra media queries:

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

This setup elegantly adapts from one column on small phones to multiple columns on larger screens.

Aligning Content

CSS Grid offers powerful alignment tools:

.container {
  justify-items: center;   /* horizontal alignment */
  align-items: start;      /* vertical alignment */
}

Use place-items, justify-content, and align-content for advanced alignment of entire grid or grid items.

Nesting Grids

Complex layouts often need grids inside grids. Simply apply display: grid to a grid item:

<div class="container">
  <div class="item">
    <div class="nested">
      <div>Nested 1</div>
      <div>Nested 2</div>
    </div>
  </div>
</div>

.nested {
  display: grid;
  grid-template-columns: 1fr 2fr;
}

Tips for Modern Layouts

EXample: Responsive Card Grid

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 2rem;
}
.card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 0 12px rgba(0,0,0,0.1);
  padding: 1.5rem;
}