1

I'm using the technique of transitioning the max-height instead of the height because of varying content height but I have a problem.

Inside the element that grows or shrinks I have an absolute positioned element that overflows, and I want to show that element when the parent element is expanded.

The problem is that if I only set the overflow hidden when the element is not expanded, it jumps because of the transition. Here's a pen where you can see it jump:

https://codepen.io/nicopavlotsky/pen/pozLBxY

with this code:

div{
  transition: all .5s;
  overflow: hidden;
}

div.active{
  overflow: visible;
  max-height: 10rem;
}

and here's another one where it doesn't have the jump but the children element is cut out:

https://codepen.io/nicopavlotsky/pen/yLBKrGW

div{
  transition: all .5s;
  overflow: hidden;
}

div.active{
  max-height: 10rem;
}

is there a JS less way to do this?

1 Answer 1

1

An alternative approach that may provide the desired result would be to transform: scaleY() as follows.

div {
   transform-origin: top;
   transform: scaleY(0);
}

div.active {
   transform: scaleY(1);
}

Here's an example:

document.querySelector('button').addEventListener('click', function() {
  document.querySelector('div').classList.toggle('active');
});
div {
  width: 200px;
  border: 2px solid red;
  transform-origin: top;
  transform: scaleY(0);
  transition: all .5s;
  position: relative;
}

div.active {
  transform: scaleY(1);
}

span {
  display: block;
  position: absolute;
  bottom: 3rem;
  right: -1rem;
  width: 2rem;
  border: 2px solid blue;
  height: 2rem;
}
<button>Toggle</button>
<div>
  <p>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. At porro veniam a iste deleniti possimus aliquam minima libero est blanditiis eligendi aut, eaque, quam sunt ducimus amet qui, quo aperiam!
  </p>
  <span></span>
</div>

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

1 Comment

Thanks for the idea, I ended up using a wrapper div with more width (100% + 3rem with margin-left: -1.5rem) so it has space for the overflow item to show.

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.