why isn't the toggle respected in transition
11 Replies
can you please reduce this further? i cant see any issues with it, and there's way too much code to read to try to see if there's anything wrong
i shared the code pen so you can live check
yeah, but it's a lot of code
and i cant see whats wrong with it
it seems to work just fine
im sorry
Transitioning max-height often has unexpected results as, whilst it appears to stop transitioning once it reaches the natural height of the content, the browser continues to transition the max-height value until it reaches the new value. This can can result in the timing appear different when transitioning between values, usually taking longer to "close" than to "open" due to the fact that we have to set a max-height to a value larger than actually required to ensure that all the content is displayed.
Also, hard-coding a max-height value isn't really a viable solution as it may be too small for smaller viewports or not enough for large ones.
However the issue is here is more due to the way that browsers toggle between "open" and not open states on the <details> element. It is either "open" or "not open". In some ways, is much like toggling between
display: block;
and display: none;
- i.e. there is no in-between state to allow transition.
When you initially click on the "summary" text to open the details you do see the transition due to the the content being displayed instantly but the transition delaying the time taken to reach the max height and opacity 1, thus revealing the content bit by bit.
However, when closing, it closes instantly so, whilst the transition is happening you can't actually see it as the details content has already closed.
Note - as far as I can see, the JavaScript isn't doing anything here that isn't being done by the CSS.i see what you are talking about
this is a good case to transition from 0 to auto
but that is still pretty new stuff
The data inside details Is sett from display auto to display none when toggling the detail?
There's a whole page about animating details and it's just ugh
I don't think that those are the specific properties but basically, yes. It is either open or closed.
There is no way currently to animate the details natively, at least not in a cross-browser way. Some methods might work on Chrome but will no doubt fail on other browsers. Of course if you consider this as progressive enhancement (ie "nice to have") then that wouldn't be an issue.
Kevin made a video a few months ago as to how this can be achieved on Chrome but even that need(ed) the latest version and with certain developer options being turned on (things may have moved forwards since then).
If the animation is important, you may have to look at more tradituonal accordion methods. These are simple to do with CSS alone but of course might not have the "advantages" of using the details element.
Kevin Powell
YouTube
Animate details & summary with a few lines of CSS
If you’ve ever used a details/summary, you’ve definitely wished there was an easy way to animate them opening and closing. Well, thanks to to modern CSS, this is becoming possible! And while browser support isn’t perfect for a lot of this, it’s the perfect example of a progressive enhancement.
🔗 Links
✅ Check out Zoran’s version on his CSS Week...
no, it's set from block to none and vice-versa
oh, and the contents arent just hidden: they completely stop existing for rendering - almost like an
inert
elementGeoff Graham posted an article on CSS Tricks just a couple days ago which has an alternative method to animating the details but again, it only works on Chrome
https://css-tricks.com/using-styling-the-details-element/