Transitioning the Hard Cases: display, Height auto, @starting-style and allow-discrete
Transitions that used to need JavaScript: entry effects with @starting-style, exit effects for display: none with allow-discrete, overlay for popovers and dialogs, height: auto with interpolate-size and calc-size(), the grid-rows fallback, and details.
kvi5hnuCode Sprout team member7 min readOn this page
Three things used to make CSS transitions fail and push developers to JavaScript:
- Elements appearing, from
display: noneor newly added to the page, have no "before" style to transition from. - Elements disappearing with
display: nonevanish at once, so a fade-out never shows. - Sizes like
height: autocan't be interpolated.
Modern CSS handles all three.
Entry transitions: @starting-style
A transition needs an old value. When an element is first rendered, because it was just inserted or its display changed from none, there isn't one, so nothing transitions. Tested in Chrome: changing an element from display: none; opacity: 0 to display: block; opacity: 1, with a transition on opacity, created no transition; the element appeared instantly at full opacity.
@starting-style provides the values to transition from in that case:
.toast {
opacity: 1; translate: 0 0;
transition: opacity 0.3s, translate 0.3s;
}
@starting-style {
.toast { opacity: 0; translate: 0 1rem; } /* where it starts when it first appears */
}
Tested in Chrome, an element with this pattern, inserted with JavaScript, started a transition from opacity 0.
@starting-stylerules apply only on an element's first style update: when it's inserted, or when itsdisplaychanges fromnone. They don't affect later changes.- They can be nested inside the rule they belong to:
.toast { … @starting-style { opacity: 0; } }. - They follow normal specificity, so the starting rule must match the element as well as the rule it's starting from does. For an open state, write the full selector:
@starting-style { dialog[open] { opacity: 0; } }. - The element still needs a
transition;@starting-styleonly supplies the starting point.
Exit transitions: allow-discrete

display is discrete, so a change to none normally happens at once and cuts off any fade-out. transition-behavior: allow-discrete, written as a keyword in the shorthand, lets discrete properties take part in a transition:
.panel {
display: block; opacity: 1;
transition: opacity 0.25s, display 0.25s allow-discrete;
}
.panel.hidden { display: none; opacity: 0; }
@starting-style { .panel { opacity: 0; } } /* for the way back in */
How a discrete property switches with allow-discrete:
- Most discrete properties switch halfway. Tested in Chrome, with a linear transition,
justify-contentchanged fromstarttoendbetween 40% and 60%. With an easing likeease, "halfway" is measured on the eased progress, so it can happen earlier. displayandcontent-visibilityare special: they switch at the point that keeps the element visible the whole time. Entering,nonebecomesblockat the start. Exiting, the element staysblockuntil the end. Tested in Chrome: halfway through the exit above,displaywas stillblockand opacity was 0.5, and it was stillblockat 99.9%.- Without
allow-discrete, discrete properties don't transition at all. Tested in Chrome, ajustify-contentchange with onlytransition: justify-content 1screated no transition.
Firefox supports allow-discrete but not yet for display. There, the element hides at once, which is an acceptable fallback.
Popovers and dialogs: overlay
Popovers and modal dialogs live in the top layer (article 60). When they close, the browser removes them from the top layer immediately, which would put them behind other content during the exit transition. The overlay property, which only the browser can set, reflects that, and transitioning it with allow-discrete keeps the element in the top layer until the end:
[popover] {
opacity: 0; scale: 0.95;
transition: opacity 0.2s, scale 0.2s, display 0.2s allow-discrete, overlay 0.2s allow-discrete;
}
[popover]:popover-open { opacity: 1; scale: 1; }
@starting-style { [popover]:popover-open { opacity: 0; scale: 0.95; } }
The same pattern works for dialog[open] and its ::backdrop, as article 60 shows. overlay is supported only in Chromium. Other browsers ignore it, and the exit may briefly drop behind other content.
Keyframe animations
@keyframes (article 109) can also animate display, which switches as with allow-discrete, and don't need @starting-style, since an animation defines its own start. Transitions are usually simpler for open and close states because they reverse smoothly when interrupted.
Transitioning height: auto
Interpolating between 0 and auto needs the browser to know what auto would compute to. Tested in Chrome: from height: 0 to height: auto on a 100px-tall block, the transition didn't happen, and the height was already 100px halfway through.
interpolate-size: allow-keywords
Setting interpolate-size: allow-keywords allows transitions between a length and the sizing keywords auto, min-content, max-content and fit-content. It inherits, so set it once:
:root { interpolate-size: allow-keywords; }
.accordion-body { block-size: 0; overflow: clip; transition: block-size 0.3s ease; }
.accordion.open .accordion-body { block-size: auto; }
Tested in Chrome, the same block was 50px halfway through. It's opt-in because changing the default could break existing sites that unknowingly transition to auto.
calc-size()
calc-size(auto, size) is a size value that can take part in calculations: calc-size(auto, size + 2rem), or calc-size(fit-content, size / 2). It also transitions without interpolate-size; tested in Chrome, height: calc-size(auto, size) was 50px halfway, too. Use it when you need arithmetic on an intrinsic size.
Both work in Chrome 129. Safari has interpolate-size in preview, and Firefox has neither, so treat them as progressive enhancements: other browsers jump to the final size.
The grid-rows technique
For a cross-browser expanding section, transition grid-template-rows from 0fr to 1fr. Fractions do interpolate, and the row's size follows its content:
.expander { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 0.3s; }
.expander.open { grid-template-rows: 1fr; }
.expander > .inner { overflow: hidden; } /* min-block-size: 0 also works */
Tested in Chrome: with a 100px-tall content block, the grid was 50px tall halfway through.

The last two rows produce identical heights at every point: interpolate-size and the grid-rows technique reach the same result, but only the grid version works in every browser.
Animating <details>
<details> content is in the ::details-content pseudo-element (article 13), which can be transitioned. Its hidden state uses content-visibility: hidden, which is discrete, so it needs allow-discrete:
details::details-content {
block-size: 0; overflow: clip;
transition: block-size 0.3s, content-visibility 0.3s allow-discrete;
}
details[open]::details-content { block-size: auto; } /* with interpolate-size on :root */
::details-content works in all browsers since Firefox 143. The height animation needs interpolate-size; elsewhere, the content just appears.
:root { interpolate-size: allow-keywords; }
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
button { font: inherit; padding: 0.3rem 0.7rem; margin-block: 0.3rem; }
.panel {
padding: 0.6rem; border-radius: 8px; background: #f4efe6;
opacity: 1; translate: 0 0;
transition: opacity 0.3s, translate 0.3s, display 0.3s allow-discrete;
}
.panel.hidden { display: none; opacity: 0; translate: 0 -0.5rem; }
@starting-style { .panel { opacity: 0; translate: 0 -0.5rem; } }
.toast {
margin-block-start: 0.3rem; padding: 0.4rem 0.6rem; border-radius: 6px; background: #1d3557; color: white;
transition: opacity 0.3s, translate 0.3s;
@starting-style { opacity: 0; translate: 1rem 0; } /* nested form */
}
details { margin-block: 0.5rem; border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.4rem 0.6rem; }
details::details-content {
block-size: 0; overflow: clip;
transition: block-size 0.3s, content-visibility 0.3s allow-discrete;
}
details[open]::details-content { block-size: auto; }
details p { margin: 0.4rem 0 0; }
.expander { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 0.3s; }
.expander.open { grid-template-rows: 1fr; }
.expander > .inner { overflow: hidden; }
.expander p { margin: 0.3rem 0; }
@media (prefers-reduced-motion: reduce) { * { transition-duration: 0.01s !important; } }
The panel fades and slides in both ways, and is removed with display: none only after its fade-out. New toasts slide in from their @starting-style. The details element and the expander grow smoothly to their content's height.
What's current
@starting-styleandtransition-behavior: allow-discrete: all browsers. Firefox doesn't yet transitiondisplay.overlay: Chromium only.interpolate-sizeandcalc-size(): Chrome 129; Safari hasinterpolate-sizein preview.::details-content: all browsers.
Common questions
Why doesn't my element fade in when it's added to the page?
There's no previous style to transition from. Add an @starting-style rule.
Why does my element disappear before its fade-out finishes?
display: none applies at once. Add display 0.3s allow-discrete to the transition list.
How do I animate to height: auto?
interpolate-size: allow-keywords on :root in Chromium, or the grid-template-rows: 0fr to 1fr trick everywhere.
Why does my popover fall behind other content while closing?
It left the top layer at once. Transition overlay with allow-discrete.
Summary
@starting-stylesupplies the starting values for an element's first style, enabling entry transitions.allow-discretelets discrete properties transition;displaystays visible for the whole transition, switching at its start on entry and its end on exit.- Transition
overlaytoo for top-layer popovers and dialogs. interpolate-size: allow-keywordsandcalc-size()let sizes transition toauto;grid-template-rows: 0frto1frworks everywhere.::details-contentmakes<details>animatable.
Practice questions
Write the CSS for a .menu that fades in when its hidden class is removed and fades out before display: none applies.
Answer
.menu { opacity: 1; transition: opacity 0.2s, display 0.2s allow-discrete; }
.menu.hidden { display: none; opacity: 0; }
@starting-style { .menu { opacity: 0; } }
Why doesn't this rule make an open dialog fade in?
dialog { transition: opacity 0.3s, display 0.3s allow-discrete; }
dialog[open] { opacity: 1; }
@starting-style { dialog { opacity: 0; } }
Answer
The @starting-style rule's selector, dialog, is less specific than dialog[open], which sets opacity: 1, so the starting style loses. Use @starting-style { dialog[open] { opacity: 0; } }. Without an opacity: 0 on the closed dialog, there's no fade-out either.
Write a cross-browser expanding FAQ answer that animates its height.
Answer
.faq-answer { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 0.3s; }
.faq.open .faq-answer { grid-template-rows: 1fr; }
.faq-answer > div { overflow: hidden; }
What does interpolate-size: allow-keywords on :root change?
Answer
It's inherited, so throughout the page, transitions and animations between lengths and keywords like auto, min-content and fit-content interpolate instead of jumping.
Previous: Timing Functions: Easing Keywords, cubic-bezier(), steps() and linear()
Next: Keyframe Animations: @keyframes and the animation Properties

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.