Skip to article content
FrontendXLesson 23 / 30

Media queries and container queries

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

intermediate · about 40 minutes · 2 exercises

  • Implement
  • Debug

What you will be able to do

#
  • Write media queries for viewport conditions.
  • Use container queries to style a component by its own size.

Overview

#

Media queries respond to the viewport, the user's preferences or the output device. Beyond width, they can target prefers-reduced-motion, prefers-color-scheme, prefers-contrast, orientation and resolution. Preference queries are how you respect user settings.

snippet.css
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
@media (prefers-color-scheme: dark) {
  :root { color-scheme: dark; }
}

Media queries have a limitation: a component cannot know how much space it has, only how wide the viewport is. The same card in a narrow sidebar and a wide main column gets the same styles. Container queries fix this by letting a component respond to its container's size.

snippet.css
.card-host { container-type: inline-size; }

.card { display: grid; gap: 0.5rem; }

@container (min-width: 30rem) {
  .card { grid-template-columns: 8rem 1fr; }
}

You declare a containment context with container-type: inline-size on the parent, then query it with @container. The component adapts to wherever it is placed, which makes it genuinely reusable. Named containers (container-name) let a component query a specific ancestor when several nested containers exist.

Use media queries for page-level decisions and container queries for component-level decisions. The two compose: a media query can change the page grid, and container queries inside each region adapt the components to the space they receive.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Make a card switch from a stacked layout to a two-column layout when its own container is at least 30rem wide.

    starter.css
    .card { display: grid; gap: 0.5rem; }
    /* add containment and the container query */

    Reveal the reference answer
    reference.css
    .card-host { container-type: inline-size; }
    .card { display: grid; gap: 0.5rem; }
    @container (min-width: 30rem) {
      .card { grid-template-columns: 8rem 1fr; }
    }

    Authored expected output: The card adapts based on its container width, not the viewport width.

    container-type: inline-size on the parent creates the query context the @container rule targets.

  2. Exercise 2Debug

    This container query never matches. Explain why and fix it.

    starter.css
    .card { container-type: inline-size; }
    @container (min-width: 30rem) { .card { grid-template-columns: 1fr 1fr; } }

    Reveal the reference answer
    reference.css
    A container cannot query itself. Move container-type to a parent wrapper:
    .wrapper { container-type: inline-size; }
    @container (min-width: 30rem) { .card { grid-template-columns: 1fr 1fr; } }

    Authored expected output: The query targets a parent container instead of the element itself.

    Container queries resolve against an ancestor container, so the queried element must be a descendant of the container.

Common misconceptions

#
  • Container queries are just media queries with a different name.
  • A component can query any ancestor without declaring a container.

Follow-up practice

#
  • html-css-24-fluid-typography

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.