For example: prefers-color-scheme: dark, or hover: hover. Parentheses are added for you if you leave them out.
Live preview
Pick breakpoints from what your layout needs, not from a device list: a breakpoint belongs where your design breaks, and no device has one fixed width — the same phone in landscape and portrait is two different widths. Resize the browser window and watch the preview: the box turns crimson only while the query matches your current viewport.
What is the CSS Media Query Generator?
The CSS Media Query Generator is a free online tool that builds a CSS media query for you — the @media rule that applies styles only when the visitor’s screen meets certain conditions, such as a minimum width or a dark colour scheme. You set the widths, orientation and resolution, add a custom condition if you need one, and the tool writes the valid CSS. A live preview then tells you whether your query matches your current viewport, with a demo box that turns crimson while it matches. It is made for anyone building a responsive site: beginners writing their first breakpoint, bloggers fixing a layout that breaks on phones, and developers who want a correct media query without checking the syntax.
A media query is a condition wrapped around a block of CSS. The browser checks the condition — for example, “is the viewport at least 768 pixels wide?” — and applies the styles inside only when it is true. That is how one page can look different on a phone and on a desktop. Writing the condition by hand is easy to get wrong: a missing parenthesis or a backwards range produces a rule that never matches. This tool builds the condition from your settings, so the output is always valid, and the preview shows you immediately whether it matches.
Everything runs in your browser. There is no sign-up, no account, and nothing you type leaves your device.
How to use the CSS Media Query Generator
- Pick a device preset. The Device preset dropdown offers Custom, Mobile (up to 767px), Tablet (768–1023px), Laptop (1024–1439px) and Desktop (1440px and up). Tablet is selected by default. Choosing a preset fills in the minimum and maximum widths for you.
- Set the widths. Minimum width (px) and Maximum width (px) define the range. Leave one blank to set only the other side: a minimum of 1440 with no maximum means “1440px and up”, while a maximum of 767 with no minimum means “up to 767px”. Typing a width by hand switches the preset back to Custom.
- Add orientation and resolution. Orientation can be Any, Portrait or Landscape. Resolution can be Any, 1.5x and up, 2x and up (Retina) or 3x and up — the tool writes it as a
min-resolutionvalue in dppx (dots per pixel). - Add a custom condition (optional). The Custom condition field takes any extra condition, for example
prefers-color-scheme: darkorhover: hover. You do not need to type the parentheses — the tool adds them for you if you leave them out. - Check the live preview. The preview shows a match line and a demo box. When your query matches your current viewport, the line says so and the box turns crimson. Resize the browser window and the preview re-checks on every resize, so you can watch the match switch on and off as the window crosses your breakpoint.
- Copy the CSS. Press Copy CSS to take the generated rule, or Reset to return to the Tablet preset. If your minimum width is greater than your maximum, the tool warns you the query can never match.
Worked example: a tablet breakpoint
Leave the preset on Tablet (768–1023px). The tool generates this CSS:
@media (min-width: 768px) and (max-width: 1023px) {
.your-class {
/* Add your styles here */
}
}
Replace .your-class with your own selector and write your styles where the comment is. On a desktop window wider than 1023px, the preview says the query does not match; narrow the window into the 768–1023px range and the demo box turns crimson.
Now try a custom condition. Clear the widths, leave orientation and resolution on Any, and type prefers-color-scheme: dark in the Custom condition field. The tool generates:
@media (prefers-color-scheme: dark) {
.your-class {
/* Add your styles here */
}
}
The parentheses were added for you. If your browser or system is set to dark mode, the preview matches; otherwise it does not.
Responsive layouts this tool helps you build
Stack a two-column layout on phones. A layout with a sidebar next to the main content works on a laptop but squeezes on a phone. Use the Mobile preset — @media (max-width: 767px) — and write the stacking styles inside it: make the columns full width and let the sidebar drop below the content. The preview tells you exactly when the rule switches on as you narrow the window.
Give desktop visitors larger type and wider spacing. Small screens need compact type; large screens can afford more. Use the Desktop preset — @media (min-width: 1440px) — and increase the font size and the content width inside it. Because the query has no maximum width, it applies to every screen from 1440px upwards, however wide the monitor is.
Serve sharper images to high-resolution screens. Phones and modern laptops often have twice as many physical pixels as CSS pixels, so a normal image can look soft. Combine a width with the 2x resolution option and swap in a higher-resolution image inside that rule.
Tips for better media queries
- Put breakpoints where your design breaks. A breakpoint belongs at the width where your layout stops looking right, not at a device’s exact width. Narrow the window until something breaks, and set the breakpoint there — the same phone in landscape and portrait is two different widths.
- Watch out for a minimum above the maximum. If the minimum width is greater than the maximum width, the range is empty and the query can never match. The tool warns you when this happens. It is an easy mistake when typing numbers by hand, so check the status line before you copy the CSS.
- Use the preview to test, not just to admire. Resize the browser window while the tool is open and watch the demo box change colour as your breakpoint is crossed. If the box never turns crimson at the width you expect, the range is probably not what you think it is.
- Keep custom conditions simple. One extra condition, such as
prefers-color-scheme: darkorhover: hover, is usually enough. The tool joins multiple conditions withand, which means every condition must be true at the same time — add too many and the rule may never match in practice. - An empty query is not an error. If you clear every condition, the tool does not write a broken
@mediarule. It shows a comment telling you to add at least one condition, so the output stays valid while you are still thinking.
Frequently asked questions
What is a media query?
A media query is a condition in CSS that applies a block of styles only when the condition is true. The most common conditions are about the viewport width, but they can also cover orientation, resolution, colour scheme and whether the device supports hovering. Media queries are the main tool for responsive design — one page, different layouts for different screens.
Which widths should I use for my breakpoints?
Use the widths where your layout breaks, not a fixed list of device sizes. That said, the presets in this tool are a good starting point: up to 767px for phones, 768–1023px for tablets, 1024–1439px for laptops and 1440px and up for desktops.
What does dppx mean?
Dppx stands for dots per pixel — the number of physical screen pixels for each CSS pixel. A standard screen is 1dppx, while a Retina or high-density screen is 2dppx or 3dppx. The resolution option in this tool writes a min-resolution condition, so 2x and up becomes (min-resolution: 2dppx).
Can I combine width, orientation and a custom condition?
Yes. The tool joins every condition you set with and, so all of them must be true for the query to match. For example, a minimum width of 768px, portrait orientation and a dark colour scheme produces @media (min-width: 768px) and (orientation: portrait) and (prefers-color-scheme: dark). The preview checks the whole combined query against your viewport.
Why does my query not match when I expect it to?
First, check the status line — if the minimum is above the maximum, the query can never match. Second, check your actual viewport width — scrollbars and open panels make it narrower than your screen. Third, remember that conditions joined with and all have to be true; if any one of them fails, the whole query fails. Resize the window slowly and watch the demo box to find where the match switches.
Conclusion
The CSS Media Query Generator turns responsive design into a quick, visible process: pick a range, see whether it matches, and copy a valid rule. Try the Tablet preset above, then check the result on a real page with the Responsive Preview Tester.

