Home › All Tools ›

CSS Media Query Generator

Use CSS Media Query Generator online for free at FreeToolZones — no sign-up, no watermark.

Loading tool...

The CSS Media Query Generator builds responsive breakpoints without syntax errors, turning a width, a query type, an orientation, a selector, and a list of declarations into a correctly formatted @media block. Responsive design depends on media queries: the same page must rearrange itself for a phone, a tablet, a laptop, and a desktop, and a single mistyped parenthesis or missing brace can disable a whole breakpoint. This tool assembles the block for you and validates the parts that most often go wrong.

Choose a preset breakpoint or enter a custom width, pick max-width or min-width, optionally restrict the query to portrait or landscape, enter the selector to style, and list the declarations one per line. The tool checks the width, the selector, and each declaration, skips lines without a colon, adds missing semicolons, and shows a plain-language summary of when the styles apply. Copy the result into your stylesheet and the breakpoint is ready.

What Is the CSS Media Query Generator?

The CSS Media Query Generator is a builder for CSS @media rules. A media query applies a set of styles only when the viewing conditions match, most commonly when the viewport width is below or above a breakpoint. The generated block starts with @media screen and, followed by the width condition and an optional orientation condition, then contains the selector and its declarations, indented for readability.

The breakpoint presets are Mobile at 480 pixels, Tablet at 768 pixels, Laptop at 1024 pixels, and Desktop at 1440 pixels, plus a Custom width option. The default preset is Tablet at 768 pixels. The width field accepts values from 1 to 5000 pixels, and selecting a preset fills the width field with that preset value. The query type dropdown offers max-width, meaning the styles apply on screens that width or smaller, and min-width, meaning the styles apply on screens that width or larger.

The orientation dropdown offers Any orientation, Portrait only, and Landscape only. The selector field defaults to .container, and the declarations field defaults to two lines: a flex-direction change to column, and a padding of 12 pixels. Those defaults describe a common responsive pattern: a row layout that stacks vertically on smaller screens, with adjusted padding. The output includes a summary sentence stating the width condition in plain language, and a warning if any declaration lines were skipped.

  • Five breakpoint options: four presets plus a custom width from 1 to 5000 pixels.
  • Two query types: max-width for smaller screens, min-width for larger screens.
  • Orientation control: any orientation, portrait only, or landscape only.
  • Selector and declarations: a selector field and a multi-line declarations field.
  • Validation and summary: error messages for missing inputs, skipped-line warnings, and a plain-language summary.

How to Use the CSS Media Query Generator

Decide the strategy first: are you writing styles for small screens and adding larger-screen rules, or starting large and overriding for small screens? The query type follows from that choice.

  1. Choose a breakpoint preset. Use the Breakpoint preset dropdown to select Mobile at 480px, Tablet at 768px, Laptop at 1024px, Desktop at 1440px, or Custom width. Selecting a preset fills the width field automatically.
  2. Enter a custom width if needed. If you chose Custom width, enter the breakpoint in the Width field, between 1 and 5000 pixels. Use a custom width when your design breaks at a size that does not match a preset, such as a navigation bar that overflows at 900 pixels.
  3. Choose the query type. Use the Query type dropdown to select max-width, for styles that apply up to the breakpoint, or min-width, for styles that apply from the breakpoint upward. Max-width suits desktop-first overrides, and min-width suits mobile-first enhancements.
  4. Choose an orientation. Use the Orientation dropdown to select Any orientation, Portrait only, or Landscape only. Orientation is optional and is most useful for tablets and phones, where the same width can mean very different layouts in each orientation.
  5. Enter the selector. Type the CSS selector in the CSS selector field, such as .container, .card-grid, or header nav. The selector must not be empty, or the tool will show an error asking for a selector.
  6. Enter the declarations. Type one declaration per line in the Declarations field, in property: value form, such as font-size: 18px;. Lines without a colon are skipped and reported. A missing semicolon at the end of a line is added automatically.
  7. Generate and copy. Click Generate Media Query to build the block, read the summary sentence to confirm the condition, and click Copy CSS to copy the @media block into your stylesheet.

Key Features of the CSS Media Query Generator

The tool combines sensible presets with validation, so the most common media query mistakes are caught before the code reaches a stylesheet.

  • Common breakpoints as presets: the four presets cover the device classes most designs target, without requiring memorised numbers.
  • Custom width support: any width from 1 to 5000 pixels can be used, supporting content-based breakpoints that match the design rather than device categories.
  • Colon validation: declaration lines without a colon are skipped and counted in a warning, so a typo such as a missing colon does not silently produce broken CSS.
  • Automatic semicolons: a declaration missing its trailing semicolon is corrected automatically, removing a frequent source of errors.
  • Plain-language summary: a sentence beneath the output states when the styles apply, including the orientation when one is set, making the condition easy to verify.
  • Error messages: missing widths, missing selectors, and empty declaration lists each produce a specific error message rather than an empty or broken block.

Practical Use Cases

Media queries are the mechanism behind responsive design. The examples below show the breakpoints that appear in almost every responsive stylesheet.

Stacking a row layout on mobile

A row of cards or a two-column section that works on desktop often needs to stack vertically on a phone. A max-width query at 768 pixels, targeting the container, with a flex-direction of column, achieves that stacking. The default declarations in this tool describe exactly this pattern.

Adjusting typography for small screens

Headings sized for a desktop can overwhelm a phone screen. A max-width query at 480 pixels that reduces font sizes, line heights, and padding keeps headings proportionate without changing the markup. Generate one query per selector that needs adjustment.

Adding columns on larger screens

In a mobile-first stylesheet, the base layout is a single column, and min-width queries add columns as the screen grows. A min-width query at 768 pixels might switch a grid to two columns, and another at 1024 pixels to three. Generate each breakpoint separately and stack them in order.

Orientation-specific layouts

A tablet in landscape orientation has room for a sidebar, while the same tablet in portrait does not. An orientation condition combined with a width condition lets you target exactly that situation, such as a min-width query with landscape orientation that shows the sidebar.

Hiding and showing elements

Some elements, such as a large decorative image or a secondary navigation, may be hidden on small screens to save space and loading time. A max-width query that sets display to none for that selector removes it cleanly below the breakpoint, and it reappears automatically above it.

Tips and Best Practices

Good responsive design starts from the content, not from device names. Breakpoints should follow the points where the layout actually breaks.

  • Choose breakpoints from the design: resize the browser until the layout looks wrong, note the width, and use that as a custom breakpoint. Device presets are starting points, not rules.
  • Prefer min-width for mobile-first design: writing the mobile layout as the base and adding min-width enhancements keeps the stylesheet organised and the mobile experience fast.
  • Keep queries close to their rules: placing a media query near the rule it overrides, rather than in a separate section at the end of the file, makes the stylesheet easier to maintain.
  • Test at the exact breakpoint: check the layout at one pixel below, exactly at, and one pixel above the breakpoint, because off-by-one errors are common at boundaries.
  • Avoid too many breakpoints: two or three well-chosen breakpoints usually suffice. A breakpoint for every device size creates a stylesheet that is hard to reason about.
  • Read the summary sentence: before copying, confirm that the summary matches your intention. A max-width query when you meant min-width inverts the entire behaviour.

Limitations

This generator builds one media query block for one selector at a time. It does not support multiple selectors in one block, nested media queries, range syntax with both minimum and maximum widths, media features other than width and orientation, such as resolution or hover capability, or the prefers-reduced-motion and prefers-color-scheme features. Each breakpoint and selector combination must be generated separately and combined by hand.

The validation is limited to checking the width, the selector, and the presence of a colon in each declaration. It does not validate property names, value syntax, or selector correctness, so a declaration with a mistyped property will still be included. The generated condition always uses the screen media type, and the declarations are reproduced as typed apart from the added semicolons.

  • One selector per block; no multiple or nested queries.
  • Width and orientation only; no other media features.
  • Colon check only; property names and values are not validated.
  • Always uses the screen media type.

Frequently Asked Questions

What is the difference between max-width and min-width?

Max-width applies the styles on screens up to and including the breakpoint width, so it targets smaller screens. Min-width applies the styles on screens at and above the breakpoint width, so it targets larger screens. The summary sentence beneath the output states which behaviour the generated block has.

Why was one of my declaration lines skipped?

Lines without a colon are skipped, because a valid declaration needs the property: value form. The tool reports how many lines were skipped. Check the skipped line for a missing colon, a typo, or a comment, and re-enter it in the correct form.

Do I need to add semicolons to every line?

No. If a line ends without a semicolon, the tool adds one automatically. Adding semicolons yourself is still good practice, because it makes the input clearer, but the generator will correct the omission either way.

Can I generate a query for a range, such as 768 to 1024 pixels?

Not in one block with this tool, which generates a single width condition. To target a range, generate the block and edit the condition by hand to include both a minimum and a maximum width, following the same @media syntax.

What happens if I leave the selector empty?

The tool shows an error message asking for a CSS selector, with .container as an example, and does not produce a result. Enter a selector and click Generate Media Query again. The same applies to an invalid width or an empty declaration list.

Mobile-First and Desktop-First: Choosing a Direction

The choice between max-width and min-width is really a choice about which layout is the base and which is the exception. In a mobile-first stylesheet, the base rules describe the small-screen layout, and min-width queries add complexity as the screen grows: a second column, a visible sidebar, larger type. In a desktop-first stylesheet, the base rules describe the large-screen layout, and max-width queries remove or rearrange elements as the screen shrinks. Both approaches produce the same final result when done well, but they feel different to write and maintain.

Mobile-first has practical advantages that explain its popularity. The base stylesheet stays small, which helps the devices with the least power and the slowest connections, and each breakpoint adds only what the larger screen needs. It also forces a useful discipline: deciding what matters most, because the small screen cannot hold everything, and then adding secondary content as space allows. Desktop-first can feel more natural when redesigning an existing desktop site, because the current design becomes the base and the queries describe the adaptations, but it risks leaving mobile users with a stylesheet full of desktop rules that are then overridden.

Whichever direction you choose, consistency within a project matters more than the choice itself. Mixing max-width and min-width queries freely in one stylesheet makes it hard to predict which rule wins at a given width, because the cascade order and the overlapping conditions interact in subtle ways. Pick one direction as the project convention, document it for anyone else who will edit the stylesheet, and use the CSS Media Query Generator with that query type as the default. The occasional exception, such as an orientation query, should be commented clearly so it is not mistaken for the convention.

Related Tools

These tools cover the layouts and units that media queries usually adjust.

Browse More Tools