CSS width Media Feature

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Media Queries

What You’ll Learn

The width media feature targets the viewport’s horizontal size. Build responsive layouts for phones, tablets, and desktops with min-width and max-width queries.

01

@media

Media query.

02

min-width

Wide screens.

03

max-width

Narrow screens.

04

Viewport

Window width.

05

vs height

Horizontal axis.

06

vs property

Query vs size.

Introduction

Responsive web design starts with width. The width media feature lets you apply CSS when the browser window is wide or narrow — the foundation of mobile-first layouts, breakpoint systems, and adaptive grids.

A phone in portrait mode has a narrow viewport; a desktop monitor has a wide one. By querying viewport width with min-width and max-width, you control when columns stack, when navigation collapses, and when typography scales up.

Definition and Usage

Write @media (min-width: 600px) or @media (max-width: 768px) to apply styles when the viewport meets your width condition. Values use length units such as px, em, rem, or viewport units like vw.

💡
Beginner Tip

Do not confuse this with the CSS width property, which sizes an element. The media feature checks the browser window width, not an individual box.

📝 Syntax

Use min-width, max-width, or an exact width inside @media:

syntax.css
/* Viewport is at least 600px wide */
@media (min-width: 600px) {
  /* Styles for wide viewports */
}

/* Viewport is at most 768px wide */
@media (max-width: 768px) {
  /* Styles for narrow viewports */
}

/* Exact viewport width (rare) */
@media (width: 1024px) {
  /* Matches only at exactly 1024px width */
}

Feature Variants

  • min-width — Matches when viewport width is greater than or equal to the value (wide screens).
  • max-width — Matches when viewport width is less than or equal to the value (narrow screens).
  • width — Matches an exact viewport width; rarely used because few users hit one precise pixel width.

width Media Feature vs width Property

@media (min-width: 600px) Viewport query — “Is the browser window at least 600px wide?”
width: 600px; Element sizing — sets one box to exactly 600 pixels wide.

width vs height Media Features

@media (min-width: 48rem) Horizontal axis — phones vs tablets vs desktops by window width.
@media (min-height: 32rem) Vertical axis — short split windows vs tall full-screen viewports.

Basic Example

width-basic.css
body {
  font-size: 0.875rem;
}

@media (min-width: 600px) {
  body {
    font-size: 1rem;
  }
}

@media (max-width: 400px) {
  body {
    font-size: 0.8125rem;
  }
}

Default Value

There is no CSS default. You choose the threshold based on your layout needs. Prefer rem breakpoints for scalable, accessible designs.

Syntax Rules

  • Use length values: px, em, rem, vw.
  • Combine with height: @media (min-width: 48rem) and (min-height: 32rem).
  • max-width overrides min-width when both match — order matters in CSS cascade.
  • Resize the browser horizontally to test; DevTools device mode supports width emulation.
  • Mobile-first: start with base styles, then add min-width rules for larger screens.

Related Topics

@media (min-width: 600px) @media (max-width: 768px) @media (min-width: 600px) and (max-width: 1024px)

⚡ Quick Reference

QuestionAnswer
Feature nameswidth, min-width, max-width
What it measuresViewport (browser window) width
Wide screens@media (min-width: 768px)
Narrow screens@media (max-width: 600px)
Common unitspx, rem, em, vw
Tablet range@media (min-width: 600px) and (max-width: 1024px)
Browser support100% — all modern browsers

When to Use width

Reach for width-based media queries when horizontal space drives the design:

  • Mobile layouts — Stack columns and hide sidebars with max-width.
  • Desktop upgrades — Enable multi-column grids on wide screens with min-width.
  • Typography scaling — Increase font size when the viewport is wide enough to read comfortably.
  • Navigation patterns — Show hamburger menus on narrow viewports, full nav on wide ones.
  • Tablet-only styles — Combine min-width and max-width for a specific range.

👀 Live Preview

Resize your browser window horizontally. This block stays blue on wide viewports and turns coral below max-width: 600px:

Viewport Width Demo Wide viewport: blue

Examples Gallery

Practice the width media feature with font scaling, background changes, grid layouts, and tablet-only ranges.

📜 Core Patterns

Adapt styles when the viewport is wide or narrow.

Example 1 — Font size with min-width

Increase body font size on viewports at least 768px wide:

width-font.css
body {
  font-size: 0.875rem;
  line-height: 1.5;
}

@media (min-width: 768px) {
  body {
    font-size: 1.125rem;
  }
}
Try It Yourself

How It Works

Mobile users get compact 14px text; tablets and desktops get 18px for comfortable reading. This is a classic mobile-first typography pattern.

Example 2 — Background color with max-width

Apply a different background on narrow viewports:

width-background.css
body {
  background-color: #ffffff;
}

@media (max-width: 600px) {
  body {
    background-color: #f1f5f9;
  }
}
Try It Yourself

How It Works

max-width targets smaller screens. Wide desktops keep white; phones and narrow windows get a subtle gray tint.

Example 3 — Grid layout with min-width

Switch from a single column to a two-column grid on wide viewports:

width-grid.css
.container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (min-width: 768px) {
  .container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
  }
}
Try It Yourself

How It Works

Narrow viewports stack content vertically. At 768px and above, CSS Grid creates a side-by-side two-column layout.

Example 4 — Tablet range with min-width and max-width

Apply styles only when the viewport is between 600px and 1024px wide:

width-tablet.css
.sidebar {
  display: none;
}

@media (min-width: 600px) and (max-width: 1024px) {
  .sidebar {
    display: block;
    width: 200px;
    background: #f8fafc;
    padding: 1rem;
  }
}
Try It Yourself

How It Works

Combining min-width and max-width with and creates a bounded range. The sidebar appears only on tablet-sized viewports.

💬 Usage Tips

  • Mobile-first — Write base styles for narrow viewports, then add min-width upgrades.
  • Use rem breakpointsmin-width: 48rem scales with user font settings.
  • Test real devices — Emulators help, but physical phones reveal touch and scroll issues.
  • Avoid too many breakpoints — Three or four well-chosen widths beat a dozen arbitrary ones.
  • Pair with container queries — Width media features query the viewport; container queries target individual components.

⚠️ Common Pitfalls

  • Confusing with width property — Media feature checks viewport; property sizes elements.
  • Desktop-first max-width chains — Overlapping max-width rules are harder to maintain than mobile-first min-width.
  • Exact width matches(width: 1024px) rarely matches real users.
  • Ignoring cascade order — Later rules win when min and max both match.
  • Fixed px everywhere — Prefer rem breakpoints for accessibility.

♿ Accessibility

  • Zoom and text size — Large text can trigger layout shifts; test at 200% zoom.
  • Touch targets — Narrow viewports need adequately sized buttons and links.
  • Content reflow — Ensure text reflows without horizontal scrolling at 320px width.
  • Focus visibility — Collapsed navigation must keep visible focus outlines.
  • Reduced motion — Layout changes should respect prefers-reduced-motion.

🧠 How width Works

1

Browser measures viewport

The user agent reads the visible window width in CSS pixels.

Measure
2

Media queries evaluate

min-width and max-width conditions are tested against that value.

Evaluate
3

Matching rules apply

Styles inside true @media blocks override defaults.

Apply
=

Width-aware layout

UI adapts when users resize horizontally or switch devices.

🖥 Browser Compatibility

The width, min-width, and max-width media features are supported in all modern browsers with 100% global support.

Baseline · Universal support

Viewport width queries

Works in Chrome, Firefox, Safari, Edge, and Opera.

100% Global support
width media features 100% supported

Bottom line: Width media queries are the backbone of responsive CSS and are safe for production in every browser.

🎉 Conclusion

The width media feature lets you respond to viewport width with min-width, max-width, and exact width queries. It is the most essential tool for building responsive websites.

Use it for mobile layouts, desktop upgrades, typography scaling, and tablet-only ranges. Always distinguish viewport width queries from the CSS width property on elements.

💡 Best Practices

✅ Do

  • Start mobile-first with min-width
  • Use rem breakpoints
  • Test at 320px and 1920px
  • Combine min- and max-width for ranges
  • Keep breakpoints meaningful

❌ Don’t

  • Confuse with width property
  • Rely on exact width only
  • Add breakpoints for every device
  • Hide essential content on mobile
  • Forget zoom and accessibility testing

Key Takeaways

Knowledge Unlocked

Five things to remember about width

Use these points when writing width-based responsive CSS.

5
Core concepts
02

max-width

Narrow screens.

Query
VP 03

Viewport

Window width.

Target
+H 04

and height

Both axes.

Combine
🌐 05

100% support

Universal.

Compat

❓ Frequently Asked Questions

The width media feature matches styles based on the viewport width — the visible horizontal size of the browser window. Use min-width, max-width, or an exact width value inside @media rules to adapt layouts for phones, tablets, and desktops.
min-width applies styles when the viewport is at least the given width (larger screens). max-width applies styles when the viewport is at most the given width (smaller screens). Combine both to target specific width ranges like tablet-only layouts.
The media feature queries the viewport width for conditional rules. The width property sets the width of an individual element. @media (min-width: 600px) checks the window; width: 600px sizes one box.
Use them for responsive layouts — stacking columns on phones, multi-column grids on tablets, wider typography on desktops, and any design that changes based on how wide the browser window is.
Yes. min-width, max-width, and width media features are supported in all modern browsers including Chrome, Firefox, Safari, Edge, and Opera with 100% global support.

Practice in the Live Editor

Open the HTML editor and experiment with @media (min-width: 600px) and responsive layouts.

HTML Editor →

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.

5 people found this page helpful