A layout that looks perfect on your laptop falls apart on a phone: columns squeeze into unreadable slivers, the sidebar lands on top of the article, buttons drift off the edge of the screen. Media queries are the CSS mechanism that fixes this. They let one stylesheet carry several sets of layout rules and apply each set only when the viewport matches its conditions.

Writing the queries by hand is fiddly work: nested brackets, conditions, units, and one transposed number that silently disables an entire block. This guide shows how to plan breakpoints and assemble clean, ready-to-paste blocks with the free Media Query Builder on Ovatly. One point to be clear about from the start: the builder assembles the @media blocks. The layout rules inside them are your own CSS, written for your own design.

Contents

  • Quick answer
  • What a media query actually does
  • Plan breakpoints where the layout breaks
  • Anatomy of a generated block
  • How to build your media queries, step by step
  • Worked example: small screens, then a tablet range
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

Decide the widths where your layout actually breaks, then build one @media block per range in the Media Query Builder: pick a device preset or type exact min and max widths, choose the media type, add an orientation condition if you need one, and copy the finished block into your stylesheet. Replace the placeholder comment inside each block with your layout rules for that range. Conditions combine with and, so a block applies only when every condition matches.

What a media query actually does

A media query is a condition attached to a group of CSS rules. When the condition is true, the rules inside apply; when it is false, the browser ignores the whole block. The conditions are usually about the viewport: its width and height in CSS pixels, and whether it is in portrait or landscape orientation.

Two details trip people up. First, widths are CSS pixels, not the physical pixels of the screen. A phone with a very sharp display still reports a modest CSS width, which is why it gets the small-screen layout. Second, the media type matters: screen targets screens, while all also includes print and other media. Responsive layout work almost always means screen.

Plan breakpoints where the layout breaks

There is no official table of phone, tablet and desktop widths. No standards body defines them, and the builder is honest about this: its presets are labeled as the conventions they are, gathered from common device sizes. A preset is a sensible place to start, not a rule to obey.

The more durable method is to let your own layout choose. Shrink the browser window slowly and watch where the design stops working: where the navigation wraps badly, where three columns become too narrow to read, where the sidebar crowds the text. That width is your breakpoint, because it is where your layout breaks, whatever device happens to have that width. Your analytics can refine the choice later, since they show the viewport sizes your visitors actually use.

Anatomy of a generated block

A finished block looks like this:

@media screen and (max-width: 767px) {
  /* your CSS here */
}

Every part is a decision the builder walks you through. The media type comes first. Then the conditions: min and max width, min and max height, each optional, since an empty field means no limit on that side. An orientation condition can be added on top. Conditions are joined with and, so all of them must be true at once for the block to apply. The placeholder comment marks where your rules go.

The builder also validates the obvious mistake: a minimum width larger than the maximum. A query like that can never match anything, and catching it at build time beats hunting for it in a stylesheet later.

How to build your media queries, step by step

  1. Open the Media Query Builder. Optionally choose a device preset to fill in typical widths as a starting point.
  2. Adjust the min and max width or height in pixels for this range. Leave a field empty when that side has no limit.
  3. Choose the media type, usually screen, and add an orientation condition if this range needs one.
  4. Copy the complete @media block and paste it into your stylesheet.
  5. Replace the placeholder comment with your layout rules for this range: column widths, stacking, spacing, font sizes.
  6. Repeat for each breakpoint range, then test by resizing a browser window across the full span and checking each range in turn.

Worked example: small screens, then a tablet range

Start with the range that needs the most help: small screens. In the builder, choose media type screen and set a maximum width of 767px, leaving the minimum empty. The output is the block shown above: every screen up to 767 CSS pixels wide gets the rules you write inside it, typically a single stacked column, full-width images and a collapsed navigation.

Now add a middle range. Set a minimum width of 768px, a maximum of 1023px, and the orientation condition to landscape. Because the conditions combine with and, the generated block applies only to viewports in that width band held sideways:

@media screen and (min-width: 768px) and (max-width: 1023px) and (orientation: landscape) {
  /* your CSS here */
}

Inside it you might restore two columns, since a landscape tablet has the horizontal room for them. Everything wider than 1023px falls through to your base styles, which carry the full desktop layout. Three layers, each generated cleanly, each filled with your own rules.

Limitations and honest tradeoffs

The builder produces query blocks, nothing more. It does not design your layout, write your CSS or test the result on real devices; that work, and the judgement behind it, stays with you.

Preset widths are conventions. Devices keep changing, and a breakpoint chosen from a preset table may sit awkwardly for your particular design. Treat presets as scaffolding, and move the numbers when your layout or your analytics say otherwise.

Finally, a query is only as good as its conditions. Stack enough and conditions together and a block can end up matching almost nothing, which is a quiet way to ship CSS that never runs.

Common mistakes

  • Copying device widths as if they were standards. They are conventions. Set breakpoints where your layout breaks.
  • Setting a minimum larger than the maximum. The query can never match. The builder flags it; do not override the warning.
  • Forgetting that every and condition must match. A width range plus landscape means portrait tablets get none of those rules, intended or not.
  • Writing layout rules outside the blocks. Rules outside any @media block apply at every width, including the phone you were trying to fix.
  • Testing on one device instead of resizing continuously. Dragging the browser window across the range exposes the awkward in-between widths that a single phone never shows.

FAQs

Are the device preset breakpoints official standards? No. No standards body defines phone or tablet widths. The presets are widely used conventions gathered from common device sizes, offered as a practical starting point. Your own analytics may justify different breakpoints.

What does leaving a min or max field empty mean? It means no limit on that side. A block with only a max width applies to every viewport up to that width, and a block with only a min width applies to everything wider than it.

What is the difference between the screen and all media types? 'all' includes print and other media. 'screen' limits the block to screens, which is what responsive layout rules almost always intend.

Why does my media query never match? Check for a minimum larger than the maximum, which the builder flags, and remember that widths are the viewport's CSS pixels, which differ from a device's physical resolution on high-density screens.

Can I combine a width range with orientation? Yes. Set a width range and pick portrait or landscape; both appear in the query joined by 'and', and the block applies only when both are true.

Conclusion

Responsive layout with media queries is two jobs: choosing honest breakpoints, and writing clean blocks that encode them. The Media Query Builder handles the second job, assembling validated, ready-to-paste blocks from presets or your exact numbers, while the first job and the layout rules themselves stay yours. When the blocks are in place, it is worth auditing what search engines and social platforms see in your page head, which our guide to fixing meta tags that are missing or duplicated walks through. For more guides in this series, see our Website and Webmaster Tools guides, which pair each technique with the free tool that performs it.

References