@tailwindcss/container-queries
repository·main·Indexed 23 days ago
https://github.com/tailwindlabs/tailwindcss-container-queriesA Tailwind CSS plugin for versions 3.2+ that enables container queries, allowing elements to be styled based on the dimensions of a parent container rather than the viewport. It provides the @container utility, named containers, arbitrary container sizes, and customizable breakpoints via the theme.containers configuration. Note: Container queries are supported natively in Tailwind CSS v4.0 and later.
What's inside @tailwindcss/container-queries
- As of Tailwind CSS v4.0, container queries are supported natively by the framework. If you are using Tailwind CSS v4.0 or later, you do not need to install or configure this plugin.
Remove a container with @container-normal
mainTo stop an element from acting as a container (for example, conditionally disabling container behavior at a certain breakpoint), use the
@container-normalclass.<div class="@container xl:@container-normal"> <!-- ... --> </div>Use container queries with @container
mainTo use container queries, first mark an element as a container using the
@containerclass. You can then apply styles to child elements using container variants like@md:,@lg:, and@xl:. These styles will trigger based on the size of the container rather than the viewport.<div class="@container"> <div class="@lg:underline"> <!-- This text will be underlined when the container is larger than `32rem` --> </div> </div>Use container queries with a Tailwind prefix
mainIf your Tailwind configuration uses a prefix (e.g.,
tw-), you must prefix both the@containerclass and the container query modifiers.<div class="tw-@container"> <!-- ... --> <div class="@lg:tw-underline"> <!-- ... --> </div> </div>Use arbitrary container sizes
mainIf the default sizes do not meet your needs, you can use arbitrary values with the
@[value]syntax to create one-off container query breakpoints.<div class="@container"> <div class="@[17.5rem]:underline"> <!-- This text will be underlined when the container is larger than `17.5rem` --> </div> </div>Install @tailwindcss/container-queries
mainFor Tailwind CSS v3.2+, install the plugin via npm as a development dependency:
npm install -D @tailwindcss/container-queriesUse named containers with @container/{name}
mainYou can name specific containers using the
@container/{name}syntax. To target a specific named container, use the variant format@size/{name}:utility. This allows you to query against a specific ancestor container even if there are multiple containers nested in the tree.<div class="@container/main"> <!-- ... --> <div class="@lg/main:underline"> <!-- This text will be underlined when the "main" container is larger than `32rem` --> </div> </div>Configure @tailwindcss/container-queries in tailwind.config.js
mainAfter installing, add the plugin to your
tailwind.config.jsfile within thepluginsarray.// tailwind.config.js module.exports = { theme: { // ... }, plugins: [ require('@tailwindcss/container-queries'), // ... ], }Configure custom container sizes
mainYou can extend or override the available container sizes by adding a
containerskey to thetheme.extendsection of yourtailwind.config.jsfile.// tailwind.config.js module.exports = { theme: { extend: { containers: { '2xs': '16rem', }, }, }, }Install and configure the @tailwindcss/container-queries plugin
mainTo use container queries in Tailwind CSS, add the
@tailwindcss/container-queriesplugin to yourtailwind.config.jsfile.By default, the plugin provides a set of container size breakpoints under the
containerstheme key. You can customize these values in your configuration to define your own container query breakpoints.Configure custom container breakpoints
mainYou can define custom breakpoint values for container queries within the
theme.containerssection of your Tailwind configuration. These values will then be available as@<key>variants.Default values provided by the plugin (if not overridden) include:
xs:20remsm:24remmd:28remlg:32remxl:36rem2xl:42rem3xl:48rem4xl:56rem5xl:64rem6xl:72rem7xl:80rem
module.exports = { theme: { containers: { xs: '20rem', sm: '24rem', // ... } } }Reference default container sizes
mainThe plugin provides the following default container sizes:
| Name | CSS | | ------ | -------------------------------------------- | | `@xs` | `@container (min-width: 20rem /* 320px */)` | | `@sm` | `@container (min-width: 24rem /* 384px */)` | | `@md` | `@container (min-width: 28rem /* 448px */)` | | `@lg` | `@container (min-width: 32rem /* 512px */)` | | `@xl` | `@container (min-width: 36rem /* 576px */)` | | `@2xl` | `@container (min-width: 42rem /* 672px */)` | | `@3xl` | `@container (min-width: 48rem /* 768px */)` | | `@4xl` | `@container (min-width: 56rem /* 896px */)` | | `@5xl` | `@container (min-width: 64rem /* 1024px */)` | | `@6xl` | `@container (min-width: 72rem /* 1152px */)` | | `@7xl` | `@container (min-width: 80rem /* 1280px */)` |