Sub topic 10
Customizing container sizes in the theme
This chapter assumes you know how to mark an element with @container, how to use variants such as @md and @lg, and how the default container size scale works. You should also be comfortable with Tailwind utility classes and with basic CSS.
Why customize the scale
Tailwind ships a default scale of container sizes, from @3xs up to @7xl. These sizes cover most layouts. Sometimes they don't fit:
- Your design system uses breakpoints that fall between the defaults.
- A component needs a wider step than
@7xl. - You want names that match your own vocabulary, such as
@cardor@wide.
In Tailwind v4, the container sizes are theme variables. You change the scale in CSS, not in a JavaScript config file.
How container sizes are defined
Each size in the scale comes from a theme variable whose name starts with --container-. The part after the prefix becomes the variant name. For example, --container-md provides the @md variant, and its value is the minimum container width at which the variant applies.
Adding a new variable adds a new size to the scale. The variable is declared inside an @theme block.
@import "tailwindcss";
@theme {
--container-8xl: 96rem;
}After this change, the variant @8xl exists. It applies when the nearest @container ancestor is at least 96rem wide. Because it is a regular container variant, it also works in its max and range forms, such as @max-8xl and @min-md:@max-8xl.
<div class="@container">
<div class="grid grid-cols-1 @3xl:grid-cols-2 @8xl:grid-cols-4">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
</div>
</div>The flow from theme to variant
The diagram shows how a theme variable becomes a working variant.
Choosing your own names
You are not limited to the existing naming style. Any name after --container- works, as long as it forms a valid class name.
@import "tailwindcss";
@theme {
--container-card: 28rem;
--container-wide: 64rem;
}You can then write @card:flex-row and @wide:grid-cols-3. Pick names that describe a purpose rather than a pixel size. A name like @card stays correct if you later change the value from 28rem to 30rem.
Keep in mind that the variants are ordered by the size of their values. Tailwind sorts them so that wider variants override narrower ones. If two names have the same value, the result is hard to predict, so give each size a distinct value.
Replacing instead of extending
By default, adding a variable extends the scale. If you want only your own sizes, you can clear the default container variables first. Setting a namespace to initial removes every variable that starts with that prefix.
@import "tailwindcss";
@theme {
--container-*: initial;
--container-small: 24rem;
--container-large: 60rem;
}With this setup, @md and @lg no longer exist. Using them in a class produces no CSS, and Tailwind gives no error. Reset the scale only if your project controls all of its markup, or search for old variants first.
Common errors
- Using the variant before declaring it. Writing
@8xl:flexwithout a matching--container-8xlgenerates nothing. The element simply never changes. - Putting the variable outside
@theme. A plain:root { --container-8xl: 96rem; }is an ordinary CSS variable. It does not create a variant. Use@theme. - Forgetting the
@containerancestor. A new size still needs a marked parent. Without@container, no variant responds. - Using v3 syntax in v4. The
theme.extendobject intailwind.config.jsbelongs to the older workflow. In v4, declare the sizes in CSS.
One-off sizes
If you need a size in only one place, an arbitrary value is simpler than a theme variable. For example, @min-[475px]:flex-row sets a breakpoint for a single element. Use a theme variable when the size repeats across components or has a meaning in your design.
| Situation | Better choice |
|---|---|
| A size used once | Arbitrary value such as `@min-[475px]` |
| A size used in many components | Theme variable such as `--container-card` |
| A full set of project-specific sizes | Reset with `--container-*: initial` |
Exercise
You add --container-card: 28rem; inside @theme, but @card:flex-row has no effect on an element. Name two things to check.
Show answer
First, confirm the variable is declared inside an @theme block and not in :root, since only @theme creates variants. Second, confirm the element has an ancestor with the @container class, because container variants respond only to a marked container. Also check that the container is at least 28rem wide.
Exercise
What happens to the @lg variant if you write --container-*: initial; in @theme and then define only --container-small and --container-large?
Show answer
The @lg variant no longer exists, so classes that use it generate no CSS and have no effect. Only @small and @large are available.