Online admissions open for the SEO, web development and networking & cybersecurity courses. Enquire for the next batch.

CSS Media Queries: Mobile-First Breakpoints, Range Syntax and User Preferences

5 min read Updated By the SkillNewton team

What is a CSS media query?

A CSS media query applies a block of styles only when a condition about the device or the visitor's settings is true, such as the viewport being at least 48rem wide or the visitor preferring a dark colour scheme. Written with the @media rule, media queries are the main tool for responsive breakpoints and for respecting user preferences.

Anatomy of a media query

A media query has an optional media type and one or more media features in brackets, joined with and, commas (meaning "or") or not.

@media screen and (min-width: 48rem) { ... }        /* type + feature */
@media (min-width: 48rem) and (hover: hover) { ... } /* both must be true */
@media (max-width: 30rem), print { ... }             /* either one */
@media not print { ... }                             /* negation */
FeatureWhat it tests
width, min-width, max-widthViewport width
heightViewport height
hover, pointerWhether the main input can hover and how precise it is
prefers-color-schemeLight or dark theme chosen in the operating system
prefers-reduced-motionWhether the visitor has asked for less animation
prefers-contrastWhether the visitor has asked for more or less contrast

Media types are now mostly limited to screen, print and all. Older types such as handheld and tv are deprecated and match nothing in modern browsers.

Mobile-first breakpoints with min-width

In a mobile-first stylesheet, the rules outside any query describe the narrow layout. Each min-width query then adds or overrides rules for wider screens, in ascending order. This keeps the base CSS simple and means a small screen never has to undo desktop rules.

.site-nav ul { display: none; }           /* small screens: menu button only */
.menu-toggle { display: block; }

@media (min-width: 48rem) {
  .site-nav ul { display: flex; gap: 1.5rem; }
  .menu-toggle { display: none; }
}

@media (min-width: 75rem) {
  .container { max-width: 72rem; margin-inline: auto; }
}

Set breakpoints where your content needs them: widen the browser and add a query when lines become too long or components look stretched. Writing breakpoints in rem or em lets the layout respond when a visitor increases their default font size. The responsive web design guide covers the full approach, including images and fluid type.

The range syntax: (width >= 768px)

Media Queries Level 4 added a range syntax that uses comparison operators instead of min- and max- prefixes. It is supported in all current major browsers and reads more naturally, especially for ranges with two ends.

/* Equivalent to (min-width: 768px) */
@media (width >= 768px) { ... }

/* Between two sizes, inclusive */
@media (480px <= width <= 767.98px) { ... }

/* Clearer than max-width: 767.98px, with no overlap at exactly 768px */
@media (width < 768px) { ... }

The range syntax also solves a long-standing gap problem. With max-width: 767px and min-width: 768px, a viewport of 767.5px, possible with zoom or high-density screens, matched neither query. Using < and >= with the same value leaves no gap and no overlap.

Respecting user preferences: colour scheme and motion

prefers-color-scheme

Visitors can choose a light or dark theme in their operating system. Define colours as custom properties, then change their values inside the query, so components need no extra rules.

:root {
  color-scheme: light dark;   /* form controls and scrollbars follow the theme */
  --bg: #ffffff;
  --text: #1a1a1a;
  --link: #0b57d0;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --text: #e8e8e8;
    --link: #8ab4f8;
  }
}
body { background: var(--bg); color: var(--text); }
a { color: var(--link); }

Check text contrast in both themes; a colour that passes on white often fails on a dark background.

prefers-reduced-motion

Some people experience dizziness or nausea from movement on screen and turn on a "reduce motion" setting. Remove or tone down large movements, parallax and auto-playing animation when it is set. The CSS animations guide shows patterns for this.

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .hero-slide { animation: none; }
}

hover and pointer

Screen width does not tell you whether a device has a mouse. Use (hover: hover) to add hover-only effects and (pointer: coarse) to enlarge controls for touch input.

Container queries in brief

Media queries respond to the viewport, but a reusable component may appear in a wide main column on one page and a narrow sidebar on another. Container queries let the component respond to the width of its own container instead. They are supported in all current major browsers.

.card-list { container: cards / inline-size; }

@container cards (width >= 32rem) {
  .card { display: grid; grid-template-columns: 8rem 1fr; gap: 1rem; }
}

A good rule of thumb: use media queries for page-level layout and user preferences, and container queries for components. Container queries cannot test user preferences such as dark mode.

Writing and testing media queries step by step

  1. Confirm the page has the viewport meta tag, or media queries will not behave on phones.
  2. Write the base styles for the narrowest layout with no query.
  3. Widen the browser slowly and note each width where the content starts to look wrong.
  4. Add min-width or range-syntax queries at those points, in ascending order.
  5. Add prefers-color-scheme and prefers-reduced-motion rules for colours and motion.
  6. Test with dev tools device mode, and use the Rendering panel in Chromium-based browsers to emulate dark mode and reduced motion.

Practical exercise: a theme-aware, responsive landing page

Take a simple landing page with a header, a three-feature section and a footer, built with the techniques in our CSS basics guide.

  1. Write mobile-first CSS and add two breakpoints using the range syntax.
  2. Turn the navigation into a horizontal menu from the first breakpoint upwards.
  3. Define all colours as custom properties and add a dark theme with prefers-color-scheme.
  4. Add a subtle hover lift to feature cards only for devices that support hover.
  5. Disable the hover animation and smooth scrolling under prefers-reduced-motion: reduce.

You are done when the page has no horizontal scroll from 320px upwards, switches theme when you change the emulated colour scheme, and shows no movement with reduced motion emulated. Responsive templates like this are built in SkillNewton's PHP foundation course.

Common mistakes

  • Forgetting the viewport meta tag, so phones render the desktop layout scaled down.
  • Mixing min-width and max-width queries at random, so rules fight each other and gaps appear between breakpoints.
  • Writing min-width queries in descending order, so smaller breakpoints override larger ones.
  • Using screen width to guess whether a device supports hover instead of testing hover and pointer.
  • Adding a dark theme without rechecking text and link contrast.

Frequently asked questions

Should I use max-width or min-width media queries?

Either works, but a mobile-first stylesheet with min-width (or width >=) queries is usually simpler, because each breakpoint only adds rules and small screens get the lightest CSS.

Can I put media queries in the HTML?

Yes. The media attribute works on <link>, <source> and <style>, for example media="print" on a print stylesheet. JavaScript can test a query with window.matchMedia().

How many breakpoints should a site have?

As few as the content needs. Many layouts work with two or three, and flexible techniques such as grid with auto-fit reduce the number further.

Do media queries affect page speed?

CSS inside a non-matching query is still downloaded, but stylesheets linked with a non-matching media attribute are fetched at low priority and do not block rendering. The web performance guide covers render-blocking resources.

Want to Learn This Properly?

The PHP Web Development: Foundation & Practical Implementation (Online) turns guides like this into practised skills, with projects on real websites.

WhatsApp Call Us Now

Free counselling

Send an Enquiry

Tell us which course you are interested in. We reply by email or phone with the next batch and how to enrol.

Protected by reCAPTCHA. Google Privacy and Terms apply.

We reply by email or phone within working hours. No spam.