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: gridmakes the element a grid container.grid-template-columnsdefines the columns.1.2frand0.8frare fractions of the available space.gapspaces 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:
- Start small. Write the mobile layout first, then add complexity at larger widths.
- Use flexible units.
%,fr,rem,clamp()— not a fixedwidth: 960px. - 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.
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 stepWrap 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: 960pxwill break on a phone. Usemax-widthpluswidth: 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
vwfor everything. It ignores scrollbars and can make text tiny. Useclamp().
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.”
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.