CSS Reference ยท Media Queries and Features
The width and height media features
Reference for the width and height media features: min-width, max-width, range syntax, viewport scrollbars, em-based breakpoints and common mistakes.
The width and height media features test the size of the viewport: the visible area of the page in a browser window or, in a print context, the page box. They are the backbone of responsive design, used to switch layouts at breakpoints.
You can write them with the traditional prefixes min- and max-, or with the modern range syntax using comparison operators. Both mean the same thing.
@media (min-width: 40em) { /* 40em or wider */ } @media (width >= 40em) { /* the same, range syntax */ } @media (400px <= width <= 800px) { /* between */ }
Try it
Syntax
width: <length> height: <length> @media (width: 600px) @media (min-width: 600px) @media (max-width: 599.98px) @media (width >= 600px) @media (width < 600px) @media (400px <= width < 800px)
Type: range. A value is a non-negative <length>. Without a value, (width) is true for any non-zero width.
Values
| Syntax | Meaning |
|---|---|
(width: 600px) |
The viewport is exactly 600px wide. Rarely useful. |
(min-width: 600px) |
The viewport is 600px or wider (inclusive). |
(max-width: 600px) |
The viewport is 600px or narrower (inclusive). |
(width >= 600px), (width > 600px) |
Range comparison; >, >=, <, <= and = are allowed. |
(400px <= width <= 800px) |
A range with both bounds. |
height forms |
The same options, applied to the viewport's height. |
Usage notes
- Mobile-first. Write base styles for small screens, then add
min-widthqueries for larger ones. Your default CSS stays simple. emin a query is not your font size. Units in media queries are relative to the browser's initial font size (usually 16px), not to any CSS you wrote. A breakpoint inemorremalso moves with the user's font settings, which is often a good thing.- Inclusive edges can overlap.
max-width: 600pxandmin-width: 600pxboth match at exactly 600px. Range syntax with<or>avoids overlap without odd values like599.98px. - The scrollbar counts. Viewport width includes a classic scrollbar, so it can differ slightly from
100%or100vw-based expectations. - Viewport, not container. These features describe the whole window. For a component that should react to the space it has, use a container query.
device-widthis deprecated. It measures the screen, not the viewport; do not use it.- Mixing
andand range syntax is allowed:(width >= 600px) and (orientation: landscape).
Browser support
min-width, max-width and the height equivalents work everywhere. Range syntax is newer and supported in current browsers; for old browsers, use the min- and max- form or check a compatibility table.
Related
@media- range syntax
orientationaspect-ratiofeature- Lesson: Media Queries
- Lesson: Responsive Design