The simple trick to transition from height 0 to auto with CSS

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 มิ.ย. 2024
  • Animating or transitioning to and from height auto is, well, not really possible (though it is being worked on!), but luckily, there is actually a solution using CSS Grid that’s really easy to implement!
    🔗 Links
    ✅ Keith J. Grant’s article on this (also includes a flexbox solution): keithjgrant.com/posts/2023/04...
    ✅ The first simple example: codepen.io/kevinpowell/pen/po...
    ✅ The accordion: codepen.io/kevinpowell/pen/NW...
    ⌚ Timestamps
    00:00 - Introduction
    00:20 - The setup
    01:55 - The transition
    02:20 - It works with more content too
    02:45 - How I discovered this
    03:10 - Using it for an accordion
    #css
    --
    Come hang out with other dev's in my Discord Community
    💬 / discord
    Keep up to date with everything I'm up to
    ✉ www.kevinpowell.co/newsletter
    Come hang out with me live every Monday on Twitch!
    📺 / kevinpowellcss
    ---
    Help support my channel
    👨‍🎓 Get a course: www.kevinpowell.co/courses
    👕 Buy a shirt: teespring.com/stores/making-t...
    💖 Support me on Patreon: / kevinpowell
    ---
    My editor: VS Code - code.visualstudio.com/
    ---
    I'm on some other places on the internet too!
    If you'd like a behind the scenes and previews of what's coming up on my TH-cam channel, make sure to follow me on Instagram and Twitter.
    Twitter: / kevinjpowell
    Codepen: codepen.io/kevinpowell/
    Github: github.com/kevin-powell
    ---
    And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!

ความคิดเห็น • 504

  • @glenn_myridia
    @glenn_myridia ปีที่แล้ว +212

    This is gold. I spent a while building an accordion in react a couple of weeks ago and had to resort to refs to calculate the height dynamically. Thanks Kevin!

    • @eeespartak
      @eeespartak ปีที่แล้ว +2

      Same! That's just amazing!

    • @chotai
      @chotai ปีที่แล้ว +4

      Tears in my eyes with glorious smile. It's so relatable

    • @demetrx7972
      @demetrx7972 ปีที่แล้ว +1

      Same 😂

    • @yishayhazan1040
      @yishayhazan1040 ปีที่แล้ว

      @@demetrx7972 +1

    • @unknownuser1762
      @unknownuser1762 ปีที่แล้ว +1

      Same😅😅

  • @oxuhs-gy4xm
    @oxuhs-gy4xm ปีที่แล้ว +112

    Make sure to add "grid-row: 1 / span 2;" on the child element. Otherwise the child will animate at a diffrent speed than the parent.

    • @DevZoom7
      @DevZoom7 11 หลายเดือนก่อน +4

      Thank you man, I saw that behavior and I was about to ask about it, this is 10 times better.

    • @fatihbulut9567
      @fatihbulut9567 11 หลายเดือนก่อน +4

      This is a critical detail, should be even pinned.

    • @mandokir
      @mandokir 10 หลายเดือนก่อน +1

      How does this even work? There should be nothing to span since there is only one row...

    • @oxuhs-gy4xm
      @oxuhs-gy4xm 10 หลายเดือนก่อน +7

      @@mandokir when transitioning from 0 to 1fr you will get fractional values. The grid system will create a second row to fill up the remaining space to get back to 1fr. So with grid-template-rows: .3fr; you will get a second row that fills the remaining .7fr. It's still weird how the div reacts to the size change because the overall height will shrink to the fraction while it's also divided into the two rows, but... it works

    • @thejaredwilcurt
      @thejaredwilcurt 5 หลายเดือนก่อน +1

      Bless your heart Oxuhs, savior of the day

  • @waffle-codes-js
    @waffle-codes-js ปีที่แล้ว +61

    You have literally fulfilled your tagline goal with me. I may not be great at css yet but I’ve definitely fallen in love with css and front end work.

  • @pascalmaranus
    @pascalmaranus ปีที่แล้ว +88

    This is absolutely brilliant!
    Having to "kind of" do it with max-height, etc. is always not quite what you want.

    • @MrW0rDs
      @MrW0rDs ปีที่แล้ว +2

      Give my secret sauce transition from 0 to auto a try (I haven't seen this trick anywhere):
      tab.style.height = 'auto';
      const height = tab.offsetHeight;
      tab.style.height = 0;
      tab.style.height = height + 'px';
      And of course a css transition on tab. Don't ask me why but as far as I can tell it does the job perfectly.

    • @dabbopabblo
      @dabbopabblo ปีที่แล้ว +3

      @@MrW0rDs You're setting its height to be auto, recording the height it takes up in pixels, then setting its height to 0px, and finally setting its height to the recorded height. Transitions can occur between two pixel values but not auto because sometimes auto is calculated on a subsequent layout refresh. But being that that is the case sometimes your method will fail and capture a height of 0 and your element wont transition at all, or worse if the element has padding the padding will be included in the height you capture and make the element larger than it should, and also your element becomes unresponsive because it has a hard coded pixel value which is very bad for complex UI's and not what auto is supposed to do at all. With the method described in the video none of that is the case, it is a perfect solution. Also if you don't understand how something as simple as that works yet naively use it and suggest others do too, you should take the time to learn the fundamentals and actually understand the code you are writing or you'll make the mistake of using bad code that causes rippling issues through your work you cant even begin to diagnose without the understanding of what its doing.

    • @MrW0rDs
      @MrW0rDs ปีที่แล้ว

      @@dabbopabblo Thanks for your input! I'm not sure how offsetHeight can ever return a height of 0 since the height is set to 'auto' beforehand, can you elaborate? I made it a simple example but you can see how the transition is suppose to happen from 0 to XXpx, try on a codepen. If responsiveness is a problem you can switch back to auto once the transition is completed, and vice-versa when the tab is toggled. The tab content wrapper must not have a padding, this is not a deal breaker whatsoever. Been using this for 3+ years and haven't seen it fail a single time on any browser, nor has this been reported to me. What I would like to know is why the browser doesn't repaint before the transition happen (probably because 0 => auto => 0 happens so fast, but there must be a technical explanation)

    • @joshuafountain
      @joshuafountain ปีที่แล้ว +3

      ​@@MrW0rDs This is how I do it, it's extremely simple.
      const contentContainer = < selector here >;
      contentContainer.style.maxHeight = isOpen ? `${contentContainer.offsetHeight}px` : '0';

  • @i_am_ergo
    @i_am_ergo ปีที่แล้ว +3

    What an awesome little niblet of knowledge. Thank you, Chris, Keith, and, of course, Kevin! Stay awesome.

  • @LP...
    @LP... ปีที่แล้ว

    Omg this is awesome! I had to do an accordion in the job recently and had to use JS to calc the size of elements times their numbers to achieve the same effect. That's why I love this channel!

  • @patfox2201
    @patfox2201 ปีที่แล้ว +9

    It's amazing just how happy this made me -- I'm just about done with a project where I had wanted to have this effect on the menu but had given up trying to find a non-horrible solution. Two minutes' work and it's done, looks perfect.
    I know that it's likely no-one but me (not even the client) will even notice the difference, but just the knowledge that it's there gives me untold satisfaction.
    Thanks Kevin!

  • @p_o_z_e
    @p_o_z_e 7 หลายเดือนก่อน

    Totally forgot about this video and came across it again just now since i was on the lookout for exactly this, since i need it for a project. Amazing as always Kevin!

  • @rogerpence
    @rogerpence ปีที่แล้ว +1

    Kevin, your channel has been _extremely_ helpful to me. This technique is solid gold. Thank you!

  • @danielosthues5229
    @danielosthues5229 8 หลายเดือนก่อน

    Wow - I love this. It saves me lots of code to build a more / less toggle and it's the most straight-forward solution I've seen so far.

  • @petarkolev6928
    @petarkolev6928 11 หลายเดือนก่อน +1

    The passion everyone can see with you're doing the videos with is priceless, that makes the videos even more valuable!

  • @KeithGrant
    @KeithGrant ปีที่แล้ว +3

    Hey Kevin, thanks for the shout-out! I’m glad you’re as excited about this as I am 😄

  • @user-wx3bf7qt9d
    @user-wx3bf7qt9d ปีที่แล้ว

    This is absolutely amazing! I was struggling with this issue for a while. This solution is amazingly creative and simple. Thank you

  • @user-bk3gy9sw4b
    @user-bk3gy9sw4b ปีที่แล้ว

    I have searched for this effect for so long but always ended up with some compromised version. It works like charm! Thanks you for sharing.

  • @gabrielpfgm
    @gabrielpfgm 11 หลายเดือนก่อน

    This is so SO HELPFUL! My collapsible components are now perfect! Had to use some javascript to change overflow to visible once the transition is complete, because I'm using dropdown inside, but the overall result is top notch! Thank you!

  • @nephiw
    @nephiw 10 หลายเดือนก่อน

    I was so happy to see this when you first published it and I just used it again today. As a matter of fact, I made it a collection of helper classes that I can apply to anything and I plan to replace all of my collapsible blocks with this.

  • @artemeelemann317
    @artemeelemann317 5 หลายเดือนก่อน

    Been struggling with this for years, thanks for sharing such an easy solution!

  • @whizzbang7520
    @whizzbang7520 ปีที่แล้ว

    You helped me out a lot, since I had to build accordions at work today. Others have already built accordions in other projects but I really didn't like the implementation since it involved JS and a fixed height. This solution is so smooth, thanks!

  • @AmrouBouaziz
    @AmrouBouaziz 2 หลายเดือนก่อน

    Man ! you saved the day!
    I remember watching this video as it was released, and today I came back to watch it again because I had the same use case.

  • @DrewLytle
    @DrewLytle ปีที่แล้ว +1

    Absolute *game changer!!!* Great video Kevin!

  • @charlesst-yves711
    @charlesst-yves711 ปีที่แล้ว

    Finally it has been done. Congrats on this acheivement!🏆and thanks for sharing.

  • @Enes-ik4bm
    @Enes-ik4bm 10 หลายเดือนก่อน

    Thank you so much. I needed that and now my accordion menu works perfectly.

  • @wzt9376
    @wzt9376 ปีที่แล้ว

    Man I love the way you are so so happy about it. Thanks for the video.😊

  • @morphx666
    @morphx666 ปีที่แล้ว +2

    Just today I got assigned a task to implement an FAQ where you see the question and, upon clicking on it, the answer gets revealed, with a nice expanding animation. Lucky me, I watched your video before implementing it!
    Thank you, Kevin!

  • @mathis922
    @mathis922 ปีที่แล้ว

    OMG! This is the solution i searching for months. Currently i using the max-height trick or i calc the exact height via js. But this is so simple. I Love it!! Thank you Kevin!

  • @napapt
    @napapt ปีที่แล้ว +1

    Kevin... I was struggling with a clanky animation I have on my project due to layout shifts... this trick has saved the day, there was no better become aware of it than now

  • @Nashiuz
    @Nashiuz 8 หลายเดือนก่อน

    Thanks a bunch Kevin! I love your tutorials, always down for some tips and tricks from you.

  • @ahsath
    @ahsath ปีที่แล้ว +5

    This is sick! I was having a problem where the font sizes changes with breakpoints throwing away the height i calculated initially, this fixes and I don't have to worry about setting hard height values

  • @SCGekko
    @SCGekko ปีที่แล้ว

    This is so cool. And it works! Thank you so much for sharing!

  • @Dellaran
    @Dellaran ปีที่แล้ว

    Where was this half a year ago when I needed it for my job!? Thank you for great educational tips and tricks as usual!

  • @the-real-tridder
    @the-real-tridder ปีที่แล้ว

    I was doing this with the columns the other day for a slide out panel, but for some weird reason I've never thought to go vertical! Thanks for the awesome content Kevin!

  • @winns87x
    @winns87x 6 หลายเดือนก่อน

    Amaizing find, my new vue accordion is ready to rock 💪. Thanks!

  • @JmenDem
    @JmenDem ปีที่แล้ว

    Thank you! You are the best! I've been looking for sth like this for about 7 years!🎉🎉🎉

  • @heitoralthmann
    @heitoralthmann 11 หลายเดือนก่อน

    Thanks Kevin! Been looking for something like this for a long time!

  • @JeremyFriesensGoogleProfile
    @JeremyFriesensGoogleProfile ปีที่แล้ว

    I just watched this video 2 days ago. And today, lo and behold... I needed to do exactly this. Fantastic timing! It works like a charm and it definitely would not have occurred to me that this can be done, were it not for this great video! Thank-you!!

  • @alexg6953
    @alexg6953 หลายเดือนก่อน

    This is so good! Thank you Kevin.

  • @stefanyordanov7052
    @stefanyordanov7052 ปีที่แล้ว

    Another amazing video! Thank you so much 🙌

  • @timgreller
    @timgreller ปีที่แล้ว +1

    this is so cool! why didn't I hear about this sooner? It was a pain for every website I built so far, but not anymore! thanks :)

  • @jmnoob1337
    @jmnoob1337 8 หลายเดือนก่อน

    WOW! This is awesome! I'll use this in a couple projects I've been working on!

  • @NicTacks
    @NicTacks 15 วันที่ผ่านมา

    This clip was very helpful! Having a dynamic height for a div with that smooth transition is exactly what I was looking for my REACT custom component.

  • @SEWebDesign
    @SEWebDesign ปีที่แล้ว

    This is awesome, thank you for re-re-sharing!

  • @luisretzbach8210
    @luisretzbach8210 ปีที่แล้ว

    This is so amazing. Spend many hours to calculate the height of my accordion items dynamically with js after searching for a good css solution. Now after a few lines the magic is done. So great. Thanks a lot!

  • @lukawalli
    @lukawalli ปีที่แล้ว

    Genius! Thank you for sharing, fantastic solution! :)

  • @jus.juraev
    @jus.juraev 3 หลายเดือนก่อน

    Thank you Kevin! You helped me a lot!

  • @jugibur2117
    @jugibur2117 ปีที่แล้ว

    Wow, really great that this is now possible, thanks for sharing!

  • @jameshuggins2520
    @jameshuggins2520 4 หลายเดือนก่อน

    Short and so sweet… thank you Kevin!

  • @tenc6491
    @tenc6491 ปีที่แล้ว

    Superb as always. Thank you.

  • @tas_codes
    @tas_codes ปีที่แล้ว

    Ive been looking for something like this for a while and omg having such a simple native CSS solution is amazing

  • @wendanny6732
    @wendanny6732 ปีที่แล้ว +2

    This is super useful !! I'll use this on my job, thank you Kevin

    • @T25de
      @T25de ปีที่แล้ว

      Tech job must be great
      Hook a brother up 😂

  • @jonmayer
    @jonmayer ปีที่แล้ว

    Incredible! Simply simple, but so amazing.

  • @DonAlonzo
    @DonAlonzo 7 หลายเดือนก่อน

    Fantastic, thanks for sharing!

  • @karim3445
    @karim3445 ปีที่แล้ว

    Thank you for sharing this brilliant trick, no more js no more headaches

  • @nuhelsyed2258
    @nuhelsyed2258 ปีที่แล้ว

    This is brilliant! Thank you Kevin

  • @berk473
    @berk473 ปีที่แล้ว

    This comes at a perfect time, thank you! I will be faced with this problem soon and would have wasted much time for a more or less reasonable solution

  • @gE0013
    @gE0013 ปีที่แล้ว

    You bring joy to the world

  • @grandmaster-yo-yo
    @grandmaster-yo-yo 3 หลายเดือนก่อน

    Brother this is brilliant! I've spent so much time trying to build a Sidebar with animated width while collapsing...I tried animating font-size to 0 and it worked, but not quite as I wanted, and this solution is exactly what I needed!!!!!!!! Thank you so much!

  • @StemLG
    @StemLG 3 หลายเดือนก่อน

    this is so dope, i just needed it in a project and was referred to this vid by some guy in a stackoverflow question
    genuinely thank you!!!

  • @henriquemoura9570
    @henriquemoura9570 8 หลายเดือนก่อน

    So grateful! ⭐️

  • @tatof2
    @tatof2 ปีที่แล้ว

    this is amazing! nice find :D

  • @user-px5lo9nc5t
    @user-px5lo9nc5t ปีที่แล้ว

    I was literally stuck trying to solve the faq accordion challenge by Frontend Mentor with CSS only. It felt almost impossible til this vid notification popped up in front of me!
    Thanks man, you came just in the perfect time!

  • @ollio9582
    @ollio9582 ปีที่แล้ว

    this is a gamechanger.Brilliant. Thanks a lot!

  • @tusharghildiyal6814
    @tusharghildiyal6814 9 หลายเดือนก่อน +1

    damn i was trying to figure out effective way to create this animation for last 2 weeks and you just made it so easy for me. Thanks Kevin

  • @AndiDrajan
    @AndiDrajan 6 หลายเดือนก่อน

    Brilliant! Thank you

  • @NBGdeki
    @NBGdeki ปีที่แล้ว

    Great trick, thanks for sharing!

  • @nguyenhoanglong1331
    @nguyenhoanglong1331 ปีที่แล้ว

    It really make my day. Thank you guys

  • @beefmaestro5468
    @beefmaestro5468 2 หลายเดือนก่อน

    This is a massive game changer. Thank you!

  • @johannesherbert6729
    @johannesherbert6729 ปีที่แล้ว

    I had this problem today at work and I couldn't get it to work properly. Checking youtube, this video comes up and fixes my problem! Thanks alot!

  • @joe-skeen
    @joe-skeen ปีที่แล้ว

    Wow! I was trying to do this with the width of a sidebar just a couple days ago and gave up. Now I know how to do it! Thanks!

  • @jakubschulz4353
    @jakubschulz4353 ปีที่แล้ว

    Cool! ❤ I always had to create a complicated component for such things

  • @cyeg12c
    @cyeg12c ปีที่แล้ว

    This is a great tip. Thanks man ❤️

  • @felipebarth
    @felipebarth 3 หลายเดือนก่อน

    This is awesome!! I can finally stop using JS to define innerHeight at div's style!!! Thanks so so much!

  • @mehboob_pythonist
    @mehboob_pythonist 8 หลายเดือนก่อน

    Kevin you are my favorite mentor. Stay blessed 💜

  • @SirNoviTheChauvi
    @SirNoviTheChauvi 11 หลายเดือนก่อน

    Awesome! Thanks, buddy.

  • @yashrajjaiswal9941
    @yashrajjaiswal9941 8 หลายเดือนก่อน

    This was really helpful , I have used this trick in my projects and it's really convenient

  • @anthonyiu
    @anthonyiu ปีที่แล้ว

    Thats exactly what i wanted. I was struggling with the height auto issue in React, because auto won't trigger any css animation, making everything so awkward. Now you saved my life.

  • @martijnhalekor
    @martijnhalekor ปีที่แล้ว

    This kinda stuff gets me up in the morning. Awesome Kevin, thanks for sharing! Accordion-ception incoming.

  • @QwDragon
    @QwDragon ปีที่แล้ว

    That's great!!! Love it!

  • @pulok9909
    @pulok9909 ปีที่แล้ว

    It is super useful because I got it when I really need it. I'm gonna it to my client’s project now. Cheers!

  • @rileygriffith2055
    @rileygriffith2055 ปีที่แล้ว

    Thank you for sharing this. You're awesome

  • @pavanchalla3787
    @pavanchalla3787 9 หลายเดือนก่อน

    thanks kevin really helped me alot

  • @fatihbulut9567
    @fatihbulut9567 11 หลายเดือนก่อน

    You are a brilliant man, thank you so much.

  • @millermcdonell291
    @millermcdonell291 ปีที่แล้ว

    You always surprise me with your tips and tricks.

  • @bartek4210
    @bartek4210 11 หลายเดือนก่อน

    Amazing, thanks a lot!

  • @karlgustav9960
    @karlgustav9960 ปีที่แล้ว +1

    🎉 Wow, it took 10 years to come up with a css only solution. I remember using the max-height hack for accordions in 2013… this is amazing.

  • @sergejkukshaus2788
    @sergejkukshaus2788 10 หลายเดือนก่อน

    This is crazy! I've been struggling today with exact this requirement. Ending up with transitioning the min-height from 0 to e.g. 400px and setting the height from 0 to auto. I've also added the scaleY() transform, which actually looks very awesome. But from my point of view, this is a much cleaner solution than setting the min-height to a random value. Thanks for sharing this cool trick.

  • @LouieSonugan21
    @LouieSonugan21 ปีที่แล้ว

    Great discovery! Thanks Kevin.

  • @shenaldesilva4198
    @shenaldesilva4198 7 หลายเดือนก่อน

    Helped a lot..Thanks❤❤✌🏻✌🏻

  • @AzulaAlwaysLies2461
    @AzulaAlwaysLies2461 4 หลายเดือนก่อน

    I figured this out once, and forgot what I did. Thank you so much for reminding me!!!! You are awesome. This is awesome :)

  • @moulayyoussefasakour769
    @moulayyoussefasakour769 ปีที่แล้ว

    Awesome, awesome, awesome. Thank you Kevin

  • @mundomusicaearte
    @mundomusicaearte ปีที่แล้ว

    Really clever way of doing it, thanks.

  • @icaromendes1250
    @icaromendes1250 ปีที่แล้ว

    This is gold. Thanks!

  • @dariuszrodzewicz
    @dariuszrodzewicz ปีที่แล้ว +1

    Thanks Kevin, you are a top: G; and a life saver.
    I have been using the min-height hack for a while which was ok but was problematic since when you put a higher min-height it takes long time for the transition to complete, but this is just amazing, you are a wizard 🧙‍♂

  • @blackpurple9163
    @blackpurple9163 ปีที่แล้ว +4

    This is great, now I don't need to use extra unnecessary javascript to animate these transitions anymore, thanks to people like you that make this so much simpler

    • @whirled_peas
      @whirled_peas ปีที่แล้ว

      You wouldn't have needed to use JS to perform the animation/transition though. The minimum implementation before this was to find the height value and set it, but to use transition: height ...;

  • @user-ci6pr6oy6q
    @user-ci6pr6oy6q 28 วันที่ผ่านมา

    Thanks man, you are the best!

  • @shanejohnson4546
    @shanejohnson4546 ปีที่แล้ว

    This is unbelievable!!!! Thank you so much!

    • @shanejohnson4546
      @shanejohnson4546 ปีที่แล้ว

      My mind is blown.... it's be JS 100% to achieve this for so many years...

  • @ob5804
    @ob5804 ปีที่แล้ว

    Big thanks to Keith and you, Kevin))🙂

  • @siphamandlambokazi331
    @siphamandlambokazi331 ปีที่แล้ว

    Broe I had be struggling for so long with this that I endedup using JS everything time I need an accordion.This is very cool.

  • @return_undefine
    @return_undefine ปีที่แล้ว

    I have came back to say a big thank you. I watched this video the very day it was uploaded and this is the 7th day. I'm so excited for this hack, it's actually an eye opener. I stopped struggling with drop-down after watching this... I'm currently working on a project and this hack really helped me. I implemented this trick on my project and it works just very cool, no stress 😊. Thanks allot for such a helpful and quality content. Your smiles and expression as whole while teaching gives confidence 💚

  • @deadlock107
    @deadlock107 ปีที่แล้ว

    amazing stuff! Thanks!!!!

  • @Z3ROR
    @Z3ROR ปีที่แล้ว

    This is great. Will use it for sure!