Sub topic 8
Arbitrary container sizes
This chapter assumes you know flexbox and grid, Tailwind utility classes, and responsive variants such as md:. It also assumes you have already marked an element with @container and used variants like @md. Examples use Tailwind v4, where container queries are built in.
Why you need arbitrary sizes
The named container sizes (@sm, @md, @lg and so on) cover most layouts. Sometimes a component breaks at a width that none of them match. A product card might need to switch layouts at exactly 475px because that is where its text starts to wrap badly. Instead of adding a new size to your theme, you can write the size inline with square brackets.
The @min-[...] and @max-[...] syntax
An arbitrary container variant puts a length in square brackets after @min- or @max-:
@min-[475px]:flex-rowappliesflex-rowwhen the container is at least 475px wide.@max-[600px]:hiddenapplieshiddenwhen the container is narrower than 600px.
<div class="@container">
<div class="flex flex-col @min-[475px]:flex-row">
<div class="w-[50cqw]">Half of the container width</div>
<div>Content</div>
</div>
</div>The outer div is the container. The inner div stacks its children in a column by default. Once the container reaches 475px, the layout becomes a row. The viewport width does not matter here, only the width of the nearest @container ancestor.
The value can be any valid CSS length, including rem and em. If you want your breakpoints to follow the user's font size, use rem:
<div class="@container">
<div class="grid grid-cols-1 @min-[30rem]:grid-cols-2">
<div class="rounded bg-white p-4 shadow">One</div>
<div class="rounded bg-white p-4 shadow">Two</div>
</div>
</div>Here 30rem equals 480px at the default font size, but it grows if the user increases their browser's base font size.
Combining arbitrary and named sizes
Arbitrary values mix freely with the named scale. You can stack @min-* and @max-* to target a range, and either side can be arbitrary:
<div class="@container">
<div class="hidden @min-[400px]:@max-xl:block">
Visible from 400px up to, but not including, the xl container size
</div>
</div>The element is hidden by default. It becomes a block only when both conditions hold. The @max-xl side comes from the default scale, and the @min-[400px] side is arbitrary.
Container query units
Arbitrary values also let you use container query length units. The unit cqw is 1% of the container's width. In w-[50cqw] above, the element is half as wide as its container, whatever the viewport is doing. The container needs @container on an ancestor for these units to work. Without one, the units fall back to the viewport size, which is a common source of confusing results.
Arbitrary value or theme variable?
In Tailwind v4 you can do this with a theme variable:
@import "tailwindcss";
@theme {
--container-8xl: 96rem;
}After this, @8xl is available as a variant. Choose by how often you repeat the number.
| Situation | Better choice |
|---|---|
| One component, one odd breakpoint | Arbitrary value like `@min-[475px]` |
| Same size used across many components | Theme variable like `--container-8xl` |
| Size should scale with user font size | Either, written in `rem` |
How the variant is resolved
The diagram shows how a child decides which styles apply.
Common mistakes
- Forgetting the container. The variant needs an
@containerancestor. Without one, nothing matches and no error appears. - Putting the variant on the container itself. A container cannot respond to its own width. Put
@min-[475px]:on a child. - Spaces inside the brackets. Write
@min-[475px], not@min-[ 475px ]. Tailwind reads class names split by whitespace. - Missing units.
@min-[475]is not a valid length. Includepx,remor another unit.
Exercise
A card should stack its image and text in a column when its container is narrower than 520px, and place them side by side from 520px up. Write the class list for the inner flex element using an arbitrary value, and say what must be true of an ancestor for it to work.
Show answer
Use class="flex flex-col @min-[520px]:flex-row". An ancestor of this element must have the @container class. Without it, the variant has no container to measure and flex-row never applies. (flex flex-row @max-[519px]:flex-col is not an exact equivalent: @max-[520px] excludes 520px, so @min-[520px]:flex-row is the cleaner form.)
Exercise
You use @min-[475px]:grid-cols-2 in twelve components. What would you change, and how?
Show answer
Add a named size in the theme, for example --container-card: 29.6875rem; inside @theme, then use @card:grid-cols-2. The value is then defined once and can be changed in one place.