Container Queries & Modern Responsive
Lesson 23 โข Advanced Track
What You'll Learn
๐ก Think of It Like This
Media queries ask: "How big is the screen?" Container queries ask: "How big is my parent?"This is a game-changer for reusable components. A card component can adapt its layout based on whether it's in a wide main content area or a narrow sidebar โ without knowing anything about the viewport.
| Feature | Media Query | Container Query |
|---|---|---|
| Measures | Viewport (screen) size | Parent container size |
| Reusability | โ ๏ธ Tied to page layout | โ Works anywhere |
| Syntax | @media (min-width:) | @container (min-width:) |
| Setup | None needed | container-type: inline-size |
Container Queries in Action
The same card component adapts its layout based on its container's width โ not the viewport.
Container Query Card
Same card in different-width containers
<!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>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
h1 { color: #3b82f6; margin-bottom: 10px; }
h2 { color: #22c55e; margin: 20px 0 10px; font-size: 1rem; }
/* Set up containment on the wrapper */
...Container Query Units
Container query units let you size things relative to the container, not the viewport.
Container Query Units
Size elements relative to their container
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Container Query Units</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
h1 { color: #3b82f6; margin-bottom: 20px; }
.demo-container {
container-type: inline-size;
background: #1e293b;
padding: 20px;
border-radius: 8px;
margin: 15px 0;
resize: horizontal;
...โ ๏ธ Common Mistakes
- Forgetting container-type โ Container queries only work if the parent has
container-type: inline-size(orsize). Without it, @container rules are ignored. - Using container queries on the element itself โ A container can't query its own size. The @container query checks the nearest ancestor with containment.
- Replacing all media queries โ Media queries are still useful for page-level layout (overall column count). Container queries are for component-level adaptation.
- Performance with size containment โ
container-type: size(both axes) requires the element to have explicit dimensions. Useinline-sizefor width-only containment.
๐ Lesson Complete
You now understand container queries โ the future of responsive design:
- โ Container queries style components based on their parent's size, not the viewport
- โ
Set up with
container-type: inline-sizeon the parent - โ
Use
@container (min-width: 400px)to write responsive component styles - โ Container units (cqw, cqi) size things relative to the container
Sign up for free to track which lessons you've completed and get learning reminders.