Sub topic 10
Customizing container sizes in the theme
This chapter assumes you can already mark an element with @container and style its children with variants such as @md: and @lg:. It also assumes you know the default container size scale and have seen Tailwind v4's @theme directive. Here you will change that scale: add new sizes, override existing ones, replace the whole scale, and know what else your change affects.
Why customize the scale
The default scale runs from @3xs (16rem) to @7xl (80rem). It suits many components, but you may need to change it in a few cases:
- Your design system has a card that switches layout at 30rem, and no default size matches.
- A very wide dashboard widget needs a size above
@7xl. - You want names that describe intent, such as
@compactand@wide, instead of t-shirt sizes. - You want to drop sizes your team never uses, so a stray
@lg:can't creep in.
You can always use an arbitrary value like @min-[475px]: for a one-off. If you repeat the same arbitrary value in several places, a theme variable is easier to maintain. You change it in one place and every use follows.
How the scale is built
In Tailwind v4, container sizes are theme variables in the --container-* namespace. The default theme defines them like this:
| Variable | Value | Variant it produces |
|---|---|---|
| `--container-3xs` | 16rem | `@3xs:` |
| `--container-xs` | 20rem | `@xs:` |
| `--container-sm` | 24rem | `@sm:` |
| `--container-md` | 28rem | `@md:` |
| `--container-lg` | 32rem | `@lg:` |
| `--container-2xl` | 42rem | `@2xl:` |
| `--container-7xl` | 80rem | `@7xl:` |
The table skips some entries. The full scale also has 2xs, xl, and 3xl through 6xl.
Each variable's name after --container- becomes the size name. For each name, Tailwind generates three variants:
@name:applies at that container width and above.@min-name:is the explicit form of the same rule.@max-name:applies below that container width.
The same names also work with a container name suffix, such as @md/main:. Customizing the theme therefore changes every form of the variant at once.
Adding a new size
To add a size without touching the existing ones, declare a new variable inside @theme. This is the smallest change, and everything in the default scale keeps working.
@import "tailwindcss";
@theme {
--container-8xl: 96rem;
}After this, @8xl:, @min-8xl:, and @max-8xl: exist. You can use them like any other container variant:
<div class="@container">
<div class="grid grid-cols-1 @2xl:grid-cols-2 @8xl:grid-cols-6 gap-4">
<div class="rounded bg-white p-4 shadow">One</div>
<div class="rounded bg-white p-4 shadow">Two</div>
<div class="rounded bg-white p-4 shadow">Three</div>
<div class="rounded bg-white p-4 shadow">Four</div>
</div>
</div>Tailwind does not care that 8xl follows the 7xl pattern. The name is just a label. It orders the variants by their values, not by their names, so a size you add in the middle of the range lands in the right place in the generated CSS.
@import "tailwindcss";
@theme {
--container-card: 30rem;
}With this, @card: becomes a valid variant that applies from 30rem upward. A name that describes a component or a purpose often reads better in markup than a number that has to be looked up.
Overriding an existing size
If you declare a variable that already exists, your value replaces the default:
@import "tailwindcss";
@theme {
--container-md: 30rem;
}Now @md: triggers at 30rem instead of 28rem. Every @md:, @min-md:, and @max-md: in the project, including those in third-party markup that uses your build, shifts with it.
This is a common source of surprise, because the --container-* namespace is shared. Utilities that resolve sizes from the same variables read your new value too. For example, max-w-md is built from --container-md, so after the override above it is also 30rem. If you only want a different breakpoint for container queries, add a new name instead of changing md.
| Goal | Safer choice |
|---|---|
| One extra breakpoint for one component | Add a new name such as `--container-card` |
| Match a design system's exact steps everywhere | Override the existing sizes deliberately |
| A single odd value in one place | Use an arbitrary value such as `@min-[475px]:` |
Replacing the whole scale
You can clear the default scale and define only the sizes you want. Setting a namespace to initial with a wildcard removes every default in it:
@import "tailwindcss";
@theme {
--container-*: initial;
--container-compact: 20rem;
--container-regular: 36rem;
--container-wide: 60rem;
}Now only @compact:, @regular:, and @wide: exist, along with their @min- and @max- forms. Here is a component that uses them:
<div class="@container">
<article class="flex flex-col gap-3 @regular:flex-row @wide:gap-8">
<img class="w-full @regular:w-48" src="/photo.jpg" alt="Product photo" />
<div>
<h2 class="text-lg font-semibold">Product name</h2>
<p class="text-sm text-gray-600">Short description of the product.</p>
</div>
</article>
</div>A small, named scale has two benefits. Developers choose from three steps, not thirteen. A typo or a size from another project, such as @lg:, produces no CSS, so it is visible during review.
Container sizes are not breakpoints
Tailwind keeps container sizes and viewport breakpoints in separate namespaces:
--breakpoint-*controlssm:,md:,lg:and the other viewport variants.--container-*controls@sm:,@md:,@lg:and the other container variants.
The names look alike, but the values differ. In the default theme, the viewport md: starts at 48rem while @md: starts at 28rem. That difference is intended. A component usually fits in a narrower space than the whole page, so the container scale starts smaller.
Changing one namespace never changes the other. If you customize --breakpoint-md, @md: keeps its value. If your design system needs both to move together, set both variables explicitly.
Units and rem behavior
Write theme values in rem or px, like any other length. A few details matter in practice:
remvalues follow the root font size of the document. If a user changes their browser's default font size,rem-based container sizes shift with it, which usually keeps text and layout in proportion.- Container variants compare against the container element's width, specifically its inline size. A container's padding and border are not part of what the query measures, so a container with
p-8is queried at its content width, not its outer width. - The same theme variables are usable in plain CSS through
var(--container-card). That is helpful when a hand-written@containerrule should match your utility classes:
@import "tailwindcss";
@theme {
--container-card: 30rem;
}
.card-title {
font-size: 1rem;
}
@container (min-width: 30rem) {
.card-title {
font-size: 1.25rem;
}
}Note that the plain @container rule above repeats the value 30rem as a literal. CSS does not allow var() inside a container condition, so the theme variable cannot be referenced there. If you need the two to stay in sync, put a comment in both places or use the utility variant in markup instead.
Customizing in Tailwind v3
Tailwind v3 has no @theme directive or --container-* variables. Container support comes from the official @tailwindcss/container-queries plugin, and the plugin reads a containers key from the theme in your config file:
// tailwind.config.js (Tailwind v3 only)
module.exports = {
theme: {
extend: {
containers: {
'8xl': '96rem',
card: '30rem',
},
},
},
plugins: [require('@tailwindcss/container-queries')],
}This gives you @8xl: and @card: in v3. Using extend adds to the plugin's default sizes. Putting containers directly under theme replaces them. If you later migrate to v4, move each entry to a --container-* variable in your CSS.
Common mistakes
- Using a size name that does not exist. If you write
@card:before defining--container-card, Tailwind emits no CSS for it and the layout silently stays at its base styles. Check spelling against your theme first. - Overriding when you meant to add. Changing
--container-mdshiftsmax-w-mdand every other use of the size. Prefer a new name unless the global change is intended. - Forgetting the
@containerancestor. Theme sizes only define thresholds. A child still needs an ancestor with@containerto measure. - Mixing up the namespaces.
--breakpoint-mdwill not change@md:, and--container-mdwill not changemd:. - Resetting the scale and losing utilities. After
--container-*: initial, anything that read those variables, includingmax-w-*sizes, needs a replacement. - Using non-length values. A value such as
--container-card: 30without a unit is invalid for a size, so write30rem.
Choosing an approach
- Use an arbitrary value (
@min-[475px]:) when the size appears once. - Use a new theme variable when two or more components share a threshold, or when a name makes the intent clearer.
- Override a default only when you want that change to apply everywhere the size is used.
- Reset and redefine the scale when you want a small, enforced vocabulary and you have checked what depends on the defaults.
Exercise
Your Tailwind v4 project needs a container variant that starts at 40rem, and you want to use it as @panel:. Write the CSS that defines it. Then explain what would happen if you instead wrote --container-lg: 40rem;.
Show answer
Add --container-panel: 40rem; inside an @theme block after @import "tailwindcss";. This produces @panel:, @min-panel:, and @max-panel:. Writing --container-lg: 40rem; would instead override the existing lg size, so every @lg: variant would start at 40rem instead of 32rem, and utilities that read --container-lg, such as max-w-lg, would also change to 40rem. No @panel: variant would exist.
Exercise
A teammate resets the scale with --container-*: initial; and defines only --container-compact: 20rem;. A card still contains the classes @container, @lg:flex-row, and max-w-lg. What happens to each class?
Show answer
@container still works because it only marks the element as a query container. @lg:flex-row produces no CSS because --container-lg no longer exists, so the card never switches to a row layout. max-w-lg also stops resolving to a width because it reads --container-lg, so the max width is not applied. The fix is to use @compact: for the layout change and to replace max-w-lg with a defined width or a restored size.