let parent = document.getElementById("parent");
let img = document.getElementById("img");
parent.addEventListener("click", function() {
parent.style.display = "none";
});
img.addEventListener("click", function(e) {
e.stopPropagation();
})
#parent {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background-color: blue;
}
#img {
width: 100%;
height: 100%;
object-fit: contain;
}
<div id="parent">
<img src="https://dummyimage.com/600x400/000/fff" id="img">
</div>
Basically, what I want to achieve is a pop-up that covers the whole window and has a centered image that stretches to the maximum height or width (depending on image dimension) and preserves aspect ratio.The full image should still be visible. When you click on the remaining visible parent blue background, both the parent and the image should disappear.
The parent event is not triggering. But if I remove the CSS code, the event triggers. What is happening here?
Since I couldn't find a simple solution using CSS (considering that the problem isn't that complicated), I have decided to use a quick fix using JavaScript by dynamically adjusting image sizing.
I'm leaving my answer here for anyone who needs this. But I'm still hoping for a CSS solution for this one.
The script dynamically changes the image sizing depending on the width and height of the window using onload and onresize. This way, I don't even need to know the dimensions of the image.