JomMula

Frontend Development: HTML to React · CSS · Lesson 08

Grid and responsive design

Grid handles layouts with rows and columns at the same time. Pair it with media queries and fluid sizing, and your page works on a phone, a tablet, and a laptop.

8 min readCSSlayoutgridresponsiveUpdated October 2026

Flexbox is great at one dimension. Grid is for two: rows and columns at the same time.

If you’re building a page layout — a main area with a sidebar, a gallery of cards, a dashboard — Grid is usually the cleaner tool.

The basics

.layout {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  gap: 24px;
}
  • display: grid makes the element a grid container.
  • grid-template-columns defines the columns. 1.2fr and 0.8fr are fractions of the available space.
  • gap spaces the rows and columns.

You can also place items explicitly:

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

But most of the time you’ll let the grid place things automatically.

The magic line

This one line gives you a responsive card layout with no media queries:

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

Read it as: “Make as many columns as fit, each at least 220px wide, and share the leftover space equally.” When the container gets narrower, the number of columns drops. When it gets wider, it adds columns.

Responsive design

Responsive design means the page adapts to the screen instead of assuming a desktop. Three habits:

  1. Start small. Write the mobile layout first, then add complexity at larger widths.
  2. Use flexible units. %, fr, rem, clamp() — not a fixed width: 960px.
  3. Use media queries for real breakpoints, not device names.
.layout {
  display: grid;
  grid-template-columns: 1fr; /* mobile: one column */
  gap: 16px;
}

@media (min-width: 700px) {
  .layout {
    grid-template-columns: 1.2fr 0.8fr; /* desktop: two columns */
  }
}

The @media (min-width: 700px) block only applies when the viewport is at least 700px wide. That’s mobile-first: the base styles are the small-screen styles.

Fluid type

Instead of three font sizes for three breakpoints, let the browser interpolate:

h1 {
  font-size: clamp(1.8rem, 5vw, 3rem);
}

clamp(min, preferred, max) never goes below the minimum or above the maximum. It’s a small line that makes headings feel intentional on every screen.

Try it

Resize the preview (or open the page on your phone later). The cards reflow, and the two-column layout collapses to one.

Preview

Edit the code, then hit Run (or Ctrl + Enter) and watch the preview. This is a safe sandbox — you can't break the page.

Make the calculator responsive

the plan's verify step

Wrap the calculator form and the results section in a grid that is one column on mobile and two columns on screens wider than 700px. Then turn the three result stats into a grid with repeat(auto-fit, minmax(...)). Resize the browser and check that nothing overflows.

Test at 320px wide. If something scrolls sideways, find the element with a fixed width.

The mistakes everyone makes

  • Designing only for your laptop. Resize the browser. Use the DevTools device toolbar.
  • Fixed widths everywhere. width: 960px will break on a phone. Use max-width plus width: 100%.
  • Horizontal scrolling. Usually caused by a fixed width, a long unbroken word, or negative margins.
  • Too many breakpoints. Start with one or two. Add more only when the layout actually breaks.
  • Forgetting max-width: 100% on images. A large image will overflow its container.
  • Using vw for everything. It ignores scrollbars and can make text tiny. Use clamp().

Grid is for rows and columns; Flexbox is for one direction.

fr units share available space; gap spaces rows and columns.

repeat(auto-fit, minmax(220px, 1fr)) gives responsive cards for free.

Mobile-first: base styles for small screens, min-width queries for larger.

Use clamp() for fluid type and flexible units instead of fixed widths.

Next: color, typography, spacing, and design tokens — the difference between “it works” and “it looks like someone meant it.”

Previous: Flexbox: lay things out in a row
09

Up next

Color, type, spacing, and design tokens

Good-looking pages aren't about talent — they're about consistency. Learn a small color palette, a type scale, a spacing scale, and CSS variables to keep it all together.

Read now