I'm using vanilla JS and CSS. I would like to add a pancake (individual PNGs) on top of the previous pancake every time I click on the pancake button ().
However, I'd the pancake to stack on top of each other, instead of flying over the previous. I've tried to do negative margins for the img element in .css, as well as negative height for the
element in .css, but to no success. How can I achieve a neatly stacked pancake?
Current outcome of pancake stack
Desired Outcome
var count = 0;
var imageArray = [
"img/pancake plate 1.png",
"img/pancake straweberry 1.png",
"img/pancake 1.png",
"img/pancake 2.png",
"img/pancake 3.png",
"img/pancake 4.png"
]; // image array oragnised from bottom to top
function addPancake() {
var pancakeImage = document.createElement("img"); // create the <img> tag
pancakeImage.setAttribute("src", imageArray[count]); // insert img src="current item" as <img> src
var insertArea = document.getElementById("pancake-area"); // specify pancake-area <div>
insertArea.insertBefore(document.createElement("br"), insertArea.childNodes[0]); // add <br> before every new image to show the "stacking" effect
insertArea.insertBefore(pancakeImage, insertArea.childNodes[0]); // add pancake image before the previous image
count++;
};
* {
background-color: beige;
font-size: 50px;
}
button {
font-size: 200px;
margin-left: 25vw;
}
#pancake-area {
margin-left: 140px;
}
img {
width: 200px;
position: relative;
}
<div id="pancake-area">
</div>
<button onclick="addPancake()"></button>