Course topics

By WebNest Studio Engineering Team

CSS Tutorial

CSS Media Queries

Media queries are the mechanism that makes responsive design possible in CSS: they let you apply a block of styles conditionally, based on characteristics of the device or viewport, most commonly its width. A media query wraps a set of normal CSS rules inside an @media block, and the browser only applies those rules when the specified condition is true.

Media queries can test many features (orientation, resolution, hover capability, and more), but by far the most common use is testing viewport width with min-width and max-width, which is how "breakpoints" are implemented in practice.

Why it matters

Media queries apply styles only when a condition holds, such as the window being at least a certain width. They are how one stylesheet serves a phone and a wide monitor. They also respond to the user's preferences, such as dark mode and reduced motion.

Basic Syntax

A media query looks like @media (condition) { /* rules */ }. The condition is a media feature test, such as (min-width: 768px), and any normal CSS rules inside the block only apply when that condition matches the current viewport. Multiple conditions can be combined with and, and multiple queries can be separated with commas to act as an "or."

min-width vs. max-width

min-width matches when the viewport is at least that wide — this is the basis of mobile-first design, since you write base styles for the smallest screens and add min-width queries to enhance the layout as space increases. max-width matches when the viewport is at most that wide, which is the basis of desktop-first design, where you write base styles for large screens and use max-width queries to simplify the layout as space shrinks. Mixing both philosophies in the same project without a clear convention is a common source of confusing, hard-to-predict CSS.

Other Useful Media Features

Beyond width, media queries can test orientation: portrait / landscape, prefers-color-scheme: dark / light for respecting OS-level dark mode, and prefers-reduced-motion: reduce for respecting a user's request to minimize animation (covered further in the accessibility topic).

Examples

A mobile-first breakpoint switching a nav from a stacked to a horizontal layout

CSS
.nav-links {
  display: flex;
  flex-direction: column; /* stacked on small screens */
}

@media (min-width: 768px) {
  .nav-links {
    flex-direction: row; /* side by side once there is enough width */
    gap: 1.5rem;
  }
}
Output
Navigation links stack vertically on narrow screens and switch to a horizontal row with spacing once the viewport reaches 768px or wider.

Combining a width condition with a feature query using "and"

CSS
@media (min-width: 600px) and (orientation: landscape) {
  .hero {
    padding: 4rem 2rem;
  }
}
Output
The hero section receives extra padding only when the viewport is at least 600px wide AND in landscape orientation — both conditions must be true simultaneously.

Common Mistakes

  • Mixing min-width (mobile-first) and max-width (desktop-first) queries inconsistently within the same stylesheet, making the cascade hard to predict.
  • Choosing breakpoints that match specific, popular device widths instead of the widths where the content itself starts to break down.
  • Forgetting the viewport meta tag (<meta name="viewport" content="width=device-width, initial-scale=1">) in HTML, which causes mobile browsers to render at a zoomed-out desktop width and ignore media queries as expected.
  • Writing overlapping min-width and max-width queries that unintentionally both match at certain widths, causing conflicting styles.

Exercise

Write the layout mobile-first. By default the three elements in main stack in a column with a 12px gap. From a viewport width of 600px upwards, they sit in a row, centred vertically.

Expected result: a stacked layout in a narrow preview, switching to a single row when the preview is at least 600px wide.

CSS · starter code
/* The page contains: <main> with an <h1>, a <p> and a <button> */

main {
  /* TODO: a flex column with a 12px gap */
}

/* TODO: from 600px wide, switch to a row, centred vertically */
Show hints
  • The query is written @media (min-width: 600px) { ... }.
  • Inside it, only the properties that change are repeated: flex-direction and align-items.
Show solution
CSS · solution
/* The page contains: <main> with an <h1>, a <p> and a <button> */

main {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

@media (min-width: 600px) {
  main {
    flex-direction: row;
    align-items: center;
  }
}

Quick quiz

1. When do the styles inside @media (min-width: 768px) apply?

  • A. Only when printing
  • B. When the viewport is narrower than 768px
  • C. Only at exactly 768px
  • D. When the viewport is 768px wide or wider
Show answer

D. min-width means "at least this wide".

2. Which query type goes with a mobile-first approach?

  • A. min-width
  • B. orientation
  • C. max-width
  • D. print
Show answer

A. The base styles serve small screens, and each min-width query adds rules for larger ones.

3. Which media feature detects that the user has asked for a dark theme?

  • A. color-mode
  • B. prefers-color-scheme
  • C. theme
  • D. display-mode
Show answer

B. @media (prefers-color-scheme: dark) applies when the operating system is set to dark.

Interview questions

What is the difference between min-width and max-width media queries?

Show answer

A min-width query applies from the given width upwards and is used mobile-first: the base CSS is the small-screen layout and each query adds to it. A max-width query applies up to the given width and is used desktop-first: the base CSS is the large layout and queries undo or simplify it. Mobile-first usually gives simpler CSS, because small-screen layouts need fewer rules.

Besides width, what can media queries detect?

Show answer

The orientation of the screen; whether the output is a screen or print; the resolution, for high-density displays; whether the primary input can hover, with hover and pointer; and user preferences such as prefers-color-scheme, prefers-reduced-motion and prefers-contrast. Conditions are combined with and, and listed as alternatives with commas.

Key Points to Remember

  • @media (condition) { } applies a block of CSS only when the condition matches the current viewport or device.
  • min-width matches at or above a width (mobile-first); max-width matches at or below a width (desktop-first).
  • Conditions can be combined with "and" for AND logic, or comma-separated for OR logic.
  • Media queries can also test orientation, prefers-color-scheme, and prefers-reduced-motion, not just width.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering