Media Query Generator
Create CSS media queries for responsive web design with common breakpoints and features.
Query Builder
Generated Query
@media screen and (min-width: 768px) {
/* styles here */
}Device Reference
320px375px425px768px1024px1440px2560pxTailwind Breakpoints
sm: 640pxmd: 768pxlg: 1024pxxl: 1280px2xl: 1536pxWhat Is a CSS Media Query?
A CSS media query is a rule that applies a block of styles only when certain conditions about the user's device or browser environment are true. Introduced in CSS2 and greatly expanded in CSS3, media queries are the cornerstone of responsive web design. They let you write a single stylesheet that gracefully adapts to phones, tablets, laptops, desktops, print previews, dark-mode preferences, and much more.
The basic anatomy of a media query is:
@media <media-type> and (<media-feature>: <value>) {
/* CSS rules */
}
The media type describes the broad category of output device. The four standard types are all (matches every device), screen (colour or monochrome screens), print (paged media and print-preview mode), and speech (screen readers). The optional media feature narrows the match further — width, height, orientation, resolution, colour capability, and user preferences like reduced motion or preferred colour scheme.
Media queries are evaluated live in the browser. When the viewport is resized, the browser re-evaluates each query and applies or removes the matching rule sets instantly. This live re-evaluation is what makes fluid, breakpoint-based layouts possible without any JavaScript. Understanding how to construct precise, well-targeted media queries is one of the most valuable skills in modern front-end development.
Media Query Syntax and Formula
The media query generator on this page builds queries using the following construction logic:
- Start with the media type keyword:
@media screen,@media print, etc. - Append a
and (feature: value)clause for most features. - For
orientation, the value is a keyword (portraitorlandscape) rather than a number. - For
color, no value is appended — the bare feature name acts as a boolean test. - Close the rule set with curly braces containing a placeholder comment.
The generator supports five CSS length units — px, em, rem, vw, and vh — so you can write both absolute and relative breakpoints. Relative units like em are popular because they respect the user's browser font-size setting, making breakpoints more accessible.
Generated Query Structure
Where:
- mediaType= Output device type: all | screen | print | speech
- feature= Media feature to test, e.g. min-width, max-height, orientation, resolution
- value= Numeric value (for dimension/resolution features) or keyword (portrait/landscape for orientation)
- unit= CSS length unit appended to numeric values: px | em | rem | vw | vh (omitted for orientation and color)
Media Types and Supported Features
Choosing the right media type and media feature combination is essential for correct targeting. Here is a reference of what the generator supports:
| Media Type | Targets |
|---|---|
| all | Every device — the default when no type is specified |
| screen | Displays with a screen (phones, tablets, monitors) |
| Printers and print-preview mode | |
| speech | Screen readers and speech synthesisers |
The generator also covers the most useful media features:
| Feature Group | Features | Value Type |
|---|---|---|
| Width | min-width, max-width, width | Number + unit (px/em/rem/vw/vh) |
| Height | min-height, max-height, height | Number + unit |
| Aspect Ratio | aspect-ratio, min-aspect-ratio, max-aspect-ratio | Number + unit |
| Orientation | orientation | portrait or landscape (keyword) |
| Resolution | resolution | Number + unit (commonly dpi) |
| Color | color | Boolean (no value needed) |
Standard Breakpoints and Responsive Design Systems
While CSS media queries can target any pixel value, the industry has converged on a handful of standard breakpoints that align with common device categories. The quick-breakpoint buttons on this media query generator reflect the most widely used reference points:
| Device Category | Width | Typical Breakpoint Query |
|---|---|---|
| Mobile S | 320 px | @media screen and (min-width: 320px) |
| Mobile M | 375 px | @media screen and (min-width: 375px) |
| Mobile L | 425 px | @media screen and (min-width: 425px) |
| Tablet | 768 px | @media screen and (min-width: 768px) |
| Laptop | 1024 px | @media screen and (min-width: 1024px) |
| Laptop L | 1440 px | @media screen and (min-width: 1440px) |
| 4K / Ultra-wide | 2560 px | @media screen and (min-width: 2560px) |
Popular CSS frameworks each define their own breakpoint system. Tailwind CSS uses sm (640 px), md (768 px), lg (1024 px), xl (1280 px), and 2xl (1536 px). Bootstrap 5 uses xs (< 576 px), sm (>= 576 px), md (>= 768 px), lg (>= 992 px), xl (>= 1200 px), and xxl (>= 1400 px). Understanding both sets of breakpoints allows you to write media queries that integrate seamlessly with whichever utility or component framework your project uses.
A mobile-first approach is broadly recommended: write your base styles for the smallest screen, then use min-width queries to progressively enhance the layout for larger viewports. This strategy tends to produce leaner CSS because mobile styles serve as the default rather than being overridden later.
User Preference and Accessibility Media Queries
Beyond viewport dimensions, modern CSS media queries give you access to the user's system-level preferences. These queries are particularly important for accessibility and user experience. The prefers-color-scheme feature detects whether the operating system is set to dark or light mode, enabling your site to switch colour palettes automatically without requiring a manual toggle.
The prefers-reduced-motion feature reports whether the user has requested that the system reduce non-essential animation. Applying this query responsibly — disabling or slowing auto-playing carousels, parallax effects, and transitions — is a meaningful accessibility improvement for users with vestibular disorders or motion sensitivity.
Other available preference queries include prefers-contrast (high or low contrast modes), forced-colors (Windows High Contrast mode), and prefers-reduced-data (for users on metered connections). Building these queries into your stylesheet shows respect for the user's explicit system choices and is increasingly expected as part of inclusive design practice.
The common queries panel in this generator includes one-click copies for dark mode, light mode, reduced motion, and print — the four preference queries most commonly needed in production projects. Each query is provided as a ready-to-paste code block so you can drop it directly into your stylesheet and add your overrides inside.
Print and Speech Media Queries
Responsive design is not limited to screen sizes. Print media queries let you tailor a page for physical output — hiding navigation bars, expanding collapsed content, converting colour backgrounds to white, and ensuring that all URLs are printed as visible text next to their anchor elements. A well-crafted print stylesheet significantly improves the experience of users who print articles, invoices, and reports from your site.
The standard pattern is to write all print rules inside @media print { }. Common rules include display: none for headers, footers, and sidebars; page-break-inside: avoid for tables and figures; and color: black; background: white to conserve ink. Print media queries are easy to overlook but are quick to add and are genuinely appreciated by users who regularly print web content.
Speech media queries target text-to-speech readers. While few properties are speech-specific — the speak property was removed from the CSS spec — using semantic HTML in combination with @media speech can still help control how content is announced. Most accessibility work for screen readers is better handled through ARIA attributes and semantic markup, but knowing the query exists is useful when you need fine-grained control in edge cases.
Worked Examples
Tablet-and-Up Layout Breakpoint
Problem:
Apply a two-column grid layout for screens 768 px wide and above, while the mobile default stays single-column.
Solution Steps:
- 1Select media type: screen.
- 2Select feature: min-width.
- 3Enter value: 768, unit: px.
- 4The generator produces: @media screen and (min-width: 768px) { /* styles here */ }
- 5Inside the rule set, add grid-template-columns: 1fr 1fr; to switch the layout to two columns.
Result:
@media screen and (min-width: 768px) { .container { grid-template-columns: 1fr 1fr; } }
Dark Mode Colour Scheme Override
Problem:
Automatically switch the site to a dark background and light text when the user's OS is set to dark mode.
Solution Steps:
- 1Select media type: all (preference queries apply to every device, not just screens).
- 2Choose prefers-color-scheme from the common queries panel (not a dimension feature, so the value field is irrelevant).
- 3The generated query is: @media (prefers-color-scheme: dark) { /* styles here */ }
- 4Inside the rule, override CSS custom properties: --bg: #0f0f0f; --text: var(--card-bg);
- 5All elements that reference those variables will update automatically.
Result:
@media (prefers-color-scheme: dark) { :root { --bg: #0f0f0f; --text: var(--card-bg); } }
Landscape Orientation for Mobile Video
Problem:
Expand a video player to full width only when the device is in landscape orientation, regardless of exact pixel width.
Solution Steps:
- 1Select media type: screen.
- 2Select feature: orientation.
- 3The value dropdown offers portrait or landscape — choose landscape.
- 4The generator produces: @media screen and (orientation: landscape) { /* styles here */ }
- 5Add width: 100%; max-height: 100vh; to the video element inside the rule set.
Result:
@media screen and (orientation: landscape) { .video-player { width: 100%; max-height: 100vh; } }
High-Resolution Retina Display Assets
Problem:
Serve a 2x resolution image only on high-DPI screens to avoid blurry assets on retina displays.
Solution Steps:
- 1Select media type: screen.
- 2Select feature: resolution.
- 3Enter value: 192, unit: dpi (192 dpi = 2x device pixel ratio).
- 4The generator produces: @media screen and (resolution: 192dpi) { /* styles here */ }
- 5Inside the rule, set background-image: url('logo@2x.png') on the target element.
Result:
@media screen and (resolution: 192dpi) { .logo { background-image: url('logo@2x.png'); } }
Tips & Best Practices
- ✓Use min-width (mobile-first) queries as the default approach — they produce less CSS and are better for performance on mobile networks.
- ✓Test breakpoints by resizing the browser window rather than relying solely on device emulators, because real-world viewport sizes vary widely within each device category.
- ✓Keep your breakpoint count small — three to five breakpoints cover the vast majority of use cases and result in more maintainable stylesheets.
- ✓Always pair a prefers-reduced-motion query with any auto-playing animation to respect users who experience motion sickness.
- ✓Use em units for breakpoints to ensure your layout responds correctly when users have changed their browser's default font size.
- ✓Add a print media query to every project — hide navigation, sidebars, and ads, and ensure body text is black on white to save ink.
- ✓Group related media queries at the bottom of each component's CSS block rather than in a single monolithic breakpoints file — it keeps rules closer to the styles they modify.
- ✓Use the browser's DevTools responsive design mode to simulate specific devices, but always verify on real hardware for critical projects.
- ✓Combine the orientation query with min-width queries when targeting tablets specifically, since many tablets share width values with large phones.
- ✓Audit your media queries periodically — as browser support improves, you may be able to remove older hack queries or simplify complex conditions.
Frequently Asked Questions
Sources & References
Last updated: 2026-06-05
Help us improve!
How would you rate the Media Query Generator?
Editorial Note
MyCalcBuddy Editorial Team
This page is maintained as an educational calculator reference.
Formula Source: Standard Mathematical References
by Various