Short answer
Media queries follow the viewport or device, and container queries follow a container you name. A media query can change the page when the window is narrow. A container query can change a card when its own box is narrow, even if the window is wide. You set that up with container-type and @container. The two queries answer different questions, so many layouts use both.
Last verified against the sources listed below.
Key takeaways
- MDN says media queries depend on media type or features such as viewport width, not on one component.
- Container queries style an element from its container's size, name, styles, or scroll state.
- Size queries need container-type of size or inline-size. The initial value normal is not a size container.
- CSS Containment Level 3 moved container queries to CSS Conditional Rules Level 5.
- A component that appears in a sidebar and a wide column is the usual container-query case.
- MDN's size example uses 700px as a sample, not as a required breakpoint.
Media queries vs container queries is a choice about which box you measure. Media queries follow the viewport. Container queries follow a container. The verdict is that simple: they are not two spellings of one test.
A page layout usually cares about the window. A card in a sidebar and a main column cares about the box it sits in. CSS lets you write both tests.
Responsive web design needs that split once components move between columns.
What does a media query watch?
A media query watches the device or the viewport, not an individual component. MDN says it can depend on media type, such as print or screen, or on features such as viewport width, orientation, and resolution.
The type is optional and defaults to all, except with the only operator. Feature tests can use min-width and max-width, or the range syntax. MDN shows that (max-width: 1250px) matches a viewport that is 1250px wide or narrower, and that (min-width: 30em) and (max-width: 50em) matches a band. Range comparisons are inclusive unless you use < or >.
Media Queries Level 5 treats width as the viewport width. A sample condition is true when the viewport sits between two lengths. The same note shows a gap: styles capped at 320px and styles starting at 321px can leave widths in between with neither set.
What does a container query watch?
A container query watches an ancestor you mark as a query container. MDN’s container query guide calls this an alternative to media queries, which follow viewport size or other device characteristics.
Size queries need container-type. size uses both axes, inline-size uses the inline axis, and normal (the default) is not a size container.
You then write @container. The rule uses the nearest ancestor with a containment context, unless you target a container-name.
MDN’s sample applies a larger heading when the container is wider than 700px. That 700px is the sample’s choice. The card can be reused without knowing which column it will land in. If the container is narrower than 700px, the heading stays small.
How do you write each query?
You write @media for the viewport and @container for the container. The conditions look alike. The property that sets up the test does not.
A viewport stack:
@media (max-width: 40rem) {
.nav { flex-direction: column; }
}
A container setup from MDN’s pattern:
.post { container-type: inline-size; }
@container (width > 700px) {
.card h2 { font-size: 2em; }
}
CSS Conditional Rules Level 5 defines @container as a conditional group rule whose condition is a container query. That query combines container size queries, container style queries, or both. The declarations apply when the query is true for the element’s query container. The container shorthand sets type and name together, as in container: my-component / inline-size.
CSS Containment Level 3 records that container queries moved to that conditional module. Use Level 5 when you are checking the current syntax.
When should you pick one?
Pick a media query for something that truly depends on the window or the device. Pick a container query when the same component must change with its own box. The component’s box is the deciding measurement.
| Situation | Use | Why |
|---|---|---|
| The whole page switches under a narrow window | Media query | The viewport is the thing that changed |
| Print versus screen | Media query | MDN lists print and screen as media types |
| A card in a sidebar and in a wide column | Container query | The window can stay wide while the card gets narrow |
| A named region, such as a sidebar | Container query with a name | @container sidebar targets that context |
| You are not marking a size container | Media query, or a name-only query | normal does not answer size queries |
MDN also shows a fallback that is still a media query: a grid card becomes one column at width <= 700px. That helps when you are styling from the viewport. It will not follow the card into a narrow column of a wide page.
The CSS Grid Generator is a way to build the columns first. Once a column is the container, a container query can restyle the component inside it.
Can you use both together?
Yes. A media query can set the page columns, and a container query can restyle the cards inside those columns. They do not cancel each other.
A practical split is: the media query decides how many columns the page has, and each column is a container. The card then uses @container for its own type size and stacking. The card does not need a second copy of the page’s breakpoint.
Container query length units, such as cqi in MDN’s sample, are another tool on the container side. If no eligible container exists, MDN says those units default to the small viewport unit for that axis. A unit that quietly follows the viewport is a sign the container was never established.
What mistakes are common?
The mistakes come from measuring the wrong box, or from forgetting the property that creates the container.
- A
@containersize rule with no ancestorcontainer-typenever sees a size.normaldoes not count as a size container. - A size container is not sized by its children, so web.dev says it collapses without an explicit size or a sized layout.
- The W3C technical report publishes the same
@containerrule as the CSS conditional draft. - A media query is used for a card that moves between columns. The viewport did not change, so the card does not change.
- Two media ranges leave a gap, as in the Level 5 width example, so some viewports get neither layout.
inline-sizeis set, then the query tests a block-size change the container is not measuring.- The 700px from MDN’s article is copied as if it were a standard. It is that article’s sample.
Name the container when more than one ancestor could match. MDN shows container-name: sidebar and @container sidebar. Without a name, the query uses the nearest ancestor with a containment context, which may not be the box you meant.
Frequently asked questions
How do CSS media queries vs container queries differ in one line?
CSS media queries vs container queries differ in the box they measure. A media query measures features of the viewport or device, such as width. A container size query measures a container you marked with container-type. The syntax is similar, with @media on one side and @container on the other, but the condition is not reading the same rectangle. If the component's column changes while the window does not, only the container query sees it.
What does container-type normal mean?
MDN says normal is the default. The element is not a query container for size queries, but it can still be a container for a name-only query or a style query. size bases the query on the inline and block dimensions and applies layout, style, and size containment. inline-size uses the inline dimension and applies layout, style, and inline-size containment. CSS Conditional Rules Level 5 also offers the container shorthand, such as container: my-component / inline-size, which sets the name and the type together.
What happens if no container is eligible?
MDN says a container query length unit falls back to the small viewport unit on that axis, the sv* units, when no eligible container is available. That fallback can make a cqi size track the viewport even though you wrote it as a container unit. If styles inside @container never change, check that an ancestor has container-type set to size or inline-size. A name-only query does not establish a size container.
Did Containment Level 3 keep the query syntax?
The CSS Containment Module Level 3 draft says container queries moved to CSS Conditional Rules Module Level 5, and that inline-size containment moved to Level 2. The conditional spec defines @container as a conditional group rule. Its condition is a container query, a boolean combination of container size queries and container style queries. Declarations inside the rule apply when that query is true for the element's query container. Cite Level 5 for the current syntax, not the older containment draft.
Can a media query be a fallback?
Yes. MDN's container query article shows a card laid out with grid, then a media query that switches the card to one column when the viewport width is 700px or less. That is a viewport test, so it does not follow the card if the card is placed in a narrow column of a wide window. Use it when you need a layout for browsers or cases where you are not querying a container. It does not replace a size query on the container itself.
Sources
- CSS container queries · MDN, 2026
- Using media queries · MDN, 2026
- CSS Conditional Rules Module Level 5 · CSS Working Group, 2026
- CSS Containment Module Level 3 · CSS Working Group, 2026
- Media Queries Level 5 · CSS Working Group, 2026
- CSS Conditional Rules Module Level 5 · W3C, 2026
- Container queries · web.dev, 2026