CHAPTER 84

CSS Container Queries

Learn how components can respond to the size of their containing element rather than the viewport.

01. What is CSS Container Queries?

Container queries allow components to respond to their container size.
They are useful for reusable components that appear in different layouts.
They can make component-based responsive design more flexible.

02. Syntax

.container { container-type: inline-size; } @container (min-width: 500px) { .card { display: flex; } }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>

.wrapper {
    container-type: inline-size;
    max-width: 700px;
}

.card {
    padding: 25px;
    background: #dbeafe;
}

@container (min-width: 500px) {

    .card {
        display: flex;
        gap: 20px;
        align-items: center;
    }

}

</style>
</head>
<body>

<div class="wrapper">

    <div class="card">

        <h2>AIFA</h2>

        <p>
            Responsive component.
        </p>

    </div>

</div>

</body>
</html>

04. Important Points

✓ Container queries respond to container size.
✓ container-type enables container query behavior.
✓ They are useful for reusable components.
✓ They complement viewport media queries.

05. Common Mistakes

⚠ Forgetting to define the container.
⚠ Using container queries when a viewport query is simpler.
⚠ Not testing components in different parent sizes.

06. Real World Use

Container queries are useful for reusable cards, dashboards, sidebars and component-based design systems.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Create a Responsive Component

Create a card container that changes from block to flex when its container becomes wide enough.