1

I have the following html and css code that works exactly as I expect it to.

function toggle() {
  var el = document.getElementById("panel");
  el.className = el.className == "expanded" ? "collapsed" : "expanded";
}
.collapsed {
  height: 0px;
}
.expanded {
  height: 500px;
}
#panel {
  overflow: hidden;
  transition: height 1s linear;
}
<p id="panel" class="expanded">
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
  software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it
  has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search
  for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
<button onClick="toggle()">Toggle Panel</button>

Clicking on the button will slide up or slide down the panel with a 1 second height transition effect.

However, if I replace the expanded height 500px with auto, inherit or initial, the height transition no longer takes effect. The panel just snaps between 0px and the original height.

I plan to have text of various length, and it will not be feasible to custom define a height for every single paragraph. Is there a CSS only approach to still do the sliding animation without explicitly defining the height of the panel?

Ricky Ruiz
  • 25,455
  • 6
  • 44
  • 53
John
  • 32,403
  • 80
  • 251
  • 422
  • You can check the `max-height` alternative, but be careful with using high values there. Here's another alternative [jsFiddle](https://jsfiddle.net/zveqx61k/), setting the `height` of the element inline with javascript. – Ricky Ruiz Nov 11 '16 at 20:16

0 Answers0