Containers
Use container size tokens for reusable width caps, component dimensions, and container query variants.
Overview
Containers define the size scale for reusable layout surfaces. In Master CSS, the container namespace has two related jobs:
- Provide contextual size values such as
max-w-md,w-3xl,width-lgandheight-lg, andflex-basis-sm. - Provide a shared size vocabulary for explicit native container queries, so components can respond to the space they actually receive.
Use breakpoints for viewport-level page changes. Use containers for component-level adaptation and shared width caps.
| Token | Value | PX | Description |
|---|---|---|---|
--container-3xs | 16rem | 256px | Small component, compact card, or narrow popover. |
--container-2xs | 18rem | 288px | Compact panel and constrained media region. |
--container-xs | 20rem | 320px | Small content module or sidebar-friendly component. |
--container-sm | 24rem | 384px | Comfortable card, form block, or narrow reading panel. |
--container-md | 28rem | 448px | Common component layout threshold. |
--container-lg | 32rem | 512px | Wide component or compact page section. |
--container-xl | 36rem | 576px | Large section, modal, or media composition. |
--container-2xl | 42rem | 672px | Small page wrapper or large split-pane region. |
--container-3xl | 48rem | 768px | Compact page canvas. |
--container-4xl | 56rem | 896px | Standard page canvas. |
--container-5xl | 64rem | 1024px | Reading or documentation wrapper. |
--container-6xl | 72rem | 1152px | Wide content wrapper. |
--container-7xl | 80rem | 1280px | Maximum product canvas. |
--container-8xl | 90rem | 1440px |
Define container variables
Container tokens belong in the theme layer and use the container-* namespace.
@theme { --container-sm: 24rem; --container-md: 28rem; --container-7xl: 80rem;}Use complete query values with explicit units. Container query preludes do not resolve theme tokens or var() references; use a named @custom-variant when a query is shared. The tables show the current token values as complete query examples, preserving their authored units.
Namespace for container values
The container variable namespace is shared by width, height, min and max constraints, flex basis, intrinsic size, and media sizing utilities.
| Group | Utility keys | Description |
|---|---|---|
| Preferred size | w, h, width, height, inline-size, block-size, size-x, size-y | Set each axis explicitly from the shared container scale. |
| Minimum constraints | min-w, min-h, min-size-x, min-size-y, min-width, min-height, min-inline-size, min-block-size | Keep regions from collapsing below a reusable layout threshold. |
| Maximum constraints | max-w, max-h, max-size-x, max-size-y, max-width, max-height, max-inline-size, max-block-size | Cap wrappers, panels, media, and content regions. |
| Layout basis | flex-basis | Give flex items a shared starting size before free space is distributed. |
| Intrinsic and media sizes | background-size, mask-size, contain-intrinsic-inline-size, contain-intrinsic-block-size | Use container tokens for reusable media and intrinsic size decisions. |
This lets sizing utilities use md, 5xl, or project token names without the container- prefix. Native query values are explicit; they do not search this namespace.
Use container values in sizing utilities
Sizing utilities that read from the container namespace use contextual shorthand. Write max-w-md; do not repeat the namespace inside the value.
<main class="w:100% max-w-5xl mx:auto"> ...</main><aside class="flex-basis-md flex-shrink:0">...</aside>Generated CSS
@layer theme { :root, :host { --container-5xl: 64rem; --container-md: 28rem }}@layer utilities { .mx\:auto { margin-inline: auto } .flex-basis-md { flex-basis: var(--container-md) } .max-w-5xl { max-width: var(--container-5xl) } .w\:100\% { width: 100% }}The shorthand is contextual: md can mean a container value inside max-w-md, and a breakpoint threshold inside @md. The utility or variant decides which namespace is active.
| Value | Token | Size | Example utility |
|---|---|---|---|
3xs | container-3xs | 256px / 16rem | max-w-3xs |
2xs | container-2xs | 288px / 18rem | max-w-2xs |
xs | container-xs | 320px / 20rem | max-w-xs |
sm | container-sm | 384px / 24rem | max-w-sm |
md | container-md | 448px / 28rem | max-w-md |
lg | container-lg | 512px / 32rem | max-w-lg |
xl | container-xl | 576px / 36rem | max-w-xl |
2xl | container-2xl | 672px / 42rem | max-w-2xl |
3xl | container-3xl | 768px / 48rem | max-w-3xl |
4xl | container-4xl | 896px / 56rem | max-w-4xl |
5xl | container-5xl | 1024px / 64rem | max-w-5xl |
6xl | container-6xl | 1152px / 72rem | max-w-6xl |
7xl | container-7xl | 1280px / 80rem | max-w-7xl |
8xl | container-8xl | 1440px / 90rem | max-w-8xl |
Create a query container
Add container to an ancestor whose inline size comes from the surrounding layout. Then apply full @container((width>=28rem)) queries to its descendants. A size query cannot select the element itself as its query container.
<section class="container"> <article class="grid-cols:1 gap-lg grid-cols:2@container((width>=28rem))"> ... </article></section>Generated CSS
@layer theme { :root, :host { --spacing-lg: 1.5rem }}@layer utilities { .container { container-type: inline-size } .gap-lg { gap: var(--spacing-lg) } .grid-cols\:1 { display: grid; grid-template-columns: repeat(1, minmax(0, 1fr)) } @container (width>=28rem) { .grid-cols\:2\@container\(\(width\>\=28rem\)\) { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)) } }}Container queries make components portable. The same card can be stacked in a sidebar and split into columns in a wide content region without knowing the viewport width.
Container query scale
The following complete queries use values from the preset container scale. Copy a value with its unit, or define a named custom variant for a reusable query.
| Variant | Value | Generated query |
|---|---|---|
@container(3xs) | 256px / 16rem | @container (width>=16rem) |
@container(2xs) | 288px / 18rem | @container (width>=18rem) |
@container(xs) | 320px / 20rem | @container (width>=20rem) |
@container(sm) | 384px / 24rem | @container (width>=24rem) |
@container(md) | 448px / 28rem | @container (width>=28rem) |
@container(lg) | 512px / 32rem | @container (width>=32rem) |
@container(xl) | 576px / 36rem | @container (width>=36rem) |
@container(2xl) | 672px / 42rem | @container (width>=42rem) |
@container(3xl) | 768px / 48rem | @container (width>=48rem) |
@container(4xl) | 896px / 56rem | @container (width>=56rem) |
@container(5xl) | 1024px / 64rem | @container (width>=64rem) |
@container(6xl) | 1152px / 72rem | @container (width>=72rem) |
@container(7xl) | 1280px / 80rem | @container (width>=80rem) |
@container(8xl) | 1440px / 90rem | @container (width>=90rem) |
This is different from viewport breakpoints on purpose. A component threshold is usually smaller than a page threshold. For example, @container((width>=28rem)) can represent a comfortable card width, while @md represents a desktop page shell.
Query container ranges
Use native CSS range and logical operators when a component needs a bounded range.
<div class="hidden@container((24rem<=width<=28rem))">...</div>Generated CSS
@layer utilities { @container (24rem<=width<=28rem) { .hidden\@container\(\(24rem\<\=width\<\=28rem\)\) { display: none } }}For repeated conditions, define @custom-variant card-wide { @container (width>=28rem) { @slot; } } and use @card-wide. Container names are explicit inside the full query; an unknown @name never becomes a container query.
Use named containers
Use named containers when a descendant should query a specific ancestor instead of the nearest container.
<section class="container:card/inline-size"> <article class="grid-cols:2@container(card|(width>=16rem))">...</article></section>Generated CSS
@layer utilities { .container\:card\/inline-size { container: card/inline-size } @container card (width>=16rem) { .grid-cols\:2\@container\(card\|\(width\>\=16rem\)\) { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)) } }}Named containers are useful in nested layouts, but do not add names everywhere. The default nearest-container behavior is simpler and easier to move between compositions.
Choose caps or queries
Container size values and container queries are related, but they solve different problems.
| Need | Use | Example |
|---|---|---|
| Cap a page wrapper | Container value | w:100% max-w-7xl mx:auto |
| Cap a content measure | Local or container value | max-w:72ch or max-w-5xl |
| Adapt a reusable component | Container query | grid-cols:2@container((width>=28rem)) |
| Coordinate nested responsive regions | Named container | `grid-cols:2@container(card |
| Change the whole page shell | Breakpoint | grid-cols:12@md |
A reusable card depends on the viewport, so it changes at the wrong time inside narrow parents.
<article class="grid-cols:2@md">...</article>The card depends on the space it receives.
<section class="container"> <article class="grid-cols:2@container((width>=28rem))">...</article></section>If a layout rule describes the whole page, use breakpoints. If it describes a reusable component or a wrapper width, use containers.