When I try to append a child to an element, it increases the offset height of the element. Even when a transition is used, it just appends the child instantly. I just want to make a transition for that.
const p = document.createElement("p");
p.innerText = "Sample text";
document.getElementById("main").appendChild(p);
div#main {
transition: all 0.5s ease;
}
<div id="main"></div>
2
Answers
You need to append the element as invisible/hidden, then animate its transition to being visible. Also note that your current transition is targeting the
#main
element, which is always in the DOM as soon as the page loads, not thep
element you dynamically create.Below is a working example of how to do this with an
opacity
fade-in, although any method would work; height/width swipes, text scrolling, blurs etc.Note that the timeout is required to delay the removal of the class which hides the element until after it has been initially rendered to the DOM:
As I mentioned earlier and following the same idea of @Rory McCrossan you can use either height or font-size to set the transition as well