# Media Queries & Mobile-First

> Adapt your layout to any screen size using media queries, and learn why 'Mobile-First' is the industry standard.

Subject: CSS | Lesson 26 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/media-queries
Previous: [Subgrid](https://codewithanimation.in/css/subgrid.md) | Next: [Container Queries](https://codewithanimation.in/css/container-queries.md)

## What they are

Media queries are conditional CSS rules. They tell the browser: 'Only apply these styles if the screen matches a specific condition', such as being wider than 768 pixels.

They are the foundational technology behind Responsive Web Design, allowing a single website to seamlessly adapt its layout for phones, tablets, and desktop monitors.

## Why it matters

Without media queries, a complex multi-column desktop layout will squish on a mobile phone, making the text unreadable and buttons impossible to tap.

Media queries allow you to fundamentally alter the layout—changing grids, hiding elements, or resizing typography—based entirely on the user's device size.

## How it works (The Mobile-First Way)

1. **Mobile Default**: Write your base CSS outside of any media queries. Design for the narrowest screen first (usually a single column).

2. **Tablet Query**: Use `@media (min-width: 768px) { ... }` to add complexity (like a two-column grid) once the screen is wide enough to support it.

3. **Desktop Query**: Use `@media (min-width: 1024px) { ... }` to add even more complexity (like a three-column grid or sidebars).

## Common mistake: The Desktop-First Trap

If you design for desktop first and use `max-width` queries to fix it for mobile, you will end up fighting your own code. You'll have to write CSS to 'undo' desktop styles (like resetting margins or changing `display: flex` back to `block`). Mobile-first CSS is additive, meaning you only write new rules as the screen grows, which is much cleaner.

## Tip: Tailwind makes it invisible

If you use Tailwind CSS, media queries are built into the classes! `flex md:flex-row` means 'use flexbox (mobile default), and when the screen hits the `md` breakpoint, change it to a row'. Under the hood, this is just a `min-width` media query.

## Try it: Try it

Drag the slider to resize the simulated viewport. Watch how the CSS automatically applies different rules to completely change the layout from a mobile stack to a desktop grid!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/media-queries

## Check yourself

### Question 1

What does a 'Mobile-First' CSS strategy mean?

- A. Writing the base CSS for mobile phones without media queries, then using min-width queries for larger screens.
- B. Only supporting mobile phones.
- C. Writing the desktop CSS first, then using max-width queries for phones.

Correct answer: A

Mobile-First means your default CSS applies to the smallest screens. You then progressively enhance the layout for larger screens using min-width media queries.

- Why not B: Mobile-first still fully supports desktop; it's just the order in which you write the CSS.
- Why not C: That is called 'Desktop-First', which often leads to messy override code.

### Question 2

Which media query targets screens that are at least 1024px wide?

- A. @media (min-width: 1024px)
- B. @media (max-width: 1024px)
- C. @media (width: 1024px)

Correct answer: A

`min-width: 1024px` means the rule applies as long as the screen is a minimum of 1024 pixels wide (i.e., 1024px and up).

- Why not B: This targets screens that are 1024px or SMALLER.
- Why not C: This targets screens that are exactly 1024px wide, which is almost never what you want.

### Question 3

If you have `.card { background: blue; }` and `@media (min-width: 800px) { .card { background: red; } }`, what color is the card on a 1000px screen?

- A. Red
- B. Blue
- C. Purple (they blend)

Correct answer: A

Because 1000px is greater than 800px, the media query activates. Its rules overwrite the base styles, making the card red.

- Why not B: The media query condition is met (1000 >= 800), so the red rule is applied.
- Why not C: CSS rules overwrite each other based on specificity and order. They do not blend.

## Remember this

- Media queries are conditional CSS rules based on screen size or features.
- Always use a Mobile-First approach: base styles for mobile, `min-width` for larger screens.
- Mobile-first CSS is additive, preventing tangled 'undo' code.
- Tailwind prefixes like `md:` are just `min-width` media queries in disguise.
