how do i do container queries in tailwind 4
- User
how do i do container queries in tailwind 4
Assistant
Container Queries in Tailwind CSS v4
Tailwind CSS v4.0 now includes built-in container query support, eliminating the need for the previously required
@tailwindcss/container-queriesplugin[8]. Container queries allow you to style elements based on their parent container size rather than the viewport width, making your components truly responsive regardless of where they're placed.Basic Implementation
Step 1: Mark an element as a container
Add the
@containerclass to the parent element you want to use as a reference for sizing:<div class="@container"> <!-- Child elements go here --> </div>This applies the necessary CSS to make the element a query container[1].
Step 2: Apply responsive styles using container breakpoints
Use container query variants on child elements using the
@prefix syntax:<div class="@container"> <p class="text-base @md:text-xl @lg:font-bold"> This text changes based on the container's size </p> </div>In this example, the text will be normal size by default, become larger (
text-xl) when the container reaches themdbreakpoint (28rem/448px), and become bold when it reaches thelgbreakpoint (32rem/512px)[1][3].Available Breakpoints
Tailwind v4 provides container breakpoints from
@xs(20rem/320px) to@7xl(80rem/1280px)[3]:Name Size @xs20rem (320px) @sm24rem (384px) @md28rem (448px) @lg32rem (512px) @xl36rem (576px) @2xl42rem (672px) @3xl48rem (768px) @4xl56rem (896px) @5xl64rem (1024px) @6xl72rem (1152px) @7xl80rem (1280px) Advanced Features
Max-width container queries
Use the
@max-*variant to apply styles when the container is smaller than a specific size:<div class="@container"> <div class="grid grid-cols-3 @max-md:grid-cols-1"> <!-- Content here --> </div> </div>This creates a 3-column grid by default, but switches to a single column when the container is narrower than the
mdbreakpoint[8].Combining min and max for ranges
Stack
@min-*and@max-*variants to target specific size ranges:<div class="@container"> <div class="flex @min-md:@max-xl:hidden"> <!-- This element is hidden only when container width is between md and xl --> </div> </div>Named containers
When working with multiple containers, you can name them for clearer targeting:
<div class="@container/sidebar"> <!-- Sidebar content --> </div> <div class="@container/main"> <!-- Main content --> <h1 class="text-2xl @lg/main:text-4xl">Heading</h1> </div>This ensures styles only apply based on the specific named container's size[1][3].
Arbitrary values
For precise control, use arbitrary values:
<div class="@container"> <div class="@[500px]:bg-red-100"> <!-- Red background when container is at least 500px wide --> </div> </div>Practical Example
Here's a card component that changes layout based on container width:
<div class="@container bg-white shadow rounded p-4"> <div class="flex flex-col @md:flex-row"> <div class="w-full @md:w-1/4"> <img class="w-full h-auto rounded-tl-2xl rounded-tr-2xl @md:rounded-tr-none rounded-bl-none @md:rounded-bl-2xl" src="product.jpg"/> </div> <div class="p-4"> <h2 class="text-xl font-bold">Product Title</h2> <p class="mt-2">Product description goes here...</p> </div> </div> </div>This card displays in a vertical layout by default (image above content), but switches to a horizontal layout (image beside content) when the container reaches the
mdbreakpoint[1].Container queries make your components truly self-contained and responsive to their immediate context, rather than just the overall viewport size.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.