It is... BUT!
In CSS, you can't transition a height: auto
to another height: auto
you HAVE to provide 2 values to transition between them. However, we can achieve - nearly - the same effect using padding!
.
let textContainer = document.querySelector(".content")
// Hover to change the content
textContainer.addEventListener("mouseover", function() {
textContainer.innerHTML = "Lorem Ipsum<br> is simply dummy text<br> of the printing and typesetting<br>"
textContainer.classList.toggle("animated")
})
// Back to its normal
textContainer.addEventListener("mouseout", function() {
textContainer.innerHTML = "Dummy Content</br>Another Line..."
textContainer.classList.toggle("animated")
})
.content {
width: 150px;
background: #e9e9e9;
transition: all 300ms ease;
padding: 0 10px;
}
.animated {
padding: 20px 10px;
}
<div class="content">
Dummy Content<br>
Another Line...
</div>
To get the best result, you can manipulate the following:
padding
transition-timing-function
This is probably the cleanest way to achieve what you need!
NOTE: There is no need to use height: auto
as we don't rely on it.