Before setting innerHTML, add some class to the container that sets the state of the preliminary animation via CSS, then set innerHTML and delete this class. if the container has a transition, it must be animated to clear the state.
.container { transition: all 1s; max-height: 300px; } .container.pre-animation { opacity:0; max-height: 0; }
setTimeout to make sure the effect is more noticeable
var innerHTMLString = '<h1> I am H1 </h1>'; var container = document.querySelector('.container') container.classList.add('pre-animation'); container.innerHTML = innerHTMLString +'ravi'; setTimeout(function(){ container.classList.remove('pre-animation') },100)
source share