Sub topic 8
Arbitrary container sizes
The default container scale (@sm, @md, @lg and so on) covers most layouts. Sometimes a design needs a switch point that isn't on the scale, such as "go horizontal at 475px". This chapter covers arbitrary container variants, which let you write that value directly in a class name.
This chapter assumes you know:
- How to mark an element with
@containerand style children with@md:style variants. - The default container size scale.
- The
@min-*and@max-*variants. - Tailwind's square-bracket arbitrary value syntax, such as
w-[137px].
What an arbitrary container variant is
A named variant like @md: maps to a size in the theme. An arbitrary variant puts the size in square brackets, so no theme entry is needed:
<div class="@container">
<div class="flex flex-col @min-[475px]:flex-row">
<div>Left</div>
<div>Right</div>
</div>
</div>The children stack in a column. When the nearest @container ancestor is at least 475px wide, they switch to a row. The viewport width does not matter.
Arbitrary variants come in two forms, matching the named ones:
| Class prefix | Applies when the container is | Named equivalent |
|---|---|---|
| `@min-[475px]:` | at least 475px wide | `@min-md:` or `@md:` |
| `@max-[475px]:` | narrower than 475px | `@max-md:` |
| `@[475px]:` | at least 475px wide (shorthand for `@min-[475px]:`) | `@md:` |
The explicit @min-[...] and @max-[...] forms are easier to read in a code review, so this chapter uses them. The @[475px]: shorthand is the same as @min-[475px]:.
The generated CSS
Tailwind turns the class into a CSS container query. The output looks roughly like this (the exact condition syntax depends on the Tailwind version):
@container (width >= 475px) {
.\@min-\[475px\]\:flex-row {
flex-direction: row;
}
}Two points follow from this:
- The condition is evaluated against the nearest ancestor with
container-typeset. The@containerclass setscontainer-type: inline-size, so only width can be queried. - If no ancestor is a container, the query matches nothing and the class has no effect. Nothing warns you about this. The pitfalls chapter covers it in depth, but check for it first when an arbitrary variant "does nothing".
Choosing a unit
The value in brackets is a CSS length. You can use px, rem, or other absolute and root-relative units:
<div class="@container">
<div class="grid grid-cols-1 @min-[30rem]:grid-cols-2 @min-[60rem]:grid-cols-3 gap-4">
<div class="rounded bg-white p-4 shadow">A</div>
<div class="rounded bg-white p-4 shadow">B</div>
<div class="rounded bg-white p-4 shadow">C</div>
</div>
</div>Prefer rem for layout thresholds. A rem is relative to the root font size, so if a user raises their browser's default font size, your breakpoints shift with the text. A layout tuned to 475px of container width can feel cramped when text is 20% larger. 30rem scales with it.
Use px when the threshold is tied to a fixed asset, such as an image that is 320px wide and needs 160px of text beside it.
Avoid em. In container queries, em is resolved against the container's own font size. It is easy to get a surprising value if the container has a custom text-* class.
For reference, here is where some common values fall on the default v4 scale:
| Value | Rem | Closest named variant |
|---|---|---|
| 384px | 24rem | `@sm` (exactly) |
| 448px | 28rem | `@md` (exactly) |
| 475px | about 29.7rem | between `@md` and `@lg` |
| 512px | 32rem | `@lg` (exactly) |
If your arbitrary value is within a few pixels of a named size, use the named variant instead. A value like @min-[450px] next to @md (448px) is a source of confusion for the next reader.
What gets measured
A container query measures the container's content box, which excludes padding and border. Consider this markup:
<div class="@container p-8">
<div class="flex flex-col @min-[475px]:flex-row">
<div>Left</div>
<div>Right</div>
</div>
</div>Here p-8 adds 2rem of padding on each side, 4rem (64px) in total. The row layout starts only when the container is at least 475px + 64px = 539px wide overall. If you compute breakpoints from the visible outer width, you will be off by the padding.
A reliable pattern is to keep the @container element free of padding and put the padding on a child:
<div class="@container">
<div class="p-8">
<div class="flex flex-col @min-[475px]:flex-row">
<div>Left</div>
<div>Right</div>
</div>
</div>
</div>Now the query measures the full width of the outer element.
Combining arbitrary variants into ranges
Arbitrary variants stack the same way named ones do. This element is visible only when the container is between 400px and 700px wide:
<div class="@container">
<div class="hidden @min-[400px]:@max-[700px]:block">
Shown between 400px and 700px
</div>
</div>Both conditions must hold. You can also mix arbitrary and named variants:
<div class="@container">
<div class="grid grid-cols-1 @min-md:@max-[700px]:grid-cols-2 @min-[700px]:grid-cols-3 gap-4">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
</div>The layout has three states:
- Below
@md(448px), the grid has one column. - From 448px up to 700px, it has two columns.
- From 700px up, it has three columns.
Note the boundary. @max-[700px] is true when the container is narrower than 700px, and @min-[700px] is true from 700px up. Using the same number in both keeps the ranges from overlapping or leaving a gap.
If two variants both set the same property and both match, source order in the generated CSS decides. The range version above avoids that by making the conditions mutually exclusive.
Naming the container
When containers nest, a variant targets the nearest ancestor container by default. Add a name after a slash to target a specific one. The slash goes on the container and again on the variant, after the bracketed value:
<div class="@container/main">
<aside class="@container/sidebar">
<div class="flex flex-col @min-[475px]/main:flex-row @min-[20rem]/sidebar:gap-8">
<div>A</div>
<div>B</div>
</div>
</aside>
</div>Here @min-[475px]/main: is evaluated against the outer element, even though the sidebar is closer. @min-[20rem]/sidebar: is evaluated against the sidebar. The generated CSS includes the container name in the at-rule, for example @container main (width >= 475px).
The name has to match exactly. A variant that asks for /mian will not match anything, and again there is no error.
Writing arbitrary values correctly
Tailwind finds class names by scanning your source files as plain text. That has three consequences for arbitrary container values.
- The full class name must appear in the source. This does not work:
``html <div class="@container"> <!-- Broken: Tailwind cannot see a complete class name --> <div class="@min-[${width}px]:flex-row">...</div> </div> ``
If the width comes from a variable, Tailwind never generates the CSS. Write each full class out, or use a lookup object that contains complete strings such as "@min-[475px]:flex-row".
- No spaces inside the brackets. A class name cannot contain a space. If a value needs one, write an underscore and Tailwind converts it. Plain lengths like
475pxnever need this.
- Escape nothing yourself. Write
@min-[475px]:flex-rowin HTML exactly as shown. Tailwind escapes the characters in the generated selector.
When to use arbitrary values and when to use the theme
Arbitrary values are meant for one-off cases. Repeating them is a sign the value belongs in the theme.
@import "tailwindcss";
@theme {
--container-card: 29.7rem;
}With that definition, @card: and @min-card: become available variants. In Tailwind v4, a --container-* variable also generates a matching max-w-card utility, so only add one when a width would make sense in both places.
The flowchart summarizes the decision.
Tailwind v3
In Tailwind v3, container query variants come from the official @tailwindcss/container-queries plugin. The plugin supports arbitrary values in the form @[475px]:. It is a good idea to check the plugin's documentation for the @min-[...] and @max-[...] forms in your installed version. Tailwind v4 includes container query support in core, so no plugin is needed there.
Arbitrary values versus container query units
Do not confuse the variant condition with a length unit. Both use brackets, but they do different jobs:
@min-[475px]:flex-rowdecides when a style applies. The bracket holds a threshold.w-[50cqw]sets a size. The bracket holds a length in container query units, where1cqwis 1% of the container's width.
You can use both on the same element, because they are independent:
<div class="@container">
<div class="flex flex-col @min-[475px]:flex-row">
<div class="w-full @min-[475px]:w-[40cqw]">Image</div>
<div class="flex-1">Text</div>
</div>
</div>The image fills the width in column mode. Once the container reaches 475px, it takes 40% of the container's width.
Troubleshooting checklist
When an arbitrary container variant has no effect, check these in order:
- Is there an ancestor with
@container(or@container/name)? - If the variant is named, does the name match the container's name exactly?
- Does the full class appear literally in a file Tailwind scans?
- Is padding on the container shifting the measured width?
- Is another utility setting the same property later in the CSS and overriding it?
- Did you use
@[475px]:on a version where the shorthand isn't supported? Switch to@min-[475px]:.
Exercise
A card uses <div class="@container p-6"><div class="flex-col @min-[500px]:flex-row flex">...</div></div>. At what outer width of the @container element does the card switch to a row, assuming the default 4px spacing unit and no borders?
Show answer
The p-6 padding is 1.5rem (24px) per side, so 48px in total. Container queries measure the content box, so the row layout begins when the content width reaches 500px. The outer width is 500px + 48px = 548px. To make the query track the outer width, move the padding to a child element.
Exercise
Write the classes for a grid that has one column below 30rem, two columns from 30rem up to (but not including) 60rem, and four columns from 60rem up, using arbitrary container variants. Assume the grid's parent has @container.
Show answer
grid grid-cols-1 @min-[30rem]:@max-[60rem]:grid-cols-2 @min-[60rem]:grid-cols-4. The range variant @min-[30rem]:@max-[60rem]: applies from 30rem up to just below 60rem, and @min-[60rem]: takes over at exactly 60rem. Because the two conditions never overlap, source order does not matter.
Exercise
Why does <div class="@min-[${w}px]:flex-row"> fail to produce any CSS, and what is a safe alternative?
Show answer
Tailwind scans source files as plain text and never runs your code, so it cannot see the value of w and never finds a complete class name. A safe alternative is to write out complete class strings, for example a lookup object such as { narrow: "@min-[400px]:flex-row", wide: "@min-[600px]:flex-row" }, so each class appears literally in the source.