Developer and Text Tools

CSS Media Queries Without the Off-by-One Bugs

Mobile first or desktop first, px or em, the 767.98 trick, the new range syntax and container queries: how to write breakpoints that never overlap.

Saizul Amin
Saizul Amin5 Nov 2026 · 4 min read
CSS Media Queries Without the Off-by-One Bugs

Responsive design rests on media queries, and media queries look simple: at this width, apply these styles. The trouble starts at the edges. A rule for tablets that starts at 768 pixels and a rule for phones that ends at 768 pixels both apply at exactly 768, and the layout flickers between them. A rule that ends at 767 and one that starts at 768 leave a gap at fractional zoom widths, where neither applies. This guide explains the choices that prevent those bugs and how to generate consistent queries quickly.

Pick a direction

Mobile first means your base styles are for the smallest screens, and each media query uses min-width to add layout as the screen grows. It produces less CSS for phones and matches how most frameworks work today.

Desktop first means the base styles are for large screens and each query uses max-width to adjust for smaller ones. It is common in older sites and still practical when a desktop layout needs a few phone fixes.

Ranges close both ends: a style applies from one breakpoint up to just below the next. Use them for styles that belong to one band only, such as a two-column grid on tablets.

Avoid gaps and overlaps

With classic syntax, a max-width rule should stop just below the next min-width breakpoint. Bootstrap uses 0.02 pixels: max-width: 767.98px pairs with min-width: 768px. Browsers can report fractional widths when the page is zoomed, and .02 closes the gap without an overlap.

The newer range syntax from Media Queries Level 4 removes the problem. You write (width >= 768px) or (768px <= width < 992px), and the less-than sign means strictly below, so ranges meet exactly. All current versions of Chrome, Edge, Firefox and Safari support it. Use the classic form only if you must support browsers from before 2023.

Pixels or ems

Pixel queries are the easiest to read. Em queries follow the user's default font size: someone who sets their browser text larger reaches the narrower layout sooner, which usually suits them. At the default 16-pixel font size, 48em is 768 pixels. Both are valid; pick one and use it everywhere.

Container queries

Media queries measure the window. Container queries measure the element a component sits in. A card placed in a narrow sidebar and in a wide grid can lay itself out differently in each place with the same CSS. Give the parent container-type: inline-size, then write @container (min-width: 400px) rules for the children. Naming containers helps when containers are nested.

Generating a breakpoint set

The CSS Media Query Generator turns these choices into code.

  1. Choose a starting set. Bootstrap 5, Tailwind CSS, Material Design 3, Foundation, Bulma and Chakra UI are built in, plus a simple phone, tablet and desktop set. Edit names and widths freely, or add and remove breakpoints. Warnings flag duplicate names and breakpoints closer than 100 pixels.
  2. Choose the style. Mobile first, desktop first or ranges; classic or range syntax; px, em or rem; and an orientation if you need one.
  3. Add conditions. Tick features such as hover capability for mouse users, reduced motion, dark mode, more contrast, high-density screens or print, and they are added to every query. Or switch to container queries.
  4. Test widths. Drag the width slider to see which queries match. The device table lists common phones, tablets, laptops and desktops in CSS pixels, both portrait and landscape.
  5. Copy the code. The CSS tab writes every query with your selector. The SCSS tab writes a breakpoint map with up, down and between mixins. The JavaScript tab writes the queries for window.matchMedia with a change listener.

CSS pixels, not screen pixels

A modern phone may have a screen 1,179 pixels wide, but media queries see about 393, because the device pixel ratio is 3. All widths in media queries are CSS pixels, and the device table uses them too. That is why phone layouts usually target widths between 360 and 430 pixels.

Choosing breakpoints for real

Framework breakpoints are a fine start, but the best breakpoints come from your content. Narrow the browser slowly and note where the layout first looks wrong: a line of navigation that wraps, a table that overflows, a card grid with orphaned items. Add a breakpoint there. Fewer, well-placed breakpoints are easier to maintain than a dozen copied from a device list, and they keep working when the next phone size arrives.

Hover and pointer queries

Width does not tell you whether someone has a mouse. A large tablet has a desktop-sized screen and a finger. Use (hover: hover) and (pointer: fine) to add hover-only effects, such as menus that open on hover, and keep tap-friendly controls everywhere else. Combining width and capability queries gives layouts that fit both the screen and the way people use it.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages