Sub topic 11
Using container queries in Tailwind v3
This chapter assumes you know Tailwind utility classes, responsive variants such as md:, and the idea of a container query from earlier chapters. It covers how to get the same feature in a project that is still on Tailwind v3.
Why v3 needs a plugin
Tailwind v4 includes container query support in its core. Tailwind v3 does not. Without help, @container and @md:flex-row generate nothing, and the page silently ignores them.
The official plugin @tailwindcss/container-queries adds the same classes to v3. Once it is installed, the markup you write is almost identical to v4.
Installing 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)
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./src/**/*.{html,js,jsx,ts,tsx}'],
theme: {
extend: {},
},
plugins: [require('@tailwindcss/container-queries')],
}Restart your dev server after changing the config. Tailwind reads the plugin list at startup, so a running watcher will not pick it up.
Using the classes
After installation, the usage matches what you have already seen. Mark an ancestor with @container, then style its descendants with @ variants:
<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 layout switches to a row when the @container element is at least md wide (28rem by default). The viewport width does not matter.
The plugin supports the features you need most:
- Size variants such as
@sm,@md, and@lg. - Max and range variants:
@max-md:flex-col, or@min-md:@max-xl:blockto target a range. - Named containers:
@container/mainon the ancestor and@md/main:flex-rowon the child.
Differences from v4
The main difference is the syntax for arbitrary values. The plugin uses a bracket form directly after the @, where v4 uses @min-[...].
| Task | Tailwind v3 plugin | Tailwind v4 |
|---|---|---|
| Arbitrary container size | `@[475px]:flex-row` | `@min-[475px]:flex-row` |
| Add a custom size | `theme.extend.containers` in the config file | `--container-*` in `@theme` |
| Enable the feature | Install and register the plugin | Nothing to do |
Here is a v3 example with an arbitrary size:
<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>Container query units such as cqw work through ordinary arbitrary values. They are a browser feature, so w-[50cqw] needs no plugin. The element still needs a @container ancestor, because the units resolve against it.
Customizing container sizes in v3
Instead of a @theme variable, you add sizes under theme.extend.containers:
// tailwind.config.js (Tailwind v3 only)
module.exports = {
content: ['./src/**/*.{html,js,jsx,ts,tsx}'],
theme: {
extend: {
containers: {
'8xl': '96rem',
},
},
},
plugins: [require('@tailwindcss/container-queries')],
}This makes @8xl: available. Use extend so that you add to the default scale. Writing containers directly under theme replaces the whole scale, and @md stops working.
Deciding what to do
If you plan to upgrade to v4 later, prefer the portable parts of the syntax. Size variants like @md and named containers work the same in both versions. Arbitrary values are the one thing you will need to rewrite.
Common problems in v3
- Classes do nothing. The plugin is missing from
plugins, or the dev server was not restarted. - Classes missing from the output. Your
contentpaths do not include the file that holds the markup, so Tailwind never sees the class names. @mdstopped working after customizing. You replaced the scale instead of usingextend.- Variants have no effect. There is no
@containerancestor. A variant applies to the nearest marked ancestor, and an unmarked parent is skipped.
Exercise
A teammate on Tailwind v3 writes <div class="@container"><div class="flex-col @md:flex-row">…</div></div>, but the layout never changes to a row. The markup is correct and the file is covered by content. What are the two most likely causes, and how do you fix them?
Show answer
The plugin is probably not installed or not listed in plugins in tailwind.config.js, because Tailwind v3 does not include container queries in core. Install @tailwindcss/container-queries and add require('@tailwindcss/container-queries') to plugins. The second likely cause is that the dev server was not restarted after the config change. Restart it so the plugin loads.