The Historical Problem with CSS Height Transitions
For years, animating an element's height to fit its content was one of the most frustrating limitations in CSS. Developers resorted to JavaScript calculations, max-height hacks, or CSS Grid workarounds. Modern CSS now provides native solutions that finally make transitioning to height: auto straightforward.
Why Auto Height Can't Be Transitioned
The CSS transition property has always required computable numeric values to interpolate between. When you specify height: 0 to height: 200px, the browser knows exactly what values to calculate for each frame. However, height: auto is not a computed value--it's a keyword that tells the browser to calculate height based on content, which means there's no numeric value for the browser to animate between.
CSS transitions interpolate between values that can be calculated as numbers. When you declare height: auto, the browser determines the actual height by examining content, margins, padding, and other factors. This computed value only exists at render time and can change dynamically based on content, font loading, viewport size, or any number of factors. Without a stable numeric value, the browser cannot calculate intermediate frames for a smooth animation.
Understanding these foundational CSS concepts is essential for modern web development that leverages the latest browser capabilities to create smooth, performant user experiences.
As covered in CSS-Tricks' comprehensive guide to height transitions, this limitation has driven developers to creative but often brittle workarounds for years.
Modern CSS Solutions: calc-size() and interpolate-size
The calc-size() Function
The calc-size() function is the cornerstone of modern height animation. It accepts a keyword value like auto, min-content, max-content, or fit-content and returns its computed size, enabling transitions. When you write height: calc-size(auto), the browser calculates the actual numeric value at transition time, allowing smooth interpolation from any starting height.
This breakthrough in CSS animation capabilities complements other modern CSS features like CSS cascade layers for better style organization and specificity management.
According to Chrome's developer documentation, this function enables the browser to compute the intrinsic size at animation time, solving the fundamental limitation that prevented height animations for decades.
The interpolate-size Property
The interpolate-size property provides a simpler, declarative way to enable intrinsic keyword transitions. By setting interpolate-size: allow-keywords on an element, you enable transitions between numeric values and intrinsic keywords without needing the calc-size() wrapper. This property is particularly useful for simple use cases where you want consistent behavior across multiple elements.
This approach eliminates the need to wrap each auto value in calc-size(), making your CSS more maintainable and readable, especially when combined with responsive design best practices.
1.panel {2 height: 0;3 overflow: clip;4 transition: height 0.3s ease;5}6 7.panel.expanded {8 height: calc-size(auto);9}1.expandable {2 interpolate-size: allow-keywords;3 height: 0;4 transition: height 0.3s ease;5}6 7.expandable:hover {8 height: auto;9}Transitioning from display: none
Animating from display: none requires additional CSS features because display is a discrete property. Use @starting-style to define the initial state before the transition begins, and transition-behavior: allow-discrete to enable discrete property transitions.
The @starting-style rule allows you to specify the initial values for transitioning elements before they become visible, which is essential for smooth entry animations. Combined with transition-behavior: allow-discrete, this enables smooth transitions for properties that normally jump between values.
Performance is critical when animating elements that were previously hidden. These CSS-only solutions help you create fast-loading HTML pages by avoiding JavaScript-based height calculations that can impact rendering performance.
As demonstrated in the CSS-Tricks guide on display transitions, this pattern enables modal and panel animations that previously required JavaScript.
1.modal {2 display: none;3 height: 0;4 transition: height 0.3s ease;5 transition-behavior: allow-discrete;6}7 8.modal.open {9 display: block;10 height: auto;11}12 13@starting-style {14 .modal.open {15 height: 0;16 }17}Use overflow: clip
overflow: clip provides better performance than overflow: hidden during height transitions by avoiding new stacking contexts and reducing rendering overhead.
Respect prefers-reduced-motion
Always implement reduced motion media queries for users who experience discomfort from animations. Use `@media (prefers-reduced-motion: reduce)` to disable or minimize transitions.
Test Across Browsers
Chrome 129+ and Edge 129+ support these features natively. Implement progressive enhancement and fallbacks for Firefox and Safari users.
Common Use Cases
Accordions and Collapsible Sections
Accordions are one of the most common use cases for height animation. Whether expanding a FAQ section for better content organization, revealing additional product details, or creating a collapsible navigation menu, the calc-size() pattern provides clean, maintainable code without JavaScript height calculations.
Dropdown Menus
Dropdown menus benefit significantly from smooth height transitions, creating visual feedback that helps users understand the menu's expandability. The interpolate-size approach is particularly useful for navigation menus where you want consistent behavior across multiple dropdown elements. These animated interactions contribute to a polished user experience that keeps visitors engaged.
Expandable Cards and Panels
Expandable cards that reveal additional content on click or hover are popular in dashboards, content galleries, and product displays. The height transition creates a polished, professional feel while maintaining the intrinsic sizing behavior that adapts to different content lengths.
These patterns integrate seamlessly with modern web development practices, especially when combined with CSS best practices for maintainable, performant interfaces. Additionally, optimizing animation performance supports technical SEO goals by ensuring smooth Core Web Vitals metrics.
Frequently Asked Questions
Sources
- CSS-Tricks: Transitioning to Auto Height - Comprehensive guide covering historical context, code examples, and transition patterns
- Chrome for Developers: Animate to height:auto - Official documentation on calc-size(), interpolate-size, and browser compatibility