<details>
<summary>
What is the answer to the ultimate question of life, the universe, and everything?
</summary>
<p>42.</p>
</details>
<div class="unsupported">The animation of <code>details</code> does not work in your browser! 🙁 This is because the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/interpolate-size" target="_blank"><code>interpolate-size</code></a> property is not supported.</div>
details {
interpolate-size: allow-keywords;
margin-inline: 0.5rem;
padding: 0.25rem 1rem;
max-width: 50rem;
border-radius: 1rem;
box-shadow: 0 0 2px 0 currentColor;
}
/* closed state */
details::details-content {
height: 0;
overflow: hidden;
transition: content-visibility, height;
transition-duration: 750ms;
transition-behavior: allow-discrete;
}
/* open state */
details[open]::details-content {
height: auto;
}
summary {
cursor: pointer;
}
/* general styles */
* {
box-sizing: border-box;
margin: 0;
}
html {
color-scheme: dark light;
}
body {
width: 100%;
height: 100dvh;
font-family: system-ui, sans-serif;
font-size: clamp(1rem, 0.3043rem + 3.4783vw, 2.5rem);
display: grid;
grid-template-rows: auto 1fr;
place-items: center;
}
/* Provide a warning if interpolate-size is not suppored in the browser */
.unsupported {
grid-row: 1;
color: red;
background-color: hsl(0, 100%, 90%);
border: 1px red solid;
font-size: 1rem;
margin: 0.25rem;
padding: 0.2rem;
max-width: 600px;
a:visited {
color: blue;
}
}
@supports (interpolate-size: allow-keywords) {
.unsupported {
display: none;
}
body {
grid-template-rows: 1fr;
}
}
This animates the opening and closing of the details
element (disclosure widget) using CSS. It adds a more subtle transition between states.
This capability is unlocked through the addition of the following CSS properties/values: transition-behavior: allow-discrete
and interpolate-size: allow-keywords
to enable a discrete animation from a height of zero to auto. You can read this walkthrough of the code for a deeper understanding.
This demo currently only works in Chrome because of the limited browser support for the interpolate-size
property. However, it can be treated as a progressive enhancement.