# Media Query

> A CSS rule that only works when a test is true, like a wide screen.

**Field:** Software Development  
**Category:** Web Development  
**Also known as:** CSS Media Query, @media, Media Queries

## Think of it like

Think of it like a coat rule. If it is cold out, wear the big coat. If not, a jacket will do.

## Definition

A media query is a CSS rule that only applies when a test is true. The most common test is the width of the screen. You write the styles for a phone first. Then a media query adds changes for wider screens, like more columns. It can also test for dark mode, printing or a reader who wants less motion.

## Example

Emma's shop shows one column of products on a phone. At 768 pixels wide and up, a media query makes it three.

## Example code

```css
@media (min-width: 768px) {
  .products { grid-template-columns: repeat(3, 1fr); }
}
```

From 768 pixels wide and up, the products sit in three columns.

## Why it matters

One page has to work on a phone and a big screen. Media queries are how a page changes shape to fit.

## Common mistake

Picking sizes to match one phone model. Pick the width where your own layout starts to look wrong.

## Three principles

- **Only when true.** The styles wait for the test to pass.
- **Width is the usual test.** Change the layout as the screen grows.
- **Other tests too.** Dark mode, print and less motion.

## Related terms

- [Responsive Web Design](https://learn.benibix.com/term/design/responsive-web-design)
- [Responsive Breakpoint](https://learn.benibix.com/term/design/responsive-breakpoint)
- [Cascading Style Sheets (CSS)](https://learn.benibix.com/term/development/css)
- [CSS Grid](https://learn.benibix.com/term/development/css-grid)
- [Flexbox](https://learn.benibix.com/term/development/flexbox)
- [Viewport](https://learn.benibix.com/term/design/viewport)
- [Viewport Meta Tag](https://learn.benibix.com/term/development/viewport-meta-tag)

## Told apart from

- [Media Query vs Responsive Breakpoint](https://learn.benibix.com/compare/media-query-vs-responsive-breakpoint): People think a media query and a breakpoint are the same thing, and they are not. The breakpoint is the screen width where the layout should change. The media query is the CSS rule that makes the change happen at that width.

## Taught in

- [Responsive Web Design](https://learn.benibix.com/learn/development-web/responsive-web-design): HTML, CSS and JS, 10/14

![On the left, a narrow phone shows three boxes stacked in one column. On the right, a wide screen shows the same three boxes side by side in three columns. A note says three columns once the screen is 768 pixels or wider, and the rule @media (min-width: 768px) is written under it.](https://learn.benibix.com/diagrams/dev-media-query.svg)

---

Source: https://learn.benibix.com/term/development/media-query
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
