CSS Media Queries

Master the core of Responsive Web Design. Learn to adapt your layouts for any screen, from mobile-first to dark mode.

Lesson ProgressStep 1 of 10
0 EXP

Welcome, developer. Let's learn to control the layout of the web. This is our current design.

.container {
  display: flex;
  flex-direction: row;
}
.box {
  width: 150px;
  height: 100px;
}

The Anatomy of a Media Query

A media query is a CSS rule that starts with @media. It's followed by a condition, and if that condition is true, the CSS rules inside its curly braces { ... } are applied.

The condition consists of an optional **media type** (like screen) and one or more **media features** (like (max-width: 600px)).

@media screen and (max-width: 600px) {
  body {
    background-color: lightblue;
  }
}

This example tells the browser: "If the device is a screen AND its width is 600px or less, make the body's background light blue."

System Check

What is the name of the rule that begins a media query?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

✍️
Syntax Master

Master the '@media' rule and media feature syntax.

🏗️
Breakpoint Architect

Define a logical, ordered set of breakpoints.

🏆
Mobile-First Pro

Successfully implement a mobile-first design.

Mission: Build a Mobile-First Layout

The `.box` is already `100%` wide for mobile. Add a media query to make its `width` become `500px` and its `background-color` change to `tomato` on screens `768px` or wider.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Breakpoints

For a **mobile-first** design, drag the CSS blocks into the correct order as they would appear in a stylesheet.

@media (min-width: 768px) { ... }
@media (min-width: 1200px) { ... }
.base-styles { ... } /* Mobile */

Challenge: Complete the Syntax

Fill in the missing parts to target a screen at least 768px wide **and** in landscape mode.

(min-width: 768px)(: landscape) { ... }

Consult A.D.A.

The Philosophy of Responsive Design

In the early days of the web, we designed for one screen size: a desktop monitor. Then came laptops, then netbooks, then smartphones, tablets, and smart TVs. The web became a "one-size-fits-none" environment. **Responsive Web Design (RWD)**, a term coined by Ethan Marcotte, is the solution. It's not a single technology, but a philosophy built on three pillars: a **fluid grid**, **flexible images**, and **media queries**.

Media queries are the "brains" of the operation. They allow you to ask the browser questions about the user's environment and apply specific CSS rules based on the answers.

Mobile-First vs. Desktop-First: A Strategic Choice

You can build your responsive design from two different directions:

  • Desktop-First (Graceful Degradation): You start with a complex, wide-screen layout and use max-width queries to "remove" or "simplify" the layout for smaller screens. This was common in the early 2010s.
  • Mobile-First (Progressive Enhancement): You start with a simple, single-column layout for mobile devices. Then, you use min-width queries to "add" complexity (like columns or larger fonts) as the screen gets bigger.

Today, **mobile-first is the industry standard**. Why? Because it forces you to prioritize content and often results in lighter, faster-loading sites for mobile users (who are often on slower connections). You only load the complex styles if the device has the screen space to support them.

Content-Out: Breakpoints Based on Design, Not Devices

A common mistake is to set breakpoints based on specific device sizes (e.g., 320px for iPhone, 768px for iPad). **This is a fragile approach.** A new device will be released next month that breaks your design.

A better strategy is the "content-out" or "design-driven" approach. Start with your mobile design and widen your browser window. When the layout starts to look awkward or "breaks" (e.g., text lines get too long, elements collide), **that's where you add a breakpoint**. Your breakpoints should be determined by your content, not by arbitrary device-driven numbers.

❌ Bad Practice

/* iPhone 5 */
@media (max-width: 320px) { ... }
/* iPad */
@media (max-width: 768px) { ... }
/* iPad Pro */
@media (max-width: 1024px) { ... }

This is brittle and will quickly become outdated.

✔️ Good Practice

/* Base (Mobile) */
.container { ... }

/* When content looks bad (e.g., 600px) */
@media (min-width: 600px) { ... }

/* When it breaks again (e.g., 900px) */
@media (min-width: 900px) { ... }

This is robust and tied to your specific design.

Beyond Width: The Future of Media Features

Media queries can ask about more than just width. We can now adapt our designs to the user's preferences and environment in powerful ways:

  • (prefers-color-scheme: dark): Detects if the user has requested a dark mode at the OS level, allowing you to provide a custom dark theme.
  • (prefers-reduced-motion: reduce): An accessibility feature. If the user is prone to motion sickness, you should disable or reduce non-essential animations.
  • (orientation: landscape): Detects if the device is being held sideways.
  • (min-resolution: 2dppx): Targets high-resolution ("Retina") displays to serve higher-quality images.
Key Takeaway: Media queries are your tool for building an inclusive, accessible, and future-proof web. Embrace a mobile-first, content-out strategy, and start thinking beyond just `width` to create truly adaptive experiences that respect your users' preferences.

Media Queries & RWD Glossary

Media Query
A CSS feature that allows you to apply styles only when certain conditions (e.g., screen width, orientation) are met. It starts with the @media rule.
Responsive Web Design (RWD)
A web design approach that makes web pages render well on a variety of devices and screen sizes. It's typically built on fluid grids, flexible images, and media queries.
Breakpoint
The point (a specific screen width) at which a media query is triggered, causing the layout of a website to change.
Mobile-First
A responsive design strategy where the default styles are for the smallest screen (mobile). Media queries using min-widthare then added to "progressively enhance" the layout for larger screens.
Desktop-First
The opposite of mobile-first. Default styles target large desktop screens, and media queries using max-width are used to "gracefully degrade" or simplify the layout for smaller screens.
Media Type
A category of device. Common types are screen (for computer screens, tablets, phones), print (for printers), and all (the default, applies to all types).
Media Feature
The specific condition being tested inside a media query. Examples include (min-width: ...), (max-width: ...), (orientation: ...), and (prefers-color-scheme: ...).
Logical Operator
Used to create complex media queries. The most common is and, which combines multiple features (e.g., (min-width: 600px) and (orientation: landscape)). Others include not and only.
prefers-color-scheme
A media feature that detects if the user has requested a light or dark color theme at the operating system level (e.g., (prefers-color-scheme: dark)).
prefers-reduced-motion
An accessibility-focused media feature that detects if the user has requested the system minimize non-essential motion. Used to disable or replace animations.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust, responsive, and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS specifications (including CSS Media Queries Level 5) and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!