Container Queries

Responsive Components

Media queries look at the viewport. Container queries look at the size of the parent container, so a component adapts wherever it is placed: in a narrow sidebar or a wide main area.

Example

The same card markup in a narrow and a wide container:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
  <style>
    .box { container-type: inline-size; border: 1px dashed #aaa; padding: 6px; margin-bottom: 10px; }
    .narrow { width: 220px; }
    .card { display: grid; gap: 8px; font-family: Arial; background: #f1f3f5; padding: 8px; }
    .card img { width: 100%; height: 70px; object-fit: cover; }
    @container (min-width: 380px) {
      .card { grid-template-columns: 120px 1fr; align-items: center; }
    }
  </style>
</head>
<body>

<div class="box narrow"><div class="card"><img src="https://picsum.photos/id/1040/300/150" alt=""><p>Stacked in a narrow container</p></div></div>
<div class="box"><div class="card"><img src="https://picsum.photos/id/1040/300/150" alt=""><p>Side by side in a wide container</p></div></div>

</body>
</html>

Result