Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-search-preview-high-loc· OpenAIHugging Face LMArena Search Arena 24K

how do i do container queries in tailwind 4

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Perplexitysonar-reasoning-pro-high’s answer.
  1. User

    how do i do container queries in tailwind 4

  2. Assistant

    In Tailwind CSS v4, container queries are natively supported, allowing you to apply styles based on the size of a parent container rather than the viewport. Here's how to use them:

    1. Mark an Element as a Container: Add the @container class to the parent element you want to act as the container.

      <div class="@container">
      
    1. Apply Styles Based on Container Size: Use container query variants to apply styles to child elements when the container reaches specific sizes. Tailwind provides default container sizes ranging from @3xs (16rem) to @7xl (80rem)[1].

      <div class="@container">
      
    1. Customize Container Sizes (Optional): If the default sizes don't meet your needs, you can customize them in your tailwind.config.js file.

      // tailwind.config.js
      module.exports = {
        theme: {
          extend: {
            containers: {
              '2xs': '16rem', // Adds a smaller breakpoint at 16rem
              'lg': '40rem',  // Overrides lg to 40rem if desired
              // ...other overrides...
            },
          },
        },
      };
      
    2. Use Named Containers (Optional): For complex designs with multiple nested containers, you can name containers using @container/{name} and target specific containers with variants like @sm/{name} and @md/{name}[1].

      <div class="@container/main">
      

    By leveraging container queries in Tailwind CSS v4, you can create more responsive and modular components that adapt to their parent container's size, enhancing the flexibility and maintainability of your designs.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.