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."
The @media syntax
All media queries start with the @media keyword. Here's the basic 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:
Desktop-first: Use max-width
An older approach: start with desktop styles, then override for smaller screens. Less common now:
Combining conditions with and, or, not
You can combine multiple conditions using logical operators:
and— Both conditions must be trueor— 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.
Print styles with @media print
The @media print query targets printed pages. Use it to optimize your site for printing — hide navigation, change colors to black/white, show full URLs, etc.
Print media queries are useful for:
- Hiding navigation, sidebars, and footers that don't belong in printed pages
- Showing full URLs so readers can visit links in print
- Changing colors to black/white for printing efficiency
- Controlling page breaks to avoid orphaned headings
- Removing gradients and shadows to reduce ink usage
Try it yourself: Print stylesheet
Edit the code below, then try printing it (Ctrl+P / Cmd+P). Notice how the navigation disappears and links show their URLs.
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,notfor precise control. - @media print optimizes pages for printing — hide nav, show URLs, use black/white colors.
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
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 →