@tailwindcss/container-queries

repository·main·Indexed 23 days ago

https://github.com/tailwindlabs/tailwindcss-container-queries

A 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.

Tokens
1.8K
Snippets
10
Records
14
Agent score
31%

What's inside @tailwindcss/container-queries

  1. Use container queries with @container

    main

    To use container queries, first mark an element as a container using the @container class. 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>
  2. Use arbitrary container sizes

    main

    If 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>
  3. Use named containers with @container/{name}

    main

    You 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>
  4. Install and configure the @tailwindcss/container-queries plugin

    main

    To use container queries in Tailwind CSS, add the @tailwindcss/container-queries plugin to your tailwind.config.js file.

    By default, the plugin provides a set of container size breakpoints under the containers theme key. You can customize these values in your configuration to define your own container query breakpoints.

  5. Configure custom container breakpoints

    main

    You can define custom breakpoint values for container queries within the theme.containers section of your Tailwind configuration. These values will then be available as @<key> variants.

    Default values provided by the plugin (if not overridden) include:

    • xs: 20rem
    • sm: 24rem
    • md: 28rem
    • lg: 32rem
    • xl: 36rem
    • 2xl: 42rem
    • 3xl: 48rem
    • 4xl: 56rem
    • 5xl: 64rem
    • 6xl: 72rem
    • 7xl: 80rem
    module.exports = {
      theme: {
        containers: {
          xs: '20rem',
          sm: '24rem',
          // ...
        }
      }
    }
  6. Reference default container sizes

    main

    The 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 */)` |