Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk8 min

CSS Media Queries for Responsive Design: Examples and Best Practices

A practical guide to CSS media queries: build fluid layouts, choose breakpoints from content needs, adapt to user preferences, and decide when container queries fit better.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

CSS media queries let you apply styles only when the rendering environment meets conditions such as a viewport width, orientation, input capability, or user preference. For responsive layouts, start with flexible styles that work at narrow widths, then add a media query when the content needs a different arrangement. Choose that breakpoint from the layout—not from a named device.

How media queries work

A media query tests the environment in which a document is rendered. When its media type and feature conditions match, the CSS inside the query applies; otherwise, those rules do not apply. Queries can test screen width, orientation, input capabilities, and user preferences, among other features. See MDN’s CSS media queries guide.

As an Amazon Associate I earn from qualifying purchases.

The general form is:

@media media-type and (feature: value) {
  /* Conditional CSS rules */
}

The media type is optional. For example, screen limits a query to screen output, while print targets printed output. A query can combine a type and one or more feature expressions with and. It matches only when the type matches and every feature expression is true. Comma-separated queries are alternatives: if any one matches, the enclosed rules apply. Logical operators include not and only. For syntax details, see MDN’s guide to using media queries.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build a responsive layout around the content

Media queries complement flexible layout techniques; they do not replace them. Use fluid sizing, flexible grids, and minimum or maximum constraints for the base layout. Add a conditional change when the current arrangement becomes cramped or a wider arrangement would help.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Start with a narrow layout

Here is a complete example of an article-card grid. It begins with one column, then gains columns when the available viewport is wide enough for the cards to remain useful. The example’s breakpoint is illustrative, not a universal device threshold; adjust it after checking the actual content and layout.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Responsive article cards</title>
  <style>
    * { box-sizing: border-box; }

    body {
      margin: 0;
      font: 1rem/1.5 system-ui, sans-serif;
      color: #202124;
    }

    main {
      width: min(100% - 2rem, 72rem);
      margin-inline: auto;
      padding-block: 2rem;
    }

    .cards {
      display: grid;
      grid-template-columns: 1fr;
      gap: 1rem;
    }

    .card {
      padding: 1rem;
      border: 1px solid #c8c8c8;
      border-radius: 0.5rem;
    }

    @media (width >= 42rem) {
      .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    }

    @media (width >= 66rem) {
      .cards { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    }
  </style>
</head>
<body>
  <main>
    <h1>Latest articles</h1>
    <section class="cards" aria-label="Articles">
      <article class="card"><h2>First article</h2><p>A short description of the article.</p></article>
      <article class="card"><h2>Second article</h2><p>A short description of the article.</p></article>
      <article class="card"><h2>Third article</h2><p>A short description of the article.</p></article>
    </section>
  </main>
</body>
</html>

The viewport meta element in the example makes the page use the device’s layout viewport on mobile browsers; without it, a page may be laid out at a wider virtual width and the intended narrow-screen styles may not behave as expected. The grid’s base rule handles narrow layouts. The two media queries add columns only as the design gains room. The minmax(0, 1fr) tracks allow grid columns to shrink within the available space instead of being forced wider by their contents.

Choose breakpoints where the design needs them

There is no single correct breakpoint table for all sites. Resize the layout continuously and look for where text lines, controls, navigation, or cards stop fitting comfortably; use that point to introduce a change. Relative units such as rem are preferable for breakpoint values because they scale with the root font size. MDN’s responsive design guidance describes breakpoints as points where the design changes, rather than fixed labels for particular devices.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Keep the base layout usable before, between, and beyond your breakpoints.
  • Use fluid dimensions and flexible tracks so the layout does not depend on one exact width.
  • Add a breakpoint for a meaningful content or layout change, not simply because a device is called a phone, tablet, or desktop.
  • Check intermediate widths as well as the widths at which your media queries activate.

Use the condition that matches the design decision

Width is common for page-level responsive changes, but media queries can adapt to other environment characteristics. Use a feature query when the design genuinely depends on the tested capability or preference—not as a substitute for identifying a particular device.

Orientation

Use orientation when a layout benefits from the available shape of the viewport, rather than assuming every landscape screen is a particular device.

@media (orientation: landscape) {
  .reader-layout {
    /* Example: use available horizontal space for a sidebar. */
    grid-template-columns: minmax(0, 3fr) minmax(12rem, 1fr);
  }
}

Hover and pointer capability

hover and pointer can help condition enhancements on the primary input’s characteristics. They do not guarantee that every input available to a user has those characteristics, so keep essential actions usable without hover.

@media (hover: hover) and (pointer: fine) {
  .card a:hover {
    text-decoration-thickness: 0.15em;
  }
}

Reduced motion

Respect a reduced-motion preference by removing or shortening nonessential motion. The base style should already work; the query adjusts the experience for users who request less motion.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Color scheme and forced colors

A color-scheme query can provide a dark treatment when the user agent reports that preference. Test contrast for each theme. The forced-colors feature can be used where a design needs an adjustment for forced-color modes; avoid overriding system color choices without a clear accessibility reason.

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page-bg: #171717;
    --page-fg: #f3f3f3;
  }
}

@media (forced-colors: active) {
  .decorative-border {
    border-color: CanvasText;
  }
}

These feature categories, including orientation, hover, pointer, forced colors, color scheme, and reduced motion, are covered in MDN’s media query fundamentals and its media queries reference.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Print

A print query can remove screen-only interface elements or adjust colors and spacing for printed pages. Print is a media type, not a device breakpoint.

@media print {
  nav, .screen-only {
    display: none;
  }

  main {
    width: auto;
    margin: 0;
  }
}

Current media types include all, print, and screen. Older types such as tv and handheld are deprecated and should not be used as responsive categories. MDN also notes that a stylesheet linked with a nonmatching media query may still download at lower priority, although its rules do not apply unless the query matches.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use viewport queries for pages and container queries for components

A viewport media query is a natural choice when the whole page should change with the viewport or environment. But a reusable card, toolbar, or embedded panel may appear in containers of different widths on the same page. In that case, a container query can make the component respond to its own available space rather than the overall viewport.

.product-panel {
  container-type: inline-size;
}

.product-card {
  display: block;
}

@container (width >= 36rem) {
  .product-card {
    display: grid;
    grid-template-columns: 12rem minmax(0, 1fr);
    gap: 1rem;
  }
}

Set up a containment context on the element whose size should govern the component, then put the conditional component styles in @container. Use a media query when the condition is about the viewport or a user/environment feature; use a container query when the component’s containing space is the relevant constraint. MDN’s media queries guide distinguishes viewport media queries from container queries.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common mistakes and fixes

  • Choosing breakpoints by device folklore: A named-device width may not match the content’s needs, and devices can have different viewport sizes. Resize the actual layout and set a breakpoint where the arrangement needs to change.
  • Using only fixed widths: Fixed dimensions can overflow between breakpoints. Prefer flexible grid tracks, relative sizing, and min/max constraints, then add conditions for necessary changes.
  • Assuming a query detects a device model: Media features describe rendering conditions and capabilities, not a brand or exact model. Query the property that matters, such as width or hover capability.
  • Making hover the only way to reach an action: A hover query is not proof every user input can hover. Keep controls available to keyboard and touch users, too.
  • Expecting a nonmatching linked stylesheet never to download: A stylesheet with a nonmatching media condition can still download at lower priority; the condition governs whether its rules apply.
  • Using deprecated device dimensions: Prefer viewport features such as width, not device-width, device-height, or device-aspect-ratio. MDN identifies those device-specific features as deprecated in Media Queries Level 4.

Or skip the browser setup

If you need screenshots to check how a responsive page renders at different viewport settings, ScreenshotNeo offers a screenshot API and MCP server. One GET request can return an image or PDF; its API also accepts viewport and device preset options. For the complete parameter reference, see the ScreenshotNeo documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
  • Cookie/consent banners, newsletter popups, and chat widgets are removed before capture; each cleanup step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed. Responses identify the page verdict and billing status in headers.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients.
  • The Free plan includes 1,000 screenshots per month with no card required; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan.

Support and freshness notes

MDN’s linked documentation describes current guidance as viewed on October 3, 2026. Support for individual CSS features can vary by browser and may change; check the compatibility information for a feature before depending on it in production. MDN notes that the Media Queries Level 5 descriptors environment-blending, nav-controls, and video-color-gamut currently have no browser support, so they are not practical choices for a production stylesheet based on that status.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Does every responsive page need a media query?

No. A fluid layout can sometimes adapt across widths without conditional rules; add a media query only when a layout or style change is needed.

Can media queries detect a specific phone or tablet?

No. They test rendering conditions and capabilities, not an exact device model.

When should I use a container query instead?

Use one when a reusable component should respond to the size of its containing element rather than the page viewport.

Quick Recap

SaleBestseller No. 1
HTML and CSS: Design and Build Websites
HTML and CSS: Design and Build Websites
HTML CSS Design and Build Web Sites; Comes with secure packaging; It can be a gift option
$14.94
SaleBestseller No. 3
SaleBestseller No. 4
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.