What Are CSS Container Queries and When Should You Use Them?

Learn What Are CSS Container Queries and When Should You Use Them? with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

What Are CSS Container Queries?

CSS container queries allow a component to change its styles based on the size or other supported features of its containing element rather than relying only on the size of the viewport.

This is an important difference from traditional responsive design with media queries. A media query commonly asks, "How wide is the viewport?" A container query can instead ask, "How much space does this component actually have?"

This makes container queries especially useful for reusable components. The same card, widget, panel, or form component can appear in a large desktop region, a narrow sidebar, or a responsive dashboard column and adjust itself according to the space it receives. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_queries))

What Is the Difference Between Media Queries and Container Queries?

Media Query

Media queries respond to characteristics of the user environment, most commonly the viewport.

@media (width >= 768px) {
  .card {
    ...
  }
}

The decision is based on the viewport or another media feature.

Container Query

Container queries respond to the size or other supported features of a specific ancestor container.

= 500px) {
  .card {
    ...
  }
}

The decision is based on the container in which the component is placed.

Container queries are therefore particularly valuable when a component's layout depends on its parent rather than the entire page. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_size_and_style_queries))

Why Are Container Queries Useful for Responsive Components?

Traditional viewport breakpoints can become awkward when the same component appears in different parts of an application.

Same component, different containers

A card may be wide in the main content area but narrow inside a sidebar. A viewport breakpoint cannot directly know how much width that individual card receives.

Better component reuse

A reusable component can define its own layout behavior instead of depending on where the application places it.

Less coupling to page breakpoints

Component styles can react to local space without requiring every parent layout to expose matching viewport-specific rules.

What Is container-type in CSS?

The container-type property establishes the type of query container available to descendants. For size queries, the important values are inline-size and size.

</pre>

        <p class="mt-5">
          With <code class="text-blue-300">inline-size</code>, container queries can respond to
          the container's inline size. With <code class="text-blue-300">size</code>, the query
          can use both inline and block dimensions.
        </p>

        <div class="overflow-x-auto rounded-xl border border-slate-700 mt-6">
          <table class="w-full text-left text-sm">
            <thead class="bg-slate-800 text-white">
              <tr>
                <th class="px-4 py-3">Value</th>
                <th class="px-4 py-3">Meaning</th>
              </tr>
            </thead>

            <tbody class="divide-y divide-slate-700 bg-slate-900">
              <tr>
                <td class="px-4 py-3 font-mono text-blue-300">normal</td>
                <td class="px-4 py-3">
                  The default. The element is not a size query container, but it can still be
                  used for supported style queries and name-only queries.
                </td>
              </tr>

              <tr>
                <td class="px-4 py-3 font-mono text-blue-300">inline-size</td>
                <td class="px-4 py-3">
                  Enables queries based on the container's inline size.
                </td>
              </tr>

              <tr>
                <td class="px-4 py-3 font-mono text-blue-300">size</td>
                <td class="px-4 py-3">
                  Enables queries based on both inline and block dimensions.
                </td>
              </tr>
            </tbody>
          </table>
        </div>

        <p class="mt-5">
          Declaring a size container also applies containment needed to make container size
          queries predictable and avoid circular sizing dependencies. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/container-type))
        </p>
      </section>

      <!-- Basic Query -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Write a Basic CSS Container Query?
        </h2>

        <p class="mb-4">
          First, declare the containing element as a size query container:
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.card-container {
  container-type: inline-size;
}

Then define an @container rule:

= 500px) {
  .card {
    display: grid;
    grid-template-columns: 160px 1fr;
  }
}

The query applies to matching descendants when the selected container satisfies the condition. The default behavior uses the nearest eligible container when no explicit container name is specified. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40container))

What Does a Container Query Look Like in HTML?

<div class="card-container">
  <article class="card">
    <img src="course.jpg" alt="Course">

    <div class="card-content">
      <h2>CSS Course</h2>
      <p>
        Learn practical CSS concepts.
      </p>
    </div>
  </article>
</div>
= 500px) {
  .card {
    display: grid;
    grid-template-columns: 160px 1fr;
  }
}

The same card component can now switch from a stacked layout to a horizontal layout based on the width of its container.

What Is container-name in CSS?

The container-name property assigns one or more names to a query container. A named container can then be specifically targeted from an @container rule.

</pre>

        <p class="mt-5">
          The named container can then be queried:
        </p>

        <pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:@container sidebar (width >= 400px) {
  .card {
    display: grid;
    grid-template-columns: 120px 1fr;
  }
}

Naming becomes particularly useful when a component contains multiple nested query containers and you need precise control over which ancestor should be evaluated.

What Is the CSS container Shorthand?

Instead of writing container-name and container-type separately, you can use the container shorthand.

</pre>

        <p class="mt-5">
          This is equivalent to:
        </p>

        <pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.sidebar {
  container-name: sidebar;
  container-type: inline-size;
}

The shorthand can be convenient in component CSS when both the name and container type are known.

Which Container Does an @container Rule Query?

When no container name is specified, a size query evaluates against the nearest ancestor that is an eligible query container for the features used in the query.

= 600px) {
  .card {
    ...
  }
}

In this example, the card's query is evaluated against the nearest eligible container, which may be .inner rather than .outer.

This is one reason naming important containers can make larger component systems easier to reason about. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40container))

How Do Named Container Queries Help with Nested Components?

= 350px) {
  .card {
    grid-template-columns: 100px 1fr;
  }
}

The name tells CSS which query container should be considered, rather than relying on the nearest eligible ancestor alone.

How Do You Build a Responsive Card with Container Queries?

A responsive card is one of the best examples of where container queries solve a practical component-reuse problem.

<article class="card">
  <div class="card-media">
    <img src="course.jpg" alt="CSS Course">
  </div>

  <div class="card-body">
    <h2>Learn CSS</h2>
    <p>
      Build responsive interfaces with modern CSS.
    </p>

    <a href="#" class="button">
      Read More
    </a>
  </div>
</article>
= 520px) {
  .card {
    display: grid;
    grid-template-columns: 180px minmax(0, 1fr);
  }

  .card-media img {
    height: 100%;
    aspect-ratio: auto;
  }

  .card-body {
    padding: 1.5rem;
  }
}

The component controls its own layout based on the space assigned to it. This makes the component easier to reuse in different parent layouts.

What Are Container Query Length Units?

CSS provides special length units that are relative to a query container. These units can make component dimensions respond fluidly to the container itself.

Unit Relative To
cqw 1% of the query container's width.
cqh 1% of the query container's height.
cqi 1% of the query container's inline size.
cqb 1% of the query container's block size.
cqmin 1% of the smaller of the inline and block sizes.
cqmax 1% of the larger of the inline and block sizes.

Container units are useful when a component needs fluid values based on its own available space rather than the viewport. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Values_and_units/Numeric_data_types))

How Do You Use cqi in a Responsive Component?

The cqi unit is useful when spacing or typography should scale according to the container's inline size.

</pre>

        <p class="mt-5">
          Because these values respond to the query container, the same component can have
          different visual proportions when placed in containers of different widths.
        </p>
      </section>

      <!-- Container Units vs Viewport Units -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Is the Difference Between Container Units and Viewport Units?
        </h2>

        <div class="grid md:grid-cols-2 gap-6">

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-6">
            <h3 class="text-lg font-semibold text-white mb-3">
              Viewport Unit
            </h3>

            <pre class="bg-slate-950 border border-slate-700 rounded-xl p-4 overflow-x-auto text-sm"><code:title {
  font-size: 5vw;
}

The value responds to the viewport width.

Container Unit

</pre>

            <p class="mt-4 text-sm">
              The value responds to the inline size of the query container.
            </p>
          </div>

        </div>

        <p class="mt-5">
          This distinction is important for reusable components. A component can remain small
          even on a wide screen when it lives inside a narrow container, so viewport units may
          not describe the component's actual available space.
        </p>
      </section>

      <!-- Container Type Size -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          When Should You Use container-type: size Instead of inline-size?
        </h2>

        <p class="mb-4">
          Use <code class="text-blue-300">inline-size</code> when the component primarily needs
          to react to available inline space, which is common for cards and horizontal layout changes.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.component {
  container-type: inline-size;
}

Use size when the query needs to consider both dimensions of the container.

</pre>

        <p class="mt-5">
          Size queries can evaluate conditions involving dimensions such as width, height,
          orientation, and aspect ratio. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_queries))
        </p>
      </section>

      <!-- Orientation -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          Can Container Queries Test Orientation and Aspect Ratio?
        </h2>

        <p class="mb-4">
          Yes. Size container queries can test dimensions and related features such as orientation
          and aspect ratio.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.widget {
  container-type: size;
}

@container (orientation: landscape) {
  .widget-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

This can be useful when a component's internal arrangement should depend on the shape of the container rather than the global viewport.

What Are CSS Container Style Queries?

Container queries are not limited to size. CSS also supports style queries, which can evaluate supported style features of a containing element.

Current support for style() container queries is centered on CSS custom properties. For example:

</pre>

        <p class="mt-5">
          This allows a component to respond to a custom-property-based theme or state established
          by its container without requiring a separate class on the child.
          ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_size_and_style_queries))
        </p>
      </section>

      <!-- Named-only -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Are Name-Only Container Queries?
        </h2>

        <p class="mb-4">
          A name-only container query checks for the presence of a specific
          <code class="text-blue-300">container-name</code> without providing a size condition.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.article {
  container-name: featured;
}

@container featured {
  .badge {
    display: inline-flex;
  }
}

This can selectively target descendants based on their ancestor's assigned container name. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/container-name))

Can Container Queries Respond to Scroll State?

Modern CSS also defines scroll-state container queries, which can allow descendant styles to respond to scroll-related conditions of a query container or its scrolling context.

</pre>

        <p class="mt-5">
          Scroll-state queries are a newer part of the container query family and should be evaluated
          separately from the more established size-query pattern. For most responsive component
          layouts, size queries remain the primary use case. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_size_and_style_queries))
        </p>
      </section>

      <!-- Fallback -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Should You Do When Container Queries Are Not Available?
        </h2>

        <p class="mb-4">
          Container queries are widely available in modern browsers, but applications can still
          consider fallback strategies when supporting environments that do not implement the
          required feature.
        </p>

        <p class="mb-4">
          A common fallback is to use a flexible layout that works reasonably well without the
          container query and then progressively enhance the component when container queries are available.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.card {
  display: grid;
  grid-template-columns: 1fr;
}

@supports (container-type: inline-size) {
  .card-wrapper {
    container-type: inline-size;
  }

  @container (width >= 500px) {
    .card {
      grid-template-columns: 160px 1fr;
    }
  }
}

Progressive enhancement lets the component remain usable even if the more advanced responsive behavior is not available.

Can Container Queries Be Nested?

Yes. Components can contain multiple query containers, and container queries can be nested. However, a single container query does not directly compare multiple containers at once.

 400px) {
  .card {
    ...
  }
}

@container sidebar (width > 400px) {
  @container page (width > 1000px) {
    .card {
      ...
    }
  }
}

Nested queries can be used when a component needs to account for multiple levels of container context. Keep such structures understandable because excessive nesting can make responsive behavior harder to maintain.

Why Are Container Queries Useful for Component Libraries?

Consider a reusable card component used in three different places:

Main Content

The card receives a large amount of horizontal space.

Sidebar

The same card receives much less width.

Modal

The card may receive a medium-sized container.

A viewport-based media query gives all three cards the same breakpoint because they share the same viewport. A container query can let each card decide its layout based on the actual space it receives.

How Can Container Queries Be Used in a Dashboard?

Dashboards are a strong use case because widgets often appear in containers of very different widths.

= 420px) {
  .widget-content {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 1rem;
  }
}

A widget can therefore switch its internal arrangement depending on its own grid cell rather than on the width of the entire dashboard.

Can Container Queries Replace Media Queries?

No. They complement each other.

Use Media Queries For Use Container Queries For
Page-level navigation changes Component-level layout changes
Viewport-dependent typography Component typography based on available container space
Global responsive page structure Reusable cards, widgets, panels, and modules
User preferences such as color scheme Local component size or supported container state

A well-structured application often uses media queries for global page behavior and container queries for reusable components.

What Are Common CSS Container Query Mistakes?

Forgetting container-type

A size query needs an eligible size query container. If you write an @container width query without establishing the appropriate container type, the expected container may not exist.

Querying the wrong ancestor

Without a container name, the nearest eligible container is used. Nested component systems can therefore produce unexpected results if you forget which ancestor is being queried.

Using viewport units for local component sizing

A component inside a narrow sidebar can still be affected by the entire viewport when using vw-based sizing. Container units can better represent the component's actual space.

Creating unnecessary containers

Establish container contexts where they solve a real component-layout problem. Do not add container declarations everywhere without a reason.

Ignoring containment behavior

Size containment affects how an element gets its size from its contents. A query container therefore needs an explicit or contextual size that remains appropriate for the layout.

Making every component depend on complex queries

Container queries are powerful, but excessive conditional layouts can become difficult to maintain. Keep the component's responsive states clear and purposeful.

Why Can a Container Collapse Unexpectedly?

Size containment changes how the container derives its size from its contents. If a size container does not receive a contextual or explicit size where one is needed, its size can behave differently from a normal element.

</pre>

        <p class="mt-5">
          Block-level elements that naturally stretch across the available inline size usually
          have a contextual inline size, which makes this pattern straightforward for many card
          components. More specialized layouts should be checked carefully when the container's
          size depends heavily on intrinsic content.
        </p>

        <p class="mt-4">
          The reason for this behavior is closely related to the containment required to prevent
          circular size-query dependencies. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/container-type))
        </p>
      </section>

      <!-- Best Practices -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Are the Best Practices for CSS Container Queries?
        </h2>

        <div class="grid md:grid-cols-2 gap-5">

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Use them for reusable components
            </h3>
            <p>
              They are especially valuable when the same component can appear in differently sized containers.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Prefer inline-size when width is what matters
            </h3>
            <p>
              Most card and widget layouts primarily need to react to their available inline space.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Name important containers
            </h3>
            <p>
              Explicit names make nested component systems easier to reason about.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Keep media queries for global concerns
            </h3>
            <p>
              Viewport-based navigation and page-level structure may still belong in media queries.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Use container units when fluid local sizing helps
            </h3>
            <p>
              Values such as cqi can make spacing and typography respond to the component's own size.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Keep a sensible fallback
            </h3>
            <p>
              Make the component useful without relying entirely on advanced responsive behavior.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Test components in multiple parents
            </h3>
            <p>
              A component should be tested in wide, narrow, nested, and constrained containers.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Keep query thresholds meaningful
            </h3>
            <p>
              Add states because the component needs them, not because every small width difference requires a new rule.
            </p>
          </div>

        </div>
      </section>

      <!-- Debugging -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Debug CSS Container Queries?
        </h2>

        <div class="space-y-4">

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              1. Check container-type
            </h3>

            <p>
              Verify that the expected ancestor is actually a size query container when the query depends on size.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              2. Check container-name
            </h3>

            <p>
              If the query is named, confirm that the correct ancestor has the exact matching name.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              3. Inspect the actual container size
            </h3>

            <p>
              The component may be narrower or wider than the viewport and therefore may not match the expected condition.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              4. Check the nearest query container
            </h3>

            <p>
              A nested container can be the one actually being evaluated instead of the outer page container.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              5. Check the cascade
            </h3>

            <p>
              A matching container query can still interact with specificity, cascade layers, and source order.
            </p>
          </div>

        </div>
      </section>

      <!-- Complete Example -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Is a Complete CSS Container Query Example?
        </h2>

        <p class="mb-5">
          The following example creates a reusable course card that changes layout based on
          the width of its own container.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code><section class="course-list">

  <article class="course-card">
    <div class="course-image">
      <img
        src="course.jpg"
        alt="CSS course"
      >
    </div>

    <div class="course-content">
      <span class="course-tag">CSS</span>

      <h2>
        Responsive CSS
      </h2>

      <p>
        Learn modern responsive layout techniques.
      </p>

      <a href="#" class="course-link">
        Learn More
      </a>
    </div>
  </article>

</section>
= 520px) {
  .course-card {
    display: grid;
    grid-template-columns:
      180px minmax(0, 1fr);
  }

  .course-image img {
    width: 100%;
    height: 100%;
    aspect-ratio: auto;
  }

  .course-content {
    padding: 1.5rem;
  }
}

The outer course list controls how many cards appear across the page. Each card then controls its own internal layout through a container query. This separation makes the component independent of the page-level breakpoint strategy.

How Do Media Queries and Container Queries Work Together?

A common production pattern is to use media queries for the page and container queries for reusable components.

= 900px) {
  .page {
    display: grid;
    grid-template-columns:
      240px minmax(0, 1fr);
  }
}

.card-wrapper {
  container-type: inline-size;
}

.card {
  display: block;
}

@container (width >= 500px) {
  .card {
    display: grid;
    grid-template-columns:
      160px minmax(0, 1fr);
  }
}

The media query decides how the overall page is structured. The container query decides how the card behaves inside whatever space the page gives it.

Key Takeaways

  • CSS container queries let descendants respond to the size or other supported features of a containing element.
  • They are different from media queries, which commonly respond to viewport or device characteristics.
  • container-type: inline-size is a common starting point for width-driven responsive components.
  • container-type: size enables queries based on both inline and block dimensions.
  • container-name lets you target a particular query container by name.
  • container is the shorthand for container name and type.
  • An unnamed size query normally evaluates the nearest eligible query container.
  • cqw, cqh, cqi, cqb, cqmin, and cqmax provide lengths relative to a query container.
  • Container style queries can currently use supported custom-property-based style features.
  • Modern CSS also defines scroll-state and other container query capabilities beyond size queries.
  • Container queries are particularly useful for reusable component libraries, dashboards, cards, widgets, and nested application layouts.
  • Media queries and container queries are complementary. Use each according to whether the responsive decision belongs to the page environment or the component's container.

Code Examples

Basic Container Query

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

.card {
  display: block;
}

@container (width >= 500px) {
  .card {
    display: grid;
    grid-template-columns: 160px 1fr;
  }
}
    

Named Container Query

css
.sidebar {
  container: sidebar / inline-size;
}

@container sidebar (width >= 400px) {
  .card {
    display: grid;
    grid-template-columns: 120px 1fr;
  }
}
    

Responsive Component with Container Units

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

.card {
  padding: clamp(1rem, 4cqi, 2rem);
}

.card h2 {
  font-size: clamp(1.25rem, 3cqi, 2rem);
}
    

Container Query with Media Query

css
.page {
  display: block;
}

@media (width >= 900px) {
  .page {
    display: grid;
    grid-template-columns:
      240px minmax(0, 1fr);
  }
}

.card-wrapper {
  container-type: inline-size;
}

.card {
  display: block;
}

@container (width >= 500px) {
  .card {
    display: grid;
    grid-template-columns:
      160px minmax(0, 1fr);
  }
}
    

Reusable Responsive Card

html
<article class="card">
  <div class="media">
    <img
      src="course.jpg"
      alt="CSS course"
    >
  </div>

  <div class="content">
    <span class="tag">CSS</span>

    <h2>Responsive CSS</h2>

    <p>
      Learn modern responsive layout techniques.
    </p>

    <a href="#">Learn More</a>
  </div>
</article>

<style>
.card {
  container: card / inline-size;

  display: block;
  overflow: hidden;

  border: 1px solid #334155;
  border-radius: 1rem;
  background: #0f172a;
}

.media img {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

.content {
  padding: 1rem;
}

.tag {
  display: inline-block;
  margin-bottom: 0.75rem;
}

a {
  display: inline-flex;
  margin-top: 1rem;
}

@container card (width >= 520px) {
  .card {
    display: grid;
    grid-template-columns:
      180px minmax(0, 1fr);
  }

  .media img {
    width: 100%;
    height: 100%;
    aspect-ratio: auto;
  }

  .content {
    padding: 1.5rem;
  }
}
</style>
    

Pro Tip

For reusable components, stop asking “What breakpoint is the viewport at?” and ask “How much space does this component actually have?” If the answer depends on the parent container, a container query is usually a better fit than another viewport breakpoint.

More CSS lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.