1. Home
  2. Coding Hub
  3. Coding Notes
  4. Css
  5. Container Queries
45% OFFBLACK FRIDAY SALE
Sale ends in
03Days
12Hrs
21Mins
31Secs
Claim
  1. Home
  2. Coding Hub
  3. Coding Notes
  4. Css
  5. Container Queries

Limited time offer

45% OFF

BLACK FRIDAY SALE

Sale ends in

03Days
12Hrs
21Mins
31Secs
Claim
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Underrated CoderUnderrated Coder
Underrated CoderUnderrated Coder
Resources
  • Coding Hub
  • Roadmap
  • DSA Sheet
  • Coding Notes
  • Coding Quiz
  • Code Compiler
  • Coder AI
Interview Preparation
  • 1:1 Mentorship
  • Interview Q&A
Community
  • Blog
  • Coder Community
Help & Support
  • Contact Us
  • Troubleshooting
  • Report a Bug
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Outline
CSS IntroductionCSSOM & ParsingBasic SelectorsCSS CombinatorsAttribute SelectorsThe CascadeSpecificity RulesCSS Inheritance
Box Modelbox-sizing ResetMargin CollapsePadding & BordersLogical PropertiesSizing UnitsViewport UnitsMath Functions
Web FontsVariable FontsFluid TypographyText FormattingText OverflowModern Colorscolor-mix() FunctionCSS Gradients
Display PropertyPosition MechanicsSticky PositioningStacking ContextAnchor PositioningOverflow ControlFilters & ShadowsAspect & object-fit
Pseudo-ClassesPseudo-Elements:is() & :where():has() SelectorCustom Properties@property RuleNative Nesting
Flexbox ArchitectureFlex DirectionAxis AlignmentFlex WrappingFlex SizingGap & Ordering
Grid ArchitectureGrid TracksGrid PlacementTemplate AreasAuto-Fit GridsCSS Subgrid
Media QueriesContainer QueriesContainer UnitsPreference QueriesCascade Layers@scope Rule
CSS TransitionsKeyframe AnimationsScroll AnimationsView TransitionsRendering Performance
CSS

Container Queries

Overview

Media Queries have a fatal architectural flaw: they only know the width of the ENTIRE browser window. Imagine building a 'Product Card' component. If it's in the main content area, it has plenty of space. But if you drop that exact same card into a narrow 250px Sidebar, it completely breaks, even if the user is on a 4K Desktop monitor! Container Queries (@container) fix this. They allow a component to style itself based purely on the physical size of its parent container, completely ignoring the viewport. This is the ultimate form of Component-Driven Design.

Syntax

css
/* 1. Define the Container */
.sidebar {
  /* You MUST explicitly tell the browser this is a container! */
  container-type: inline-size;
  /* Optional: Give it a name for complex nesting */
  container-name: sidebar-zone;
}

/* 2. Style the Component based on the Container's size! */
.product-card {
  display: flex;
  flex-direction: row;
}

/* If the .sidebar ever shrinks below 400px... */
@container sidebar-zone (max-width: 400px) {
  .product-card {
    /* Stack the card vertically, because it's being squished! */
    flex-direction: column; 
    font-size: 0.8rem;
  }
}

Common Pitfalls

  • Forgetting to declare container-type: inline-size; on the parent. If you do not explicitly register the parent as a container, the browser will completely ignore any @container queries inside it.
  • Setting container-type globally on the <body>. Container queries require the browser to perform complex ResizeObserver loops natively. Applying them universally to massive DOM elements will absolutely decimate your rendering performance.

Interview Questions

Why can't you use container-type: size; to query BOTH width and height easily?

Because of infinite loops! If the container queries its own height, and then changes its child's font size... that font size changes the height of the container! The browser will infinitely loop and crash. inline-size (Width only) is the safe, standard way to use Container Queries.

Real-World Example

Building a perfectly modular Profile Component that looks great in a massive hero section AND in a tiny navbar dropdown, using the exact same HTML.

example
css
.profile-wrapper { container-type: inline-size; }

.profile-avatar { width: 100px; height: 100px; }

/* If the wrapper is squished into a tiny navbar... */
@container (width < 300px) {
  .profile-avatar {
    width: 40px; /* Shrink the avatar drastically! */
    height: 40px;
  }
  .profile-bio { display: none; } /* Hide the text entirely */
}

Check Your Knowledge

Test your understanding of Container Queries with these quick questions.

Question 1

Which declaration is mandatory on a parent element before you can use an `@container` query to style its children?

Take CSS Quiz
Outline
CSS IntroductionCSSOM & ParsingBasic SelectorsCSS CombinatorsAttribute SelectorsThe CascadeSpecificity RulesCSS Inheritance
Box Modelbox-sizing ResetMargin CollapsePadding & BordersLogical PropertiesSizing UnitsViewport UnitsMath Functions
Web FontsVariable FontsFluid TypographyText FormattingText OverflowModern Colorscolor-mix() FunctionCSS Gradients
Display PropertyPosition MechanicsSticky PositioningStacking ContextAnchor PositioningOverflow ControlFilters & ShadowsAspect & object-fit
Pseudo-ClassesPseudo-Elements:is() & :where():has() SelectorCustom Properties@property RuleNative Nesting
Flexbox ArchitectureFlex DirectionAxis AlignmentFlex WrappingFlex SizingGap & Ordering
Grid ArchitectureGrid TracksGrid PlacementTemplate AreasAuto-Fit GridsCSS Subgrid
Media QueriesContainer QueriesContainer UnitsPreference QueriesCascade Layers@scope Rule
CSS TransitionsKeyframe AnimationsScroll AnimationsView TransitionsRendering Performance
CSS

Container Queries

Overview

Media Queries have a fatal architectural flaw: they only know the width of the ENTIRE browser window. Imagine building a 'Product Card' component. If it's in the main content area, it has plenty of space. But if you drop that exact same card into a narrow 250px Sidebar, it completely breaks, even if the user is on a 4K Desktop monitor! Container Queries (@container) fix this. They allow a component to style itself based purely on the physical size of its parent container, completely ignoring the viewport. This is the ultimate form of Component-Driven Design.

Syntax

css
/* 1. Define the Container */
.sidebar {
  /* You MUST explicitly tell the browser this is a container! */
  container-type: inline-size;
  /* Optional: Give it a name for complex nesting */
  container-name: sidebar-zone;
}

/* 2. Style the Component based on the Container's size! */
.product-card {
  display: flex;
  flex-direction: row;
}

/* If the .sidebar ever shrinks below 400px... */
@container sidebar-zone (max-width: 400px) {
  .product-card {
    /* Stack the card vertically, because it's being squished! */
    flex-direction: column; 
    font-size: 0.8rem;
  }
}

Common Pitfalls

  • Forgetting to declare container-type: inline-size; on the parent. If you do not explicitly register the parent as a container, the browser will completely ignore any @container queries inside it.
  • Setting container-type globally on the <body>. Container queries require the browser to perform complex ResizeObserver loops natively. Applying them universally to massive DOM elements will absolutely decimate your rendering performance.

Interview Questions

Why can't you use container-type: size; to query BOTH width and height easily?

Because of infinite loops! If the container queries its own height, and then changes its child's font size... that font size changes the height of the container! The browser will infinitely loop and crash. inline-size (Width only) is the safe, standard way to use Container Queries.

Real-World Example

Building a perfectly modular Profile Component that looks great in a massive hero section AND in a tiny navbar dropdown, using the exact same HTML.

example
css
.profile-wrapper { container-type: inline-size; }

.profile-avatar { width: 100px; height: 100px; }

/* If the wrapper is squished into a tiny navbar... */
@container (width < 300px) {
  .profile-avatar {
    width: 40px; /* Shrink the avatar drastically! */
    height: 40px;
  }
  .profile-bio { display: none; } /* Hide the text entirely */
}

Check Your Knowledge

Test your understanding of Container Queries with these quick questions.

Question 1

Which declaration is mandatory on a parent element before you can use an `@container` query to style its children?

Take CSS Quiz

CSS coding notes for Indian students — 62 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in CSS

  1. CSS Introduction
  2. CSSOM & Parsing
  3. Basic Selectors
  4. CSS Combinators
  5. Attribute Selectors
  6. The Cascade
  7. Specificity Rules
  8. CSS Inheritance
  9. Box Model
  10. box-sizing Reset
  11. Margin Collapse
  12. Padding & Borders
  13. Logical Properties
  14. Sizing Units
  15. Viewport Units
  16. Math Functions
  17. Web Fonts
  18. Variable Fonts
  19. Fluid Typography
  20. Text Formatting
  21. Text Overflow
  22. Modern Colors
  23. color-mix() Function
  24. CSS Gradients
  25. Display Property
  26. Position Mechanics
  27. Sticky Positioning
  28. Stacking Context
  29. Anchor Positioning
  30. Overflow Control
  31. Filters & Shadows
  32. Aspect & object-fit
  33. Pseudo-Classes
  34. Pseudo-Elements
  35. :is() & :where()
  36. :has() Selector
  37. Custom Properties
  38. @property Rule
  39. Native Nesting
  40. Flexbox Architecture
  41. Flex Direction
  42. Axis Alignment
  43. Flex Wrapping
  44. Flex Sizing
  45. Gap & Ordering
  46. Grid Architecture
  47. Grid Tracks
  48. Grid Placement
  49. Template Areas
  50. Auto-Fit Grids
  51. CSS Subgrid
  52. Media Queries
  53. Container Queries
  54. Container Units
  55. Preference Queries
  56. Cascade Layers
  57. @scope Rule
  58. CSS Transitions
  59. Keyframe Animations
  60. Scroll Animations
  61. View Transitions
  62. Rendering Performance

Study next

  • All coding notes
  • CSS notes home
  • CSS certification quiz
  • Online compiler
  • Interview Q&A

CSS coding notes for Indian students — 62 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in CSS

  1. CSS Introduction
  2. CSSOM & Parsing
  3. Basic Selectors
  4. CSS Combinators
  5. Attribute Selectors
  6. The Cascade
  7. Specificity Rules
  8. CSS Inheritance
  9. Box Model
  10. box-sizing Reset
  11. Margin Collapse
  12. Padding & Borders
  13. Logical Properties
  14. Sizing Units
  15. Viewport Units
  16. Math Functions
  17. Web Fonts
  18. Variable Fonts
  19. Fluid Typography
  20. Text Formatting
  21. Text Overflow
  22. Modern Colors
  23. color-mix() Function
  24. CSS Gradients
  25. Display Property
  26. Position Mechanics
  27. Sticky Positioning
  28. Stacking Context
  29. Anchor Positioning
  30. Overflow Control
  31. Filters & Shadows
  32. Aspect & object-fit
  33. Pseudo-Classes
  34. Pseudo-Elements
  35. :is() & :where()
  36. :has() Selector
  37. Custom Properties
  38. @property Rule
  39. Native Nesting
  40. Flexbox Architecture
  41. Flex Direction
  42. Axis Alignment
  43. Flex Wrapping
  44. Flex Sizing
  45. Gap & Ordering
  46. Grid Architecture
  47. Grid Tracks
  48. Grid Placement
  49. Template Areas
  50. Auto-Fit Grids
  51. CSS Subgrid
  52. Media Queries
  53. Container Queries
  54. Container Units
  55. Preference Queries
  56. Cascade Layers
  57. @scope Rule
  58. CSS Transitions
  59. Keyframe Animations
  60. Scroll Animations
  61. View Transitions
  62. Rendering Performance

Study next

  • All coding notes
  • CSS notes home
  • CSS certification quiz
  • Online compiler
  • Interview Q&A