C

CSS Handbook

Clean • Professional

Advanced CSS Container Queries

3 minute

Advanced CSS Container Queries

CSS container queries let elements adapt based on the size of their parent container, not the viewport. This makes layouts modular, component-based, and highly maintainable. By 2025, all major browsers fully support them (Chrome 105+, Firefox 110+, Safari 16+, Edge).

Basic Container Query

Define a container with container-type and optionally container-name:

.container {
  container-type: inline-size; /* width-based */
  container-name: main;
}

Example:

<div class="container">
    <div class="card">
      <h2>Card Title 1</h2>
      <p>This is a card description. Resize the container to see changes.</p>
    </div>
    <div class="card">
      <h2>Card Title 2</h2>
      <p>Container queries make components responsive to their parent size.</p>
    </div>
    <div class="card">
      <h2>Card Title 3</h2>
      <p>When the container is wide enough, layout changes to flex automatically.</p>
    </div>
  </div>
/* Container setup */
    .container {
      container-type: inline-size; /* width-based container */
      container-name: main;
      border: 2px solid #333;
      padding: 20px;
      width: 50%; /* try resizing this to see the effect */
      margin: 20px auto;
    }

    /* Card default styles */
    .card {
      background: lightgray;
      padding: 20px;
      margin: 10px 0;
      border-radius: 8px;
      transition: all 0.3s ease;
    }

    .card h2 {
      margin: 0 0 10px;
      font-size: 1.2em;
    }

    .card p {
      margin: 0;
    }

    /* Container Query: apply styles when container > 400px */
    @container (min-width: 400px) {
      .card {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .card h2 {
        font-size: 2em;
      }
    }

Nested Containers

Target specific containers with container-name:

Example :

  <div class="outer-container">
    <h1>Outer Container</h1>
    <div class="inner-container">
      <div class="card">
        <h2>Card 1</h2>
        <p>This card responds to the inner container size.</p>
      </div>
      <div class="card">
        <h2>Card 2</h2>
        <p>Resize the inner container to see changes.</p>
      </div>
      <div class="card">
        <h2>Card 3</h2>
        <p>The outer container query also applies a color change if wider than 500px.</p>
      </div>
    </div>
  </div>
/* Outer container */
    .outer-container {
      container-type: inline-size; /* width-based */
      container-name: outer;
      border: 2px solid #333;
      padding: 20px;
      width: 60%;
      margin: 20px auto;
    }

    /* Inner container */
    .inner-container {
      container-type: inline-size; /* width-based */
      container-name: inner;
      border: 2px dashed #666;
      padding: 15px;
    }

    /* Card default styles */
    .card {
      background: lightgray;
      padding: 15px;
      margin: 10px 0;
      border-radius: 8px;
      transition: all 0.3s ease;
    }

    .card h2 {
      margin: 0 0 10px;
      font-size: 1.2em;
    }

    /* Outer container query */
    @container outer (min-width: 500px) {
      .card h2 {
        color: darkblue;
      }
    }

    /* Inner container query */
    @container inner (min-width: 300px) {
      .card {
        background-color: lightblue;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .card h2 {
        font-size: 1.5em;
      }
    }

Complex Queries

Combine multiple conditions:

@container (min-width: 400px) and (min-height: 200px) {
  /* styles here */
}

Target specific containers with names:

@container main (min-width: 500px) { /* styles */ }

Use container-relative units like cqi (container query inline size units):

font-size: 2cqi; /* 2% of container width */

Example :

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Complex Container Queries</title>
  <style>
    /* Main container */
    .container {
      container-type: size; /* track width and height */
      container-name: main;
      border: 2px solid #333;
      padding: 20px;
      width: 60%;
      margin: 20px auto;
    }

    /* Card default styles */
    .card {
      background: lightgray;
      padding: 15px;
      margin: 10px 0;
      border-radius: 8px;
      transition: all 0.3s ease;
    }

    .card h2 {
      margin: 0 0 10px;
      font-size: 1.2em;
    }

    /* Complex container query */
    @container main (min-width: 400px) and (min-height: 250px) {
      .card {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 15px;
        background-color: lightblue;
      }

      .card h2 {
        font-size: 2cqi; /* relative to container width */
      }
    }
  </style>
</head>
<body>

  <div class="container">
    <div class="card">
      <h2>Card 1</h2>
      <p>Container is wide and tall enough, grid layout applies.</p>
    </div>
    <div class="card">
      <h2>Card 2</h2>
      <p>Try resizing the container to see the effect.</p>
    </div>
    <div class="card">
      <h2>Card 3</h2>
      <p>Font size scales with container width using cqi units.</p>
    </div>
  </div>

</body>
</html>

Container Queries vs Media Queries

FeatureMedia QueryContainer Query
ScopeViewportParent container
ReusabilityLimitedHigh, component-based
FlexibilityFixed breakpointsSupports container-based conditions
MaintainabilityCan break layouts if viewport changesModular and predictable


Article 0 of 0