🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Styling••By Komal Nakrani

Astro Container Queries: Components That Fit Any Column

Use Astro container queries to make components respond to their own width: container-type in scoped styles, @container rules, named containers and Tailwind 4 syntax.

#Astro#Container Queries#CSS#Responsive Design

Astro container queries need no setup: give a wrapper element container-type: inline-size in a component’s <style> block, then use @container (min-width: ...) rules to change the layout based on that wrapper’s width instead of the viewport. The same card can then sit in a sidebar or a full-width grid and lay itself out correctly in both.

Key Takeaways

  • Container queries are plain CSS and work in Astro’s scoped styles as written.
  • The container must be an ancestor of the elements you style, not the element itself.
  • Use container-name when containers nest, so a rule targets the right one.
  • Tailwind 4 includes container queries: @container on the parent and @md: style variants on children.

A card that adapts to its column

---
// src/components/ThemeCard.astro
interface Props {
  title: string;
  image: string;
  href: string;
}
const { title, image, href } = Astro.props;
---
<div class="card-wrap">
  <a class="card" href={href}>
    <img src={image} alt="" width="640" height="400" loading="lazy" />
    <div class="body">
      <h3>{title}</h3>
      <slot />
    </div>
  </a>
</div>

<style>
  .card-wrap {
    container-type: inline-size;
  }
  .card {
    display: grid;
    gap: 1rem;
  }
  .card img {
    width: 100%;
    height: auto;
    border-radius: 0.5rem;
  }
  @container (min-width: 32rem) {
    .card {
      grid-template-columns: 40% 1fr;
      align-items: center;
    }
  }
</style>

Below 32rem of available width the card stacks; above it, image and text sit side by side. The outer .card-wrap exists because an element cannot query itself.

Named containers

When containers nest, an unnamed @container rule matches the nearest one. Name them to be explicit:

/* src/components/Sidebar.astro <style> */
.sidebar {
  container: sidebar / inline-size;
}

@container sidebar (max-width: 18rem) {
  .nav-label { display: none; }
}

container: sidebar / inline-size is shorthand for container-name and container-type.

Container query units

Inside a container, cqi is 1% of the container’s inline size. It is handy for fluid type that scales with the component rather than the screen:

.card h3 {
  font-size: clamp(1rem, 4cqi, 1.5rem);
}

With Tailwind CSS 4

Tailwind 4 includes container query utilities with no plugin. Mark the parent with @container and use size variants on children:

<div class="@container">
  <div class="grid gap-4 @md:grid-cols-[40%_1fr]">
    ...
  </div>
</div>

On Tailwind 3, install @tailwindcss/container-queries for the same syntax. Our Tailwind CSS v4 tip covers setup.

Gotchas

  • container-type: inline-size applies size containment on the inline axis, so the container no longer sizes to its content’s width. Give it a width from its parent (a grid cell or block element usually does).
  • Scoped styles add a data attribute to selectors, but @container conditions are unaffected; queries work the same as in plain CSS.
  • Container queries are supported in all current major browsers. See MDN’s container queries guide for details.

For where these fit in a wider styling approach, see our Astro styling guide and the theme styling docs. Card grids in our Astro themes are a good place to try this pattern.

FAQ

Do container queries work in Astro scoped styles?

Yes. Astro scopes selectors by adding an attribute, and @container rules pass through unchanged. Write them exactly as you would in a plain CSS file.

Why is my container query not applying?

Usually the container-type is on the element you are trying to style rather than an ancestor, or no ancestor has a container-type at all. Add a wrapper element and set the container on it.

Should I use container queries or media queries in Astro?

Use container queries for reusable components that appear in different layouts, such as cards and widgets. Use media queries for page-level layout, such as switching the main grid on wide screens.