Free tool · Developer tools
CSS media query generator
This CSS media query generator writes width rules in px, em or rem. A rule for screens 768px and wider is written (width >= 768px), or (min-width: 768px) in the older form. Choose what the rule should match, type the widths and copy the CSS.
- Free
- No sign-up
- Runs in your browser
@media (width >= 768px)px units, any media type
- Mobile first
- Pixels
- Older syntax
- @media (min-width: 768px)
- First width in em
- 48em at a 16px base
@media (width >= 768px) {
/* your styles */
}
@media (min-width: 768px) {
/* your styles */
}| Width | In em | Mobile first | Desktop first |
|---|---|---|---|
| 480px | 30em | (width >= 480px) | (width < 480px) |
| 768px | 48em | (width >= 768px) | (width < 768px) |
| 1024px | 64em | (width >= 1024px) | (width < 1024px) |
| 1280px | 80em | (width >= 1280px) | (width < 1280px) |
How to use the CSS Media Query Generator
- Under What should the rule match? choose wider, narrower, below or between.
- Type the First width, and the Second width for the two between choices.
- Pick px, em or rem, and set the Base font size that converts them.
- Copy the CSS block. The table turns your list of breakpoints into queries.
How do you write a min-width media query?
A min-width query applies its rules from a width upward, which is the mobile-first pattern: base styles for small screens, then added rules as the screen grows. For 768px the query is (min-width: 768px). The current range form says the same thing as (width >= 768px), and both include the width itself, so a screen exactly 768px wide matches.
Inside the braces you put the normal rules. The page writes the block for you with a comment where your styles go, once in each syntax, so you can paste whichever one your codebase uses.
| Width | In em | Mobile first | Desktop first |
|---|---|---|---|
| 480px | 30em | (width >= 480px) | (width < 480px) |
| 768px | 48em | (width >= 768px) | (width < 768px) |
| 1024px | 64em | (width >= 1024px) | (width < 1024px) |
| 1280px | 80em | (width >= 1280px) | (width < 1280px) |
What is the difference between min-width, max-width and the range syntax?
The prefixes min- and max- set a lower or an upper limit, and both limits are inclusive. The range syntax uses the comparison signs >=, <=, > and <, so a strict "narrower than" can be written directly. MDN documents both forms, and the range form can also take two limits in one expression, such as a lower and an upper width.
Conditions combine with the word and: screen and (width >= 768px) and (orientation: portrait) matches only portrait screens at the width or wider. Several queries separated by commas act as or, so any one of them can match.
Why does the older "below" form end at 0.02px less?
Say the small layout should stop where the 768px layout starts. With max-width: 768px on the small layout, both rules match at exactly 768px. Writing it as max-width: 767px removes the overlap but leaves a gap, since a width of 767.5px, which zoom and high density screens can produce, would match neither rule. The older syntax has no "less than" sign, so a common fix is to stop 0.02px short. For 768px that gives (max-width: 767.98px). The range syntax needs no such step: (width < 768px) is already strict.
A band from 768px up to, not including, 1024px is (768px <= width < 1024px) in the range form and (min-width: 768px) and (max-width: 1023.98px) in the older form. Because each band ends just before the next begins, every width matches exactly one band.
How do px, em and rem work in a media query?
The page converts with one division: em = px ÷ base font size. At a base of 16px, 768px becomes 48em and the query reads (width >= 48em). Change the base font size field if you want the conversion for a different base, such as 18px, where 768px is 42.6667em. The rem output uses the same division. Inside a media query, em and rem are both measured against the browser's default font size, which is 16px unless the reader changes it, so a font size you set on the html element does not move your breakpoints.
Pick the unit your stylesheet already uses, so breakpoints and sizes follow the same logic. The table always shows the em value next to the px width to make a quick comparison easy.
Use it with
- CSS Grid Generator: build a CSS grid layout and copy the code.
- Px to Rem Converter: convert px, rem, em.
- Screen Resolution Checker: show screen size and viewport.
Questions
What is the media query for screens 768px and wider?
(width >= 768px), or (min-width: 768px) in the older syntax. Both include a width of exactly 768px.
How do I target a range such as 768px to 1023px?
Use (768px <= width < 1024px) for 768px up to, not including, 1024px. The older form is (min-width: 768px) and (max-width: 1023.98px).
Should I use px or em for breakpoints?
Either works. The page shows 768px as 48em at a 16px base, so you can compare them and keep whichever unit the rest of your CSS uses.
Can I combine a width with orientation?
Yes. Choose an orientation and the page adds it with and, for example (width >= 768px) and (orientation: portrait).
What does 767.98px mean?
It is 0.02px under 768px. The older syntax has no strict less-than, so ending a rule there keeps it from matching the same width as the 768px rule.
Does the page store my breakpoints?
No. Breakpoints you enter are read by the page itself and are not uploaded or kept.
Sources
- MDN: Using media queries, read
- MDN: @media, read
The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.