patterncsstailwindTip
Container queries in Tailwind with @tailwindcss/container-queries
Viewed 0 times
@tailwindcss/container-queries; built-in in Tailwind v4
container queries@containercomponent responsivepluginlayout-aware
Problem
A reusable component needs to change layout based on its own container width, not the viewport. Standard Tailwind breakpoints are viewport-based and cause the component to behave incorrectly when placed in different layout contexts.
Solution
Install the container queries plugin and use @container and @lg/@md style variants:
Named containers:
npm install @tailwindcss/container-queriesplugins: [require('@tailwindcss/container-queries')]<div class="@container">
<div class="grid grid-cols-1 @lg:grid-cols-3 @md:grid-cols-2">
<div class="p-4 @sm:p-6">Card</div>
</div>
</div>Named containers:
<div class="@container/sidebar">
<div class="@lg/sidebar:hidden">Collapsed sidebar content</div>
</div>Why
CSS Container Queries allow elements to respond to the size of their containing block rather than the viewport, making components genuinely reusable across different layout positions.
Gotchas
- Browser support: container queries require Chrome 105+, Firefox 110+, Safari 16+. Check your target audience.
- The @container element itself cannot be styled based on its own size — only its descendants can.
- In Tailwind v4, container query support is built in without needing a separate plugin.
Context
When building reusable components that need to adapt to their container size, not viewport size.
Revisions (0)
No revisions yet.