QUICK START:HTMLCSSJavaScriptTypeScriptPythonSQLReactNext.jsNode.jsDSASystem DesignDevOpsCybersecurityAI / ML
Intermediate 18 min readModule: Module 8: Responsive Design, Media & Container Queries

Container Queries (@container) & Responsive Design

Style components based on their parent container width rather than the global viewport with @container.

What You Will Learn in This Lesson

  • Mobile-first responsive design philosophy
  • Why Container Queries (@container) supersede viewport media queries for design systems
  • Declaring container-type: inline-size

Introduction & Core Concept

While media queries inspect the entire browser window width, Container Queries allow components to adapt based on the size of their parent container.
WHY DOES THIS MATTER IN THE REAL WORLD?

A card component placed in a narrow sidebar can display vertically, while the same card in a wide main area displays horizontally, automatically!

Modern Container Query Component

css
css
1
2
3
4
5
6
7
8
9
10
.card-container {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
display: flex;
flex-direction: row;
}
}

Line-by-Line Technical Breakdown

1Container queries make components independently responsive anywhere on the page.

Try It Yourself (Interactive Editor)

Modify the code in real-time and click Run to test live browser output and console logs.

Intelligent Code Runner & Live Sandbox[CSS]
CSS SOURCE EDITOR
Interactive Live Code

Industry Best Practices & Professional Standards

  • Use container queries for self-contained UI widgets.

Lesson Summary & Core Takeaways

  • Container queries deliver component-driven responsive layouts.