Advanced Responsive Patterns: Mastering Container Queries
Master advanced responsive patterns with Tailwind CSS. Learn to use container queries and orientation logic to build truly modular, fluid UI components.

Previously in this course, we covered Implementing Responsive Modifiers and Fluid Feature Grids. While those lessons focused on global viewport breakpoints (e.g., md:, lg:), today we’re moving beyond the screen size to focus on the component itself.
In professional UI development, a component shouldn't care how wide the browser window is; it should care how much space it has in its current context. This is the power of container queries.
Understanding Container Queries vs. Viewport Breakpoints
Standard responsive design relies on the viewport width. If you put a sidebar component inside a main content area, it might look fine on a desktop but break on a mobile device because the viewport is small.
Container queries allow a component to change its style based on the width of its parent element. This is essential for building truly modular design systems.
| Feature | Viewport Breakpoint (md:, lg:) | Container Query (@sm:, @md:) |
|---|---|---|
| Reference | Browser Window | Parent Container |
| Use Case | Global Layouts | Modular Components |
| Flexibility | Rigid | Highly Fluid |
Implementing Container Queries in Tailwind
To use container queries, you must first define a "container" using the @container utility. Then, you can apply styles using @ prefixes.
Step 1: Define the Container
You need to tell Tailwind which element acts as the container for your component.
HTMLstyle="color:#808080"><style="color:#4EC9B0">div class="@container"> <!-- Your component goes here --> style="color:#808080"></style="color:#4EC9B0">div>
Step 2: Use Container Modifiers
Once defined, you can use responsive modifiers like @md:, @lg:, etc., which react to the container's width rather than the viewport.
HTMLstyle="color:#808080"><style="color:#4EC9B0">div class="@container"> style="color:#808080"><style="color:#4EC9B0">div class="grid grid-cols-1 @md:grid-cols-2"> style="color:#808080"><style="color:#4EC9B0">div>I am a cardstyle="color:#808080"></style="color:#4EC9B0">div> style="color:#808080"><style="color:#4EC9B0">div>I am a cardstyle="color:#808080"></style="color:#4EC9B0">div> style="color:#808080"></style="color:#4EC9B0">div> style="color:#808080"></style="color:#4EC9B0">div>
In the example above, the grid will switch to two columns as soon as the parent container exceeds the medium threshold, regardless of whether the user is on a phone or a desktop.
Handling Orientation Changes
Sometimes, your layout needs to adapt to device orientation (landscape vs. portrait). While media queries handle this globally, you can handle it at the component level by combining standard responsive modifiers with @media logic in your config or by using CSS-in-JS patterns.
For most cases in Tailwind, you can use the portrait and landscape variants if they are enabled, but a common trick for components is checking for aspect ratios:
HTMLstyle="color:#808080"><style="color:#4EC9B0">div class="aspect-portrait:flex-col aspect-landscape:flex-row flex"> <!-- This component shifts direction based on its own aspect ratio --> style="color:#808080"></style="color:#4EC9B0">div>
Hands-on Exercise: Building a Responsive Profile Card
Let’s advance our project. We will build a profile card that adapts its layout based on its container size.
- Create a
divwith the@containerclass. - Inside, create a card with a
flex-collayout by default. - Use the
@lg:flex-rowmodifier to change it to a horizontal layout when the container is wide enough.
HTMLstyle="color:#808080"><style="color:#4EC9B0">div class="@container w-full max-w-2xl border p-4"> style="color:#808080"><style="color:#4EC9B0">div class="flex flex-col @lg:flex-row gap-4"> style="color:#808080"><style="color:#4EC9B0">img src="avatar.jpg" class="w-20 h-20 rounded-full" alt="User avatar"> style="color:#808080"><style="color:#4EC9B0">div> style="color:#808080"><style="color:#4EC9B0">h2 class="text-xl">John Doestyle="color:#808080"></style="color:#4EC9B0">h2> style="color:#808080"><style="color:#4EC9B0">p>This component adapts to its container size!style="color:#808080"></style="color:#4EC9B0">p> style="color:#808080"></style="color:#4EC9B0">div> style="color:#808080"></style="color:#4EC9B0">div> style="color:#808080"></style="color:#4EC9B0">div>
Common Pitfalls
- Forgetting the
@containerclass: If you don't add this to the parent, your@lg:modifiers will do nothing. - Over-nesting: Avoid creating too many container levels; it can make debugging layout shifts difficult.
- Assuming Viewport = Container: Always double-check if you actually need a container query or if a standard breakpoint suffices. Use container queries for reusable components (cards, sidebars), use breakpoints for page-level layouts.
FAQ
Q: Are container queries supported in all browsers? A: Yes, modern browser support for CSS Container Queries is excellent across all evergreen browsers.
Q: Can I use custom container sizes?
A: Yes, you can extend your tailwind.config.js to define specific container breakpoints if the defaults don't match your design system.
Q: Does this replace Flexbox/Grid? A: Not at all. Container queries are a layout constraint tool that you use in tandem with Flexbox and Grid to manage how those layouts respond to available space.
Recap
We’ve learned that advanced responsiveness isn't just about the screen size—it's about the component context. By using @container, we can create modular, resilient UI elements that behave correctly regardless of where they are placed in the DOM.
Up next: Building a Card Component
Work with me

Next.js Website & Landing Page Development
A blazing-fast, SEO-optimized website or landing page in Next.js — the kind that loads instantly and ranks. Design-to-code, done right.

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.
