Media Query Generator

Create CSS media queries for responsive web design with common breakpoints and features.

Query Builder

Mobile First (sm)
Tablet (md)
Desktop (lg)
Large Desktop (xl)
Dark Mode
Light Mode
Reduced Motion
Print

Generated Query

@media screen and (min-width: 768px) {
  /* styles here */
}

Device Reference

Mobile S320px
Mobile M375px
Mobile L425px
Tablet768px
Laptop1024px
Laptop L1440px
4K2560px

Tailwind Breakpoints

sm: 640px
md: 768px
lg: 1024px
xl: 1280px
2xl: 1536px

What 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:

  1. Start with the media type keyword: @media screen, @media print, etc.
  2. Append a and (feature: value) clause for most features.
  3. For orientation, the value is a keyword (portrait or landscape) rather than a number.
  4. For color, no value is appended — the bare feature name acts as a boolean test.
  5. 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

@media {mediaType} and ({feature}: {value}{unit}) { /* styles here */ }

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
allEvery device — the default when no type is specified
screenDisplays with a screen (phones, tablets, monitors)
printPrinters and print-preview mode
speechScreen readers and speech synthesisers

The generator also covers the most useful media features:

Feature Group Features Value Type
Widthmin-width, max-width, widthNumber + unit (px/em/rem/vw/vh)
Heightmin-height, max-height, heightNumber + unit
Aspect Ratioaspect-ratio, min-aspect-ratio, max-aspect-ratioNumber + unit
Orientationorientationportrait or landscape (keyword)
ResolutionresolutionNumber + unit (commonly dpi)
ColorcolorBoolean (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 S320 px@media screen and (min-width: 320px)
Mobile M375 px@media screen and (min-width: 375px)
Mobile L425 px@media screen and (min-width: 425px)
Tablet768 px@media screen and (min-width: 768px)
Laptop1024 px@media screen and (min-width: 1024px)
Laptop L1440 px@media screen and (min-width: 1440px)
4K / Ultra-wide2560 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.

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:

  1. 1Select media type: screen.
  2. 2Select feature: min-width.
  3. 3Enter value: 768, unit: px.
  4. 4The generator produces: @media screen and (min-width: 768px) { /* styles here */ }
  5. 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:

  1. 1Select media type: all (preference queries apply to every device, not just screens).
  2. 2Choose prefers-color-scheme from the common queries panel (not a dimension feature, so the value field is irrelevant).
  3. 3The generated query is: @media (prefers-color-scheme: dark) { /* styles here */ }
  4. 4Inside the rule, override CSS custom properties: --bg: #0f0f0f; --text: var(--card-bg);
  5. 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:

  1. 1Select media type: screen.
  2. 2Select feature: orientation.
  3. 3The value dropdown offers portrait or landscape — choose landscape.
  4. 4The generator produces: @media screen and (orientation: landscape) { /* styles here */ }
  5. 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:

  1. 1Select media type: screen.
  2. 2Select feature: resolution.
  3. 3Enter value: 192, unit: dpi (192 dpi = 2x device pixel ratio).
  4. 4The generator produces: @media screen and (resolution: 192dpi) { /* styles here */ }
  5. 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

A <strong>min-width</strong> query matches when the viewport is <em>at least</em> the specified width, making it the tool for mobile-first, progressive enhancement — you write base styles, then override them as the viewport grows. A <strong>max-width</strong> query matches when the viewport is <em>no wider than</em> the value, making it the choice for desktop-first design where you start wide and scale down. Most modern projects favour min-width because it encourages leaner base CSS and aligns with how browsers render pages on small devices first.
Both are valid but behave differently. <strong>px</strong> breakpoints are absolute and unaffected by the user's browser font-size setting, which makes them predictable but less accessible. <strong>em</strong> breakpoints scale with the root font size, so if a user increases their browser's default font size, the layout switches to a wider-content mode sooner — this is often the more accessible choice. A common rule of thumb is to divide your desired px breakpoint by 16 (the browser default) to get the em equivalent; for example, 768 px ÷ 16 = 48 em.
You can chain conditions with the <code>and</code> keyword. For example, <code>@media screen and (min-width: 768px) and (max-width: 1024px)</code> targets only tablets between those two widths. CSS Media Queries Level 4 also introduced a range syntax — <code>@media (768px <= width <= 1024px)</code> — which is more readable. The <code>not</code> and <code>only</code> keywords provide further control, and comma-separated query lists act like an OR operator.
Yes, because CSS applies rules in source order when specificity is equal. In a mobile-first approach you write base styles first and append min-width queries from smallest to largest. In a desktop-first approach you write wide styles first and append max-width queries from largest to smallest. Mixing orders without careful specificity management can cause unexpected overrides and is a common source of layout bugs.
The <code>prefers-reduced-motion: reduce</code> query matches when the user has turned on the 'reduce motion' accessibility setting in their operating system. Developers use it to disable or slow auto-playing animations, parallax scrolling, fade transitions, and other motion effects that can cause discomfort or trigger vestibular disorders. Respecting this preference is considered an important part of accessible, inclusive web design and is now a common requirement in accessibility audits.
CSS custom properties (variables) cannot be redefined inside a media query at the :root level in the traditional sense because the cascade resolves variables before media queries; however, you can place the :root variable override inside a media query block and it will work as expected because the media query controls whether that rule is active. This is the standard pattern for theme switching: define light-mode variables as defaults on :root, then override them inside @media (prefers-color-scheme: dark).

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.

Source

Formula Source: Standard Mathematical References

by Various

UpdatedLast reviewed: May 2026
CheckedFormula checks are based on standard references and internal QA review.