CSS Breakpoints, Media Queries and Container Queries: A Practical Guide for Responsive Design

Responsive design used to mean one thing: write a few media queries for common screen widths and hope for the best. Today you have better tools. Media queries still handle page-level layout, container queries let individual components respond to the space they are given, and modern CSS functions reduce how many breakpoints you need at all. This guide explains how to combine them, with code you can adapt.

It assumes you know basic CSS. If you are building or auditing a layout, pair it with our responsive web design testing checklist.

Start with the viewport meta tag

None of your media queries will behave on phones without this tag in the document head. Without it, mobile browsers pretend the screen is about 980 pixels wide and shrink the page to fit.

<meta name="viewport" content="width=device-width, initial-scale=1">

Mobile-first: write the small layout first

Mobile-first means your base CSS describes the narrow layout and you add rules as the screen gets wider, using min-width queries. Desktop-first does the opposite with max-width, overriding the base styles to shrink things down. Mobile-first usually produces less CSS, because simple single-column layouts need few rules, and it lines up with how search engines crawl pages. Our guide to mobile-first indexing explains that side of it.

/* Base: small screens */
.layout { display: grid; gap: 1rem; }

/* Wider screens add columns */
@media (min-width: 48em) {
  .layout { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 64em) {
  .layout { grid-template-columns: 2fr 1fr; }
}

Use em units for breakpoints rather than pixels. They respect a user’s browser font size, so someone who has increased their default text gets the wider-screen layout at a more suitable point.

How to choose breakpoints

The old habit was to target devices: one query for iPhone, one for iPad, one for laptops. Device sizes change every year, so that list is always out of date. A better method is content-driven: start narrow, widen the browser slowly, and add a breakpoint where the layout starts to look wrong, such as lines getting too long or columns getting too cramped.

RangeTypical useCommon breakpoint
Small phones to large phonesSingle column, stacked navigationBase styles, no query
Large phones in landscape, small tabletsTwo-column cards, inline navigationabout 40em (640px)
Tablets and small laptopsSidebar layouts, three-column gridsabout 48em to 64em (768px to 1024px)
DesktopsMax-width containers, wider gridsabout 80em (1280px) and above

These are starting points, not rules. Three or four breakpoints are enough for most sites, and every extra one is more CSS to maintain and test. Set them as CSS custom properties in a comment or a shared file so the whole team uses the same values. Note that custom properties cannot be used inside media query conditions, so the values need to be repeated or generated by a preprocessor.

Layouts that need fewer breakpoints

Modern CSS can make many layouts adapt without any media query. This is often the cleanest solution.

/* A card grid that adds and removes columns by itself */
.cards {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}

/* Fluid type and spacing that scales smoothly */
h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); }
.section { padding-block: clamp(1.5rem, 4vw, 4rem); }

auto-fit with minmax() gives a grid that adds columns as space allows. clamp() sets a minimum, a flexible preferred value and a maximum in one declaration, so text and spacing scale smoothly instead of jumping at breakpoints. Flexbox with flex-wrap is another way to let items wrap naturally.

Container queries: components that respond to their own space

Media queries answer one question: how wide is the viewport? That falls short for reusable components. A card might sit in a wide main column on one page and a narrow sidebar on another, at exactly the same viewport width. Container queries let the card respond to the width of its own container instead.

.card-wrapper {
  container-type: inline-size;
  container-name: card;
}

.card { display: grid; gap: 0.75rem; }

@container card (min-width: 30em) {
  .card {
    grid-template-columns: 8rem 1fr;
    align-items: start;
  }
}

The wrapper declares itself as a container with container-type: inline-size. The query then applies styles to descendants based on the container’s inline width. Two rules to remember: an element cannot query itself, only a container above it, and a size container does not size itself from its children, so give it a width through the layout.

Container queries have been supported in all major current browsers since early 2023, so they are safe for most projects. If you must support very old browsers, provide a sensible single-column base style, because unsupported queries are simply ignored and the component still works, just without the wide variant.

Media queries or container queries?

Use caseBest tool
Page structure: header, sidebar, number of main columnsMedia queries
A card, widget or form that appears in many placesContainer queries
Switching navigation from a menu button to inline linksMedia queries
Respecting user settings such as reduced motion or dark modeMedia features (prefers-reduced-motion, prefers-color-scheme)
Touch versus mouse behaviourMedia features (hover, pointer)

They work together. A common pattern is to use media queries for the page shell and container queries inside components, so each component is portable.

Useful media features beyond width

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

@media (hover: hover) and (pointer: fine) {
  .button:hover { background: #e8f0ff; }
}

@media print {
  nav, .ads { display: none; }
}

Designing for the pointer type matters on phones: hover effects do not exist on touch screens, and small tap targets frustrate users. Aim for touch targets of at least about 44 to 48 CSS pixels.

Common mistakes and how to avoid them

  • Mixing min-width and max-width styles without a plan, which leads to conflicting rules. Pick mobile-first and stay consistent.
  • Overlapping ranges. A query for max-width 768px and another for min-width 768px both apply at exactly 768px. Use 767.98px or the modern range syntax.
  • Targeting specific devices. They change constantly; follow your content.
  • Fixed widths in pixels. Use max-width, percentages, rem and flexible grids instead.
  • Horizontal scrolling. Wide tables, code blocks and images often cause it. Give them max-width: 100% or an overflow wrapper.
  • Forgetting the viewport meta tag. Nothing responsive works without it.

The modern range syntax makes conditions easier to read:

@media (width >= 48em) { ... }
@media (40em <= width < 64em) { ... }

Range syntax is supported in current browsers, but if you need broad older support, keep the traditional min-width and max-width form.

How to test your breakpoints

  1. Resize the browser slowly through the whole range, not just at named device widths. Bugs hide between breakpoints.
  2. Use the device toolbar in Chrome or Firefox DevTools to simulate widths, pixel ratios and touch.
  3. Test on real phones and a tablet. Simulators do not reproduce real browser chrome, fonts or performance.
  4. Zoom text to 200% and check nothing overlaps.
  5. Test landscape as well as portrait.

A fast way to view your page at mobile, tablet and desktop widths directly on your phone is the free Dev Browser app, covered in the Dev Browser guide. Layout shifts caused by late-loading content are a performance issue too, which we cover in Core Web Vitals on mobile. More tools are on the products page.

Frequently asked questions

How many breakpoints do I need?

Usually two to four. Add one only when the content needs it, and prefer fluid techniques like grid auto-fit and clamp first.

Should I use px, em or rem in media queries?

Em or rem, so the layout respects user font settings. In media queries, both are based on the browser’s default font size, not on your CSS.

Can I use container queries with older browsers?

Unsupported browsers ignore them, so build a good base layout first and treat container queries as an enhancement.

Do media queries affect SEO?

Indirectly. A layout that works well on phones helps usability and mobile-first indexing, and avoiding layout shifts helps Core Web Vitals. The queries themselves are not a ranking factor.

Disclaimer: Browser support and CSS features evolve. Check current support tables (for example on MDN or Can I use) before relying on a feature in production, and test on real devices.

Leave a Reply

Share Your Love