Frontend Masters Blog · 5 min readCSS & layout
The Compacting Sidebar Blues
Learn how to prevent infinite loops when compacting a navigation sidebar by using CSS container queries and style queries with calculated custom properties, avoiding fragile magic numbers. The approach calculates item heights dynamically and hides labels only when necessary, improving responsive design.
- Use container-type:scroll-state to detect sidebar overflow without causing layout loops
- Calculate nav item height with CSS custom properties and sibling-count for precise breakpoints
- Apply style queries to compare custom properties and hide labels when the sidebar is too short