Sub topic 11
Using container queries in Tailwind v3
This chapter shows how to get container queries working in a project that still runs Tailwind CSS v3. It assumes you have read the earlier chapters, so you know what @container and @md: do and how viewport variants such as md: differ from container variants. You should also be comfortable editing tailwind.config.js and running npm commands.
Why v3 needs a plugin
Tailwind v4 includes container query support in core. Tailwind v3 does not. In a v3 project, @container and variants such as @md:flex-row generate nothing until you add the official plugin, @tailwindcss/container-queries.
Without the plugin, the failure is silent. Tailwind finds no matching utility, emits no CSS, and the element keeps its base styles. There is no build error and no browser warning.
The plugin adds three things to v3:
- The
@containerutility, which setscontainer-type: inline-sizeon an element. - Container variants such as
@sm:,@md:and@lg:, which compile to@container (min-width: ...)rules. - Named containers and arbitrary container sizes.
Newer plugin releases add @max-* variants. The plugin does not add container query length units such as cqw as utilities. You reach those through arbitrary values, which v3 already supports.
Requirements
The plugin builds its variants with Tailwind's matchVariant API, which arrived in Tailwind v3.2. Check your installed version before anything else.
npx tailwindcss --help | head -n 3
npm ls tailwindcssIf npm ls reports a version older than 3.2.0, upgrade Tailwind first:
npm install -D tailwindcss@^3Browser support is a separate matter. Container queries work in all current evergreen browsers (Chrome, Edge, Safari and Firefox). Check your own support targets, because older browsers ignore the rules and show the base layout.
Installing and enabling the plugin
Install the package as a development dependency:
npm install -D @tailwindcss/container-queriesThen register it in tailwind.config.js:
// tailwind.config.js (Tailwind v3 only)
module.exports = {
content: ['./src/**/*.{html,js,jsx,ts,tsx}'],
theme: {
extend: {},
},
plugins: [require('@tailwindcss/container-queries')],
}If your config file uses ES modules, import the plugin instead:
// tailwind.config.mjs (Tailwind v3, ES modules)
import containerQueries from '@tailwindcss/container-queries'
/** @type {import('tailwindcss').Config} */
export default {
content: ['./src/**/*.{html,js,jsx,ts,tsx}'],
theme: {
extend: {},
},
plugins: [containerQueries],
}Restart your dev server after editing the config. Many setups only read the config at startup, so a running watcher may not pick up the new plugin.
A plugin that is installed but not listed in plugins has no effect. This is the most common reason container variants appear to do nothing.
Using it: same classes as v4
After setup, the markup is the same as in the earlier chapters.
<div class="@container">
<div class="flex flex-col gap-4 @md:flex-row">
<img class="w-full @md: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>
</div>
</div>The outer div is the query container. Its child switches from a column to a row once the container is at least @md wide, regardless of the viewport width.
The following parts behave the same as in v4:
- The parent must carry
@container, and the variant must be on a descendant of it. - Variants stack with others, for example
@md:hover:bg-gray-100ordark:@lg:p-8. - Named containers work with
@container/nameand@md/name:.
Here is a named-container example. It runs as written in v3 once the plugin is installed:
<div class="@container/main">
<aside class="@container/sidebar">
<div class="flex flex-col @md/main:flex-row @lg/sidebar:gap-8">
<div>A</div>
<div>B</div>
</div>
</aside>
</div>The v3 plugin's container size scale
The plugin defines its own scale. The values are the same as the default scale in v4, but you can only see them in the plugin source or its README, not in Tailwind's default theme.
| Variant | Minimum container width |
|---|---|
| `@xs` | 20rem (320px) |
| `@sm` | 24rem (384px) |
| `@md` | 28rem (448px) |
| `@lg` | 32rem (512px) |
| `@xl` | 36rem (576px) |
| `@2xl` | 42rem (672px) |
| `@3xl` | 48rem (768px) |
| `@4xl` | 56rem (896px) |
| `@5xl` | 64rem (1024px) |
| `@6xl` | 72rem (1152px) |
| `@7xl` | 80rem (1280px) |
These are not the same values as the viewport breakpoints. In v3, md: means 768px of viewport, while @md: means 448px of container. If you copy a layout from md: to @md:, expect the switch to happen at a much narrower size.
Container variants use min-width, so they are mobile-first like viewport variants. An unprefixed class is the base, and @md: overrides it at 28rem and above.
Arbitrary sizes in v3
When the scale does not have the size you need, use square brackets:
<div class="@container">
<div class="flex flex-col @[475px]:flex-row">
<div class="w-[50cqw]">Half of the container width</div>
<div>Content</div>
</div>
</div>In v3, the arbitrary container variant is written @[475px]:. This is a difference from v4, where the same idea is written @min-[475px]:.
The w-[50cqw] class is an ordinary arbitrary value. cqw is a CSS unit equal to 1% of the container's width. It works in v3 because Tailwind passes arbitrary values straight through to CSS. It only resolves against an ancestor that is a query container, so @container must still be present.
Max-width variants
Support for @max-* variants depends on the plugin version. Recent releases (0.1.0 and later) add @max-md: and related variants. Older releases only have the min-width form.
<div class="@container">
<div class="flex flex-row @max-md:flex-col">
<div>Left</div>
<div>Right</div>
</div>
</div>Check what you installed:
npm ls @tailwindcss/container-queriesIf you are on an older release and cannot upgrade, write the layout mobile-first. Put the narrow layout in the base classes and the wide layout behind @md:. That gives you the same result without needing a max variant.
Customizing sizes in v3
In v3 you customize container sizes in tailwind.config.js, not in CSS. Add a containers key under theme.extend:
// tailwind.config.js (Tailwind v3 only)
module.exports = {
theme: {
extend: {
containers: {
'2xs': '16rem',
'8xl': '96rem',
},
},
},
plugins: [require('@tailwindcss/container-queries')],
}With this, @2xs: and @8xl: become available alongside the defaults. Using extend keeps the defaults. If you put containers directly under theme, you replace the whole scale and @md: stops working.
This is a v3-only mechanism. In v4 the same job is done with a --container-8xl: 96rem; variable inside @theme.
Where to put the plugin in a build pipeline
The plugin is a normal Tailwind plugin, so it works wherever your v3 config is read. Frameworks that run Tailwind through PostCSS (Next.js, Vite, Astro, Laravel Mix and similar) need no extra step. The only requirement is that the config file Tailwind loads is the one you edited.
Monorepos are the usual exception. If each package has its own tailwind.config.js, add the plugin to the config used by the package that renders the component. Installing it at the repo root and registering it in the wrong config gives the same silent no-output result.
Use this decision flow when container variants do not seem to work in a v3 project:
Common v3 pitfalls
Mixing up `container` and `@container`
Tailwind has an older utility called container. It sets a max-width that matches the current viewport breakpoint, and it has nothing to do with container queries. Writing class="container" and expecting @md: to work does not help. The class you need is @container, with the at sign.
Copying v4 syntax into v3
Examples written for v4 do not always run on v3. Check these differences when you copy code:
- v4 writes arbitrary container sizes as
@min-[475px]:. In v3, use@[475px]:. - v4 offers
@min-md:and@max-md:as an explicit pair. In v3,@max-*depends on the plugin version, and@min-*may not exist in older releases. - v4 customizes sizes with
@themeand--container-*. In v3, usetheme.extend.containers. - v4 has no
pluginsentry for this feature. Leaving the plugin line in a config you copy from a v3 project into v4 is unnecessary.
The container and its styled child are the same element
A query container cannot style itself with its own container variants. The query looks at an ancestor, so the variant must be on a descendant. Putting @container @md:p-8 on one element does nothing for that element's padding.
Layout that depends on content size
@container applies container-type: inline-size, so the container's width cannot depend on its contents. A container with display: inline-block, or one inside a shrink-to-fit flex or grid item, may collapse to zero width. Give it a defined width, for example w-full, or let it be a block-level element that fills its parent.
Unlisted content paths
Tailwind only generates classes it finds in your content globs. Class names such as @md:flex-row must appear in full, as plain strings, in files the globs cover. Building them from pieces, for example ` @${size}:flex-row `, means Tailwind never sees the whole name and emits no CSS.
Upgrading to v4 later
When you move to v4, remove the plugin. Core now handles @container and the variants. The migration work is small:
- Delete
@tailwindcss/container-queriesfrom thepluginsarray and frompackage.json. - Move any
theme.extend.containersvalues to--container-*variables in an@themeblock. - Rewrite arbitrary sizes from
@[475px]:to@min-[475px]:. The old form still works in some v4 builds, but the explicit form is the documented one. - Re-test any layout that relied on a specific plugin version's
@max-*behavior.
The official upgrade tool, npx @tailwindcss/upgrade, handles part of this. Review its diff rather than trusting it blindly.
Exercise
A teammate on a Tailwind v3.4 project adds <div class="@container"><div class="flex-col @md:flex-row">…</div></div> and sees the column layout at every width. They have installed @tailwindcss/container-queries with npm. List two things you would check first, and explain why the failure shows no error.
Show answer
First, open tailwind.config.js and confirm the plugin is listed in the plugins array. Installing the package alone does not enable it. Second, restart the dev server, because many setups read the config only at startup. Also confirm the file is covered by the content globs. There is no error because Tailwind simply finds no utility matching @md:flex-row, so it generates no CSS for it. The markup stays valid and the browser applies only the base classes.
Exercise
In a v3 project you need a layout that changes at a container width of 40rem, which is not in the default scale. Show two ways to do it, and say which is better if the size appears in many components.
Show answer
One way is an arbitrary value: @[40rem]:flex-row. The other is to add the size to the config with theme: { extend: { containers: { '40': '40rem' } } } and use @40:flex-row, or pick a descriptive name such as content. For a size used in many components, the config entry is better, because the value is defined once and the class names are shorter. Use extend so the default scale (@sm, @md and so on) stays available.