CSS width Media Feature

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The width media feature tests the viewport width (or the page box for paged media). It is a range feature, so you also get min-width and max-width variants.

Use it for responsive layouts—stack on small screens, expand on large ones. It is not the same as the width property, which sizes one element.

Deprecated device-width

Do not use device-width / min-device-width / max-device-width. Query the viewport with width instead.

Note Prefer em / rem for breakpoints

Length features work with px, but em or rem often keep breakpoints in sync when users enlarge text.

CSS Syntax

syntax.css
@media (width | min-width | max-width: <length>) { … }

Modern range syntax also works where supported: @media (width >= 35rem).

Feature Values

FormDescriptionTry it
min-widthViewport is at least this wide (mobile-first grow)Try it
max-widthViewport is at most this wide (narrow screens)Try it
widthExact viewport width (rarely useful alone)Try it
RangeCombine min and max, or use width >= …Try it

How to Use width

Start mobile-first with base styles, then layer larger breakpoints with min-width.

  1. Write a single-column layout that works on a small screen.
  2. Add @media (min-width: …) to enhance for tablets and desktops.
  3. Use max-width when a tweak should apply only below a size.
  4. Prefer a few clear breakpoints over many tiny ones.
PieceRequired?Role
@mediaYesWraps the query
Length valueYesBreakpoint size (px, em, rem, …)
min- / max-UsualRange matching instead of exact width
Base stylesYesLook correct before any query matches

Live Preview

Resize this window. The sample uses real @media (max-width: 600px) and (min-width: 601px) rules. The note shows the current viewport width from JavaScript.

Viewport width

Coral under 601px. Blue at 601px and up.

Width breakpoints

Resize to switch styles

max-width: 600px min-width: 601px

Checking…

CSS Examples

Each snippet focuses on the width media feature. Use Try It Yourself for a live editor—resize the preview pane.

Example 1 — Mobile-First min-width

Start simple, then enhance when the viewport is wide enough.

CSS
p {
  background: #fecaca;
}

@media (min-width: 35rem) {
  p {
    background: #dbeafe;
  }
}
Try It Yourself

How It Works

  1. Set a base. Coral background is the small-screen default.
  2. Add min-width. The query matches from 35rem upward.
  3. Enhance. Blue background applies on larger viewports.
  4. Mobile-first. You only add rules as the screen grows.

Example 2 — max-width for Narrow Screens

Apply a tweak only when the viewport is small.

CSS
p {
  background: #dbeafe;
}

@media (max-width: 600px) {
  p {
    background: #fecaca;
  }
}
Try It Yourself

How It Works

  1. Default for larger screens. Blue is the starting look.
  2. Cap with max-width. The query is true at 600px and below.
  3. Override on small. Coral marks the narrow layout.
  4. Desktop-first option. Useful when you only need small-screen exceptions.

Example 3 — Width Range

Target a band of sizes with both min and max (or modern range syntax).

CSS
p {
  background: #e2e8f0;
}

@media (min-width: 400px) and (max-width: 700px) {
  p {
    background: #bbf7d0;
  }
}
Try It Yourself

How It Works

  1. Keep a default. Gray applies outside the target band.
  2. Join with and. Both min and max must match.
  3. Hit the middle. Green appears from 400px through 700px.
  4. Optional modern form. @media (400px <= width <= 700px) means the same where supported.

Edge Cases & Pitfalls

Viewport queries are easy to confuse with element sizing.

vs property

Media feature ≠ width property

@media (min-width: 600px) checks the window. width: 600px sizes a box.

exact

Exact width: is brittle

@media (width: 360px) only matches that exact size. Prefer min- / max- ranges.

device

Skip deprecated device queries

device-width reflects hardware, not layout. Use viewport width.

scrollbar

Scrollbar can affect the value

The viewport width measurement can include scrollbar space. Avoid razor-thin exact breakpoints.

a11y

Test with larger text

If you use px breakpoints, zoom text and check layout. em/rem often fare better.

Browser Support

width is a browser media feature. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

width

min-width, max-width, and width media features have been interoperable across browsers for years (Baseline since about July 2015). Foundational for responsive CSS.

99% All modern browsers
Google Chrome All
Full
Mozilla Firefox All
Full
Apple Safari All
Full
Microsoft Edge All
Full
Opera All
Full
Samsung Internet All
Full
Internet Explorer 9+
Full
@media (width) Universally supported

Bottom line: Safe for production. Prefer min-width / max-width ranges; avoid deprecated device-width.

Key Takeaways

  • Role: query viewport width for responsive breakpoints.
  • Forms: min-width, max-width, and exact width.
  • Pattern: mobile-first with min-width layers.
  • Tip: use em/rem; never device-width.

One line: width media queries adapt CSS to the viewport—size elements with the width property separately.

Frequently Asked Questions

It tests the viewport width (or the page box for paged media). Use it inside @media to change styles at breakpoints.
min-width matches when the viewport is at least that wide. max-width matches when it is at most that wide. They are the range prefixes of width.
The media feature queries the viewport for conditional CSS. The width property sets the width of one element.
Both work. em (or rem) often scales better when users enlarge text, so breakpoints stay aligned with readable type.
No. device-width is deprecated. Use the width media feature to query the viewport instead.
No. It is a CSS media feature evaluated by the browser, not a Node.js or server runtime feature.

Next: CSS Properties

Browse the CSS properties reference and tutorials.

CSS properties →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful