RSS FeedTwitterMastodonBlueskyShare IconHeart IconGithub IconArrow IconClock IconGUI Challenges IconHome IconNote IconBlog IconCSS IconJS IconHTML IconShows IconGit IconSpeaking IconTools IconShuffle IconNext IconPrevious IconCalendar IconCalendar Edit Icon
details { inline-size: 50ch; interpolate-size: allow-keywords;
&::details-content {s opacity: 0; block-size: 0; overflow-y: clip;  transition: content-visibility 1s allow-discrete, opacity 1s, block-size 1s; }
&[open]::details-content { opacity: 1; block-size: auto; } }
My google avatar.4 min read

Open & Close Transitions with <details>

css

CSS interpolate-size is rad. Let's use it with the <details> element so this element can join other HTML elements in elegant presentation.

By far, the most common use case I keep seeing for interpolate-size is to transition the <details> element when it opens and closes.

This is your boilerplate starting place.

The Setup #

Start with a <details> element:

<details>
  <summary>Details interpolation example</summary>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Aliquid dolores, distinctio ipsum veritatis magni soluta maiores rerum optio, possimus animi eligendi architecto sed placeat quasi quibusdam, nihil odio. Amet, tempore.</p>
</details>

Next, it needs a max-inline-size (or placement inside a grid) to prevent the content from being as wide as the page:

details {
  inline-size: 50ch;
}

Next, add interpolate-size to the element so we can transition to block-size: auto:

details {
  @media (prefers-reduced-motion: no-preference) {
    interpolate-size: allow-keywords;
  }
}

Now the tricky parts that make this post valuable.

Select the slot of <details> with ::details-content and describe the closed styles. Take special note to transition: content-visibility 1s allow-discrete:

details {
  
  &::details-content {
    opacity: 0;
    block-size: 0;
    overflow-y: clip; 
    transition: content-visibility 1s allow-discrete,
                opacity 1s,
                block-size 1s;
  }
}

Lastly, add the open styles.

details {
  
  &[open]::details-content {
    opacity: 1;
    block-size: auto;
  }
}

So technically, we're not transitioning the <details> element, we're transitioning the slotted content that goes inside it.

🤯

The boilerplate #

Take this and go make awesome things!

details {
  inline-size: 50ch;
  
  @media (prefers-reduced-motion: no-preference) {
    interpolate-size: allow-keywords;
  }
  
  &::details-content {
    opacity: 0;
    block-size: 0;
    overflow-y: clip; 
    transition: content-visibility 1s allow-discrete,
                opacity 1s,
                block-size 1s;
  }
  
  &[open]::details-content {
    opacity: 1;
    block-size: auto;
  }
}

The Demo #

Try it on this page!

What is my favorite CSS color?

deeppink!

Note that this effect needs transition-behavior, ::details-content and interpolate-size to work.

Send me a link to the rad <details> elements you make with this‽

Mentions #

Join the conversation on

56 likes
10 reposts
  • Tony Ward
  • Phlip ???????? ????
  • Triptych ????
  • Apple Annie :prami:
  • Ben Schwarz
  • Fogrew :verified: ????????
  • Mario
  • Fynn Becker
  • Schalk Neethling
  • Nick McLaren
If I'm hiding rows of a table with an adjacent checkbox, is there a way to interpolate them coming and going? eg .toggle_planned:not(:checked) ~ table tr.planned {display:none;}
Kevin MarksKevin Marks
you could use View Transitions or entry/exit animation CSS with @starting-style and transition-behavior developer.chrome.com/blog/entry-e...
Adam ArgyleAdam Argyle

@argyleink excellent use case. It's indeed a really common use case. Thanks for sharing it. I need to catch up on all the new CSS properties ????

Side note, nothing alarming, but your content seems to be truncated on Android Firefox. I can't scroll horizontally or zoom out to get the content.

Geoffrey Crofte ☕Geoffrey Crofte ☕
You forgot to state up front what "transition" you mean. Is the panel expected to roll open slowly? (I use but no transitions because we do an engineering app)
Phlip ???????? ????Phlip ???????? ????
I do it slowly so you know the boilerplate worked, you know the hooks are present to customize to your liking. I agree the demo is slow, and I agree I don't state up front why that is ????
Adam ArgyleAdam Argyle

@geoffreycrofte Looks like a case of the pre that won't max inline size. Those stinkers been trouble. Firefox Mobile? Know which version?

Adam ArgyleAdam Argyle
niche memes
JanneJanne

@argyleink Eager to have this available across all browsers. So many designer expect this in their designs, it'll be a great day when all that JS can go away!

Also good to know about content-visibility, I kind of know about how it affects things but never thought of it as part of a <details>'s content.

A side note: you mix block-size and height, I'm assuming that's just an oversight?

Christopher Kirk-NielsenChristopher Kirk-Nielsen
Ohh that is nice, but was more curious to transform details summary in a real tab control. Since semanicatlly the seem to make sense. Like this codepen.io/RYJASM/pen/e... I saw something similar in your screenshots.
Stefan BauerStefan Bauer
oh yes, you could build this with carousel and disable the smooth scroll animation. still get all the accessibility goodness.
Adam ArgyleAdam Argyle

Crawl the CSS Webring