Skip to content

Media Query Generator

Build CSS media queries for any breakpoint, orientation, dark mode, print or reduced motion, with mobile-first or desktop-first output.

Runs in your browserFree, no sign-up

Settings

Results update automatically as you type.

CSS

/* sm and up */
@media (min-width: 640px) {
  .element {
    /* styles */
  }
}

/* md and up */
@media (min-width: 768px) {
  .element {
    /* styles */
  }
}

/* lg and up */
@media (min-width: 1024px) {
  .element {
    /* styles */
  }
}

/* xl and up */
@media (min-width: 1280px) {
  .element {
    /* styles */
  }
}

/* 2xl and up */
@media (min-width: 1536px) {
  .element {
    /* styles */
  }
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
  .element {
    /* dark styles */
  }
}

/* Respect users who ask for less motion */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* Print */
@media print {
  .no-print {
    display: none;
  }
}

Breakpoints in this set

NameWidthApplies from
sm640px640px or wider
md768px768px or wider
lg1024px1024px or wider
xl1280px1280px or wider
2xl1536px1536px or wider

How Media Query Generator works

Media queries apply CSS only when the browsing context matches a condition — usually a viewport width, but equally orientation, colour scheme preference, print output or a request for reduced motion. This generator writes them for you in either mobile-first or desktop-first style, using the Tailwind or Bootstrap breakpoint sets or a width of your own, and handles the fractional-pixel detail that keeps adjacent ranges from overlapping. It also emits the three non-width queries most sites should ship regardless of their breakpoint strategy: dark mode, reduced motion and print.

Why maximum widths end in .98

Viewport widths are not always whole numbers. Browser zoom, high-density displays and some desktop window sizes all produce fractional widths, so a viewport can genuinely report 767.5px. If one rule applies up to max-width: 767px and the next from min-width: 768px, that half-pixel viewport matches neither and the element falls back to its unstyled base — a bug that is maddening to reproduce because it only appears at specific zoom levels or on specific hardware. Ending the lower range just below the next breakpoint, at 767.98px, closes the gap while remaining safely below 768. This is exactly why Bootstrap's generated breakpoints use the .98 convention, and it is worth copying rather than rediscovering. The problem does not arise with mobile-first min-width queries, since overlapping ranges are harmless when later rules simply override earlier ones — which is one more reason mobile-first is the easier strategy to get right.

Preference queries deserve as much attention as widths

Width breakpoints get all the attention, but the queries that describe what a user has asked for often matter more to real people. prefers-reduced-motion is the clearest case: it reflects an operating-system setting that people with vestibular disorders enable deliberately, and honouring it is a genuine accessibility requirement rather than a nicety. The common implementation reduces every animation and transition to a near-zero duration inside that query, which preserves any state change the animation was communicating while removing the movement itself. prefers-color-scheme lets a site follow the system's light or dark setting without any toggle at all, and pairs well with CSS custom properties, where only the colour tokens need redefining rather than every rule. prefers-contrast and forced-colors cover users who have increased contrast or enabled a high-contrast mode. None of these has anything to do with screen size, and all of them are queries a site can add without touching its layout.

Using Media Query Generator

  1. 1

    Choose an approach — mobile-first min-width, desktop-first max-width, or a range between two widths.

  2. 2

    Pick a breakpoint set, or select Custom and enter your own widths.

  3. 3

    Optionally restrict the query to portrait or landscape, and choose px or em units.

  4. 4

    Copy the generated CSS and fill in the rules inside each block.

What people use it for

Setting up a new project's breakpoints

Starting from the Tailwind or Bootstrap set keeps your CSS aligned with whatever framework the project already uses.

Targeting a single size range

The range mode produces a query that applies between two widths only, for a rule that should not leak into larger screens.

Adding dark mode support

The prefers-color-scheme block respects the reader's system setting rather than requiring them to find a toggle.

Making a page printable

A print query hides navigation, footers and interactive controls so a printed page carries only the content.

Tips

  • Prefer mobile-first min-width queries: overlapping ranges are harmless, so the fractional-pixel problem never arises.
  • em-based breakpoints scale with the reader's default font size, which serves people who have increased it in their browser settings.
  • Container queries now handle 'this component is narrow' far better than viewport queries ever did — reach for them when the component, not the page, is what changed.

Answers to common questions

Should I write mobile-first or desktop-first media queries?
Mobile-first with min-width is the common default: the base styles serve the smallest screen and each query adds complexity as space allows, which keeps the mobile payload simple. Desktop-first with max-width mainly makes sense when retrofitting an older fixed-width layout.
Why use 767.98px instead of 767px as a maximum?
Viewport widths can be fractional on high-density screens, so a gap between max-width: 767px and min-width: 768px would leave 767.5px matching neither rule. Ending the range just below the next breakpoint closes that gap.
Should breakpoints be in px or em?
em breakpoints scale with the user's default font size, so someone who has increased their browser's base text size gets the simpler layout sooner — generally the more accessible choice. px is more predictable and remains very common.
Do I still need media queries with CSS Grid and Flexbox?
Far fewer. repeat(auto-fit, minmax(...)) and flex-wrap handle most reflow on their own. Media queries are still the right tool for changes those cannot express, such as switching navigation patterns or hiding a sidebar entirely.

Looking for something else? Browse all css tools or see every tool.