Skip to main content
Syllabus
On this page

Media Queries

Badar KhalilUpdated September 30, 2026 8 min read

CSS Media Queries: Complete Guide

Focus keyword: CSS media queries. Media queries let your CSS react to the device and the user: screen size, orientation, resolution, input type and preferences such as dark mode or reduced motion. They are the main tool of responsive design. In this lesson you will learn the syntax, the modern range syntax, how to choose breakpoints, how to query user preferences, how to write print styles, and how to use media queries from JavaScript.

CSS media query syntax, min-width vs max-width, media query range syntax, responsive breakpoints 2026, content-based breakpoints, orientation landscape portrait, prefers-reduced-motion, prefers-color-scheme, prefers-contrast, prefers-reduced-data, hover and pointer media features, print stylesheet, matchMedia JavaScript, resolution and dppx, media query em vs px, media queries vs container queries, nested media queries, forced-colors.

1. Media Query Syntax

A media query has an optional media type and one or more conditions in parentheses: @media screen and (min-width: 600px) { ... }. The rules inside apply only when the whole query is true.

  • Media types: all (default), screen, print. Older types such as handheld and tv are obsolete.
  • Media features: conditions such as width, height, orientation, aspect-ratio, resolution, hover, pointer and many preference features.
  • Logical operators: and (all true), a comma (any true, works as or), not (negate the whole query) and only (hide the query from very old browsers).

Example with a comma: @media (max-width: 500px), (orientation: portrait) { ... } applies when either condition is true.

2. min-width and max-width

min-width: 600px means the viewport is 600px or wider. max-width: 599px means the viewport is 599px or narrower. For mobile-first CSS use min-width as your main tool. Mixing both is useful for a range, such as a rule that only applies to tablets.

3. Range Syntax (Modern)

Modern browsers support comparison operators, which are shorter and avoid off-by-one gaps: @media (width >= 600px), @media (width < 600px) and @media (400px <= width <= 900px). The range syntax also works with height and aspect-ratio. It removes the old trick of writing max-width: 599.98px to avoid overlaps with min-width: 600px, because width < 600px and width >= 600px never overlap.

4. Choosing Breakpoints

Do not build for specific devices, because new sizes appear every year. Resize your layout and add a breakpoint where the design breaks: a line becomes too long, columns become too narrow or a gap looks empty. Typical starting values are 600px, 900px and 1200px, or 40em, 60em and 75em. Keep the number small, usually two to four. Use CSS variables for spacing, but remember that variables cannot be used inside the media query condition itself.

em vs px in media queries

In media queries em and rem are based on the browser default font size (normally 16px), not on your CSS. When a user increases their default font size, em-based breakpoints trigger earlier, which gives larger text more room. This is one reason many teams prefer em breakpoints, for example @media (min-width: 40em). Pick one unit and use it everywhere.

5. Orientation, Aspect Ratio and Resolution

  • orientation: landscape or portrait: based on width versus height of the viewport.
  • aspect-ratio and min-aspect-ratio: for example (min-aspect-ratio: 16/9) for very wide windows.
  • resolution: (min-resolution: 2dppx) matches high density screens, useful for sharper background images.
  • height: (max-height: 500px) helps with short landscape phones.

6. Input Capability Queries

hover: hover and pointer: fine describe the primary input, while any-hover and any-pointer check every available input. Use them to add hover effects only where hovering is possible and to enlarge controls for touch.

7. User Preference Queries

  • prefers-color-scheme: dark: the user wants a dark interface.
  • prefers-reduced-motion: reduce: remove or shorten animations, parallax and autoplay movement.
  • prefers-contrast: more or less: adjust borders and colors.
  • prefers-reduced-data: reduce: skip large images, videos or web fonts when the user asks to save data (limited support).
  • prefers-reduced-transparency: reduce: limited support, reduce blur and transparency.
  • forced-colors: active: Windows high contrast mode. Avoid fighting it and keep borders visible.
  • display-mode: standalone: the page runs as an installed web app.

8. Print Styles

Use @media print to make pages print well: hide navigation, ads and buttons, use black text on white, set a readable size in points, avoid breaking inside cards with break-inside: avoid, and show link addresses with a[href]::after { content: ' (' attr(href) ')'; }. Test with the print preview in DevTools.

9. Nesting and Combining

You can combine queries with and, for example (min-width: 700px) and (orientation: landscape). Media queries can be nested inside a rule in native CSS nesting, which keeps responsive rules next to the component. They can also be used in <link media='...'>, <source media='...'> and @import url() media-query.

10. Media Queries in JavaScript

window.matchMedia('(min-width: 600px)') returns an object with a matches boolean and a change event. Use it to reset a mobile menu when the screen becomes wide, to swap content, or to check the user's color scheme. Prefer CSS whenever possible and use JavaScript only for behavior.

11. Media Queries vs Container Queries

A media query asks about the viewport or the device. A container query asks about the size of a parent element, which is better for reusable components. Use media queries for the page layout, such as the number of columns of the whole page, and for user preferences. You will learn container queries in a later lesson.

12. Common Mistakes

  • Missing the viewport meta tag, so queries never match the real width.
  • Writing min-width and max-width rules that overlap or leave a gap.
  • Targeting devices instead of content.
  • Using too many breakpoints.
  • Putting a media query before the base rule it should override, so the base rule wins by order.
  • Trying to use a CSS variable inside the query condition.
  • Hiding hover content from touch users.
  • Ignoring prefers-reduced-motion.
  • Forgetting to test print output.

Key Takeaways

Write base styles first, then add min-width queries where the content needs them. Prefer range syntax, choose breakpoints by content, query user preferences and input capabilities, write a print stylesheet, and use matchMedia only for behavior that CSS cannot handle.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself JAVASCRIPT
Output

Press Run to execute.

Exercise: Build a Three Breakpoint GridHTML

Make the .cards container one column by default, two columns from 600px and four columns from 1000px using mobile-first min-width media queries. Keep a 16px gap.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
Base grid has one column, @media (min-width: 600px) uses 2 columns and @media (min-width: 1000px) uses 4 columns

This is a self-check — compare your result with the expected output above.

Exercise: Write a Print StylesheetHTML

Add an @media print block that hides the nav and the button, sets body text to black on white at 12pt, prevents the article from breaking across pages, and prints the URL after each link.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
A @media print block with nav and button set to display: none, body font-size 12pt with black text, article break-inside: avoid and a[href]::after showing attr(href)

This is a self-check — compare your result with the expected output above.

Was this page helpful?