7

Trying to create an animated dropdown menu using CSS animation, without any JS. Thought I've been barking up the right tree but can't see where I'm going wrong, for this simplified menu item...

<div class="menu">Menu Item
    <ul>
        <li>Dropdown 1</li>
        <li>Dropdown 2</li>
        <li>Dropdown 3</li>
        <li>Dropdown 4</li>
        <li>Dropdown 5</li>
    </ul>
</div>

And the following CSS;

.menu ul {
    height: 0px;
    overflow: hidden;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    -o-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
}

.menu:hover ul {
    height: auto;
}

Thought that should successfully result in a scroll down of the div, but it just keeps appearing. Any thoughts? Cheers

2

4 Answers 4

11

See this topic for reference: How can I transition height: 0; to height: auto; using CSS?

To put it simply, you can not animate to height: auto;. It is not supported. If you have a pre-determined, fixed height, you can do it by animating to that specific value. 0px to 100px for instance. Auto however is not supported.

The first answer in the link above links to another article in which a sort of work-around is given. You may explore that for implementation on your site.

Can you use CSS3 to transition from height:0 to the variable height of content?

Sign up to request clarification or add additional context in comments.

2 Comments

Ah didn't realise that, want to steer away from js to reduce the sort of shuddering effect you get on old computers and mobile devices. Guess I'll probably stick to transforming to a pre-calculated height, which to be fair shouldn't be hard to calculate given each li have a fixed height. Cheers for that!
Not hard at all, and certainly an adequate thing to do. Calculating that height gets hairy if you are loading your navigation dynamically, which would then mess up your calculation once you modify the nav. Ideally you don't have to change the value manually. But if your nav isnt expected to change much if at all, I reckon most people can live with the issue.
4

You can't use CSS transitions with height:auto, only with specific values.

.menu:hover ul {
    height: 100px;
}

http://jsfiddle.net/mblase75/cWZMu/

1 Comment

Cheers for that, as I said above, probably just going to follow that advice
4

The animation for a dropdown can be implemented with pure CSS:

ul {
  overflow: hidden;
  transition: all .3s ease;
}

ul.folded {
  max-height: 0;
}

ul.unfolded {
  max-height: 300px; //value of max-height should always bigger than actual sum of all li's height
}

Comments

-1

I was able to build a solution similar to @artcher, but instead I used max-height: 100%; and that worked perfectly:

ul.submenu {
  overflow: hidden;
  transition: all .3s ease;
  max-height: 0;
}

.top-level-item:hover {
  .submenu {
    max-height: 100%;
  }
}

1 Comment

Nop you can't animate 0 -> 100% with max height. You were sure of this answer ? Can you provide a Codepen or assimilated ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.