What are media queries?

A media query is a CSS feature that applies different styles based on device characteristics — screen size, orientation, resolution, and more. It's the foundation of responsive web design.

Before media queries, websites were designed for one fixed width (usually 1024px). Today, websites must work on screens ranging from 320px (old phones) to 4K displays (3840px+). Media queries solve this by letting you write conditional CSS: "if the screen is wider than 768px, use this layout; otherwise use this one."

Core idea:
Media queries ask the browser a question about the device, and apply CSS rules only if the answer is true. This is how you build truly responsive designs.

The @media syntax

All media queries start with the @media keyword. Here's the basic structure:

style.css @media query structure

The syntax is @media (condition) { /* CSS rules */ }. The condition is the media query that gets evaluated:

  • (min-width: 768px) — "if the viewport is 768px wide or wider"
  • (max-width: 767px) — "if the viewport is 767px wide or narrower"
  • (orientation: landscape) — "if the device is in landscape mode"
  • print — "if the page is being printed"

Common breakpoints

A breakpoint is a specific screen width where you change the layout. Most projects use these standard breakpoints, which match common device sizes:

Device Type Breakpoint Media Query Examples
Mobile (extra small) 320px – 479px @media (max-width: 479px) iPhone SE, older phones
Mobile (small) 480px – 767px @media (min-width: 480px) iPhone 12, modern phones
Tablet 768px – 1023px @media (min-width: 768px) iPad, tablet devices
Desktop 1024px – 1199px @media (min-width: 1024px) Laptops, small monitors
Large Desktop 1200px+ @media (min-width: 1200px) 4K monitors, large screens

Pro tip: Use these breakpoints in your projects. They're widely supported and tested across devices.

Mobile-first: min-width vs max-width

There are two approaches to writing media queries:

Mobile-first (recommended): Use min-width

Start with styles for mobile, then add media queries for larger screens. This is the modern, recommended approach:

mobile-first.css Base mobile styles, then enhance for bigger screens

Desktop-first: Use max-width

An older approach: start with desktop styles, then override for smaller screens. Less common now:

desktop-first.css Base desktop styles, then reduce for smaller screens
Best practice:
Use mobile-first with min-width. It's easier to maintain, performs better, and matches modern web standards. Start simple, then add complexity as screens get bigger.

Combining conditions with and, or, not

You can combine multiple conditions using logical operators:

combined.css Using and, or, not in media queries
  • and — Both conditions must be true
  • or — At least one condition must be true (use comma to separate)
  • not — The condition must be false

Try it yourself: Responsive columns

Edit the code below to see how the layout changes at different breakpoints. Resize the preview window to watch it adapt from 1 column on mobile, to 2 on tablet, to 3 on desktop.

✏️ Try It Yourself — Responsive Layout
Preview

Key takeaways

  • Media queries apply CSS conditionally based on device features like screen width.
  • @media (min-width: X) applies styles for screens X pixels wide or larger (mobile-first approach).
  • Standard breakpoints are 480px, 768px, 1024px, 1200px — use these in your projects.
  • Mobile-first is the modern way: write base styles for mobile, then add complexity for bigger screens with min-width queries.
  • Combine conditions with and, or, not for precise control.
  • @media print optimizes pages for printing — hide nav, show URLs, use black/white colors.
Challenge: Build a Responsive Page

Task: Create a responsive portfolio page that:

  • Has a grid layout: 1 column on mobile (max-width: 767px), 2 columns on tablet (768px – 1023px), 3 columns on desktop (1024px+)
  • Uses proper @media queries with min-width (mobile-first)
  • Includes at least 6 project cards with title and description
  • Has responsive padding/margins that scale with breakpoints
  • Includes a media query that hides a sidebar on mobile

Use the editors above as templates. Test by resizing your browser!

Hint: Start with mobile-first base styles (1 column grid). Then add @media (min-width: 768px) to change to 2 columns, and @media (min-width: 1024px) for 3 columns. Use grid-template-columns: repeat(2, 1fr) for 2 equal columns.

Test your knowledge

Question 1 of 5
What does @media (min-width: 768px) do?
Question 2 of 5
Which approach is mobile-first?
Question 3 of 5
How do you combine two conditions in a media query?
Question 4 of 5
What is @media print used for?
Question 5 of 5
What is a breakpoint?
0
/5
Great job! You understand media queries.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just mastered media queries.
Build responsive sites across all devices.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →