Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Responsive Design and State Variants

Container Queries and More Variants

Use Tailwind CSS container queries with @container and @md, plus aria-, data-, supports-, motion-safe, print and starting variants for adaptive components.

Breakpoints answer the question "how wide is the screen?". But a reusable card might sit in a wide main column on one page and a narrow sidebar on another, on the very same screen width. What matters to the card is the width of its own container. Tailwind v4 includes container queries in core for exactly this, and this lesson also covers the other condition-based variants: aria-*, data-*, supports-*, motion-safe, print, open and starting.

Container queries

Mark a parent as a query container with @container. Its descendants can then use variants that start with @, such as @md:flex-row. These look at the container's width instead of the viewport's.

Variant Minimum container width
@xs: 20rem (320px)
@sm: 24rem (384px)
@md: 28rem (448px)
@lg: 32rem (512px)
@xl: 36rem (576px)
@2xl: to @7xl: 42rem and up

@max-md: is the maximum version, and @sm:@max-lg: makes a range, just like with breakpoints.

The same card in three container widths

Both cards share exactly the same classes. The second one stays stacked because its box is only 192px wide, no matter how wide the preview is. The demo-box utility, defined in the <style type="text/tailwindcss"> block, only makes the first box resizable.

Named containers and arbitrary sizes

Name containers with @container/sidebar and target them with @md/sidebar:. For a one-off size, use @min-[475px]: or @max-[600px]:. Container size units such as cqw are available through arbitrary values, for example w-[50cqw].

A query container cannot size itself from its own contents in the inline direction. Put @container on a wrapper and the responsive utilities on its children, as in the example above.

aria-* and data-* variants

You can style an element from its ARIA attributes or custom data- attributes, which keeps state in one place: the attribute that JavaScript and assistive technology already use.

  • aria-expanded:, aria-pressed:, aria-checked:, aria-selected:, aria-disabled: and a few more are built in. For any other attribute use aria-[sort=ascending]:.
  • data-[state=open]: tests a data attribute value; data-active: tests that the attribute exists.
Tabs styled by aria-selected

The script only flips aria-selected; the look follows. That means the visual state and the accessibility state can never drift apart.

Data attributes work the same way. A headless UI library that sets data-state="open" can be styled with data-[state=open]:rotate-180 on an arrow icon.

Conditions of the environment

Variant Condition
motion-safe: / motion-reduce: prefers-reduced-motion
contrast-more: prefers-contrast: more
forced-colors: Windows high-contrast mode
print: printing the page
portrait: / landscape: device orientation
pointer-coarse: / pointer-fine: touch vs mouse input
supports-[display:grid]: the browser supports a CSS feature
not-supports-[...]: the browser does not

Respecting prefers-reduced-motion is an accessibility basic: wrap decorative animation in motion-safe: so people who asked their system for less motion do not get it.

Motion-safe bounce and print styles

open, details and starting-style

  • open: matches <details open> and an open <dialog> (and a popover that is showing).
  • starting: applies the CSS @starting-style rule, so an element can transition in when it first appears (for example a popover or a newly inserted element) instead of popping. You will use it in Transitions and Transforms.
  • inert: styles elements with the inert attribute.
A details disclosure

Putting it together

Variants are a small grammar: [theme/media/state]:[more]:utility. Whenever you catch yourself writing a media query or a pseudo-class selector by hand, first check whether a variant exists. If not, the arbitrary form ([&_p]:mt-2, [@media(min-aspect-ratio:16/9)]:hidden) and the custom variants from Custom Utilities and Variants fill the gap.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which class turns a wrapper into a container whose width children can query?

    @container sets the element as a query container; descendants then use variants such as @md:.
  2. 2. What does @max-md:hidden do?

    The @ prefix refers to the container width, and max- turns the query into an upper limit.
  3. 3. Why prefer aria-selected: over a custom .active class for tabs?

    Styling from the ARIA attribute keeps the visual and assistive technology state in sync.
  4. 4. Which variant should wrap a decorative animation to respect user settings?

    motion-safe: applies only when the user has not requested reduced motion.

Summary

  • @container plus @sm:, @md:, @max-md: lets a component respond to its container, so the same markup works in any layout.
  • aria-* and data-* variants style from attributes: aria-selected:, data-[state=open]:.
  • Environment variants cover reduced motion, contrast, print, orientation, input type and supports-*.
  • open: handles <details> and dialogs, and starting: animates elements as they enter.

Next: Dark Mode