Container Queries
What Is It?
A container size query adapts a component to its containing element rather than to the entire viewport.
Properties, Tags and Attributes Explained
container-type: inline-size: Establishes inline-axis size containment so descendants can query the container width.@container (min-width: 360px): Applies descendant rules when an eligible ancestor container meets the width condition.
Each example is a complete HTML page. The CSS is inside the head's style element. Use Try in new tab to edit the CSS or HTML, run your changes, and reset the example.
Example 1: Adapt a card
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Container Queries</title>
<style>
.host { container-type: inline-size; }
.card { display: grid; gap: 12px; padding: 16px; background: #e0e7ff; }
@container (min-width: 360px) { .card { grid-template-columns: 1fr 2fr; } }
</style>
</head>
<body>
<div class="host"><article class="card"><strong>CSS</strong><p>A component that adapts to its own available space.</p></article></div>
</body>
</html>Browser preview
What you see: The card becomes two columns only when its host is wide enough.
Example 2: Small sidebar variant
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Container Queries</title>
<style>
.host { container-type: inline-size; max-width: 240px; }
.card { background: #dcfce7; padding: 12px; }
@container (min-width: 360px) { .card { padding: 32px; } }
</style>
</head>
<body>
<div class="host"><article class="card">Compact card in a narrow host</article></div>
</body>
</html>Browser preview
What you see: Even in a wide browser, the narrow host keeps the compact padding.
When and Where to Use It
Use container queries for cards reused in both sidebars and full-width content regions.
Common Mistake or Useful Tip
The query styles descendants of the container; it does not use the queried element as its own size container.
Try It Yourself
Increase the second host max-width to 500px and inspect its padding.