Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I transition height: 0; to height: auto; using CSS?

I am trying to make a <ul> slide down using CSS transitions.

The <ul> starts off at height: 0;. On hover, the height is set to height:auto;. However, this is causing it to simply appear, not transition,

If I do it from height: 40px; to height: auto;, then it will slide up to height: 0;, and then suddenly jump to the correct height.

How else could I do this without using JavaScript?

#child0 {    height: 0;    overflow: hidden;    background-color: #dedede;    -moz-transition: height 1s ease;    -webkit-transition: height 1s ease;    -o-transition: height 1s ease;    transition: height 1s ease;  }  #parent0:hover #child0 {    height: auto;  }  #child40 {    height: 40px;    overflow: hidden;    background-color: #dedede;    -moz-transition: height 1s ease;    -webkit-transition: height 1s ease;    -o-transition: height 1s ease;    transition: height 1s ease;  }  #parent40:hover #child40 {    height: auto;  }  h1 {    font-weight: bold;  }
The only difference between the two snippets of CSS is one has height: 0, the other height: 40.  <hr>  <div id="parent0">    <h1>Hover me (height: 0)</h1>    <div id="child0">Some content      <br>Some content      <br>Some content      <br>Some content      <br>Some content      <br>Some content      <br>    </div>  </div>  <hr>  <div id="parent40">    <h1>Hover me (height: 40)</h1>    <div id="child40">Some content      <br>Some content      <br>Some content      <br>Some content      <br>Some content      <br>Some content      <br>    </div>  </div>
like image 909
Hailwood Avatar asked Aug 18 '10 02:08

Hailwood


People also ask

How do you make a transition height in CSS?

For animate the "height" of element with CSS Transitions you need use "max-height". If use the "height: auto", the effect not works. Is necessary some value for the CSS create a CSS animate, and you can use "max-height" with a great value for emulate this effect.

Does transition work on height?

We can't transition height , but we can transition max-height , since it has an explicit value. At any given moment, the actual height of the element will be the minimum of the height and the max-height .

How do you use transition properties?

The transition-timing-function property can have the following values: ease - specifies a transition effect with a slow start, then fast, then end slowly (this is default) linear - specifies a transition effect with the same speed from start to end. ease-in - specifies a transition effect with a slow start.

What is transition in CSS?

CSS transitions provide a way to control animation speed when changing CSS properties. Instead of having property changes take effect immediately, you can cause the changes in a property to take place over a period of time.


1 Answers

Use max-height in the transition and not height. And set a value on max-height to something bigger than your box will ever get.

See JSFiddle demo provided by Chris Jordan in another answer here.

#menu #list {      max-height: 0;      transition: max-height 0.15s ease-out;      overflow: hidden;      background: #d5d5d5;  }    #menu:hover #list {      max-height: 500px;      transition: max-height 0.25s ease-in;  }
<div id="menu">      <a>hover me</a>      <ul id="list">          <!-- Create a bunch, or not a bunch, of li's to see the timing. -->          <li>item</li>          <li>item</li>          <li>item</li>          <li>item</li>          <li>item</li>      </ul>  </div>
like image 71
jake Avatar answered Oct 16 '22 03:10

jake