I am struggling to position a div or button so that it matches the dimensions of the image it contains, without exceeding the dimensions of the parent div(s).
Despite my efforts, I can't seem to get it right for both vertical and horizontal images. When I adjust the box for the horizontal image, the vertical one ends up overflowing at the bottom.
It's important to note that I have no control over the image itself as it may come in various aspect ratios and sizes. Additionally, the outermost div is determined by the user's window size.
function log() {
console.log("Click!");
}
.container {
height: 100%;
width: 50%;
display: block;
padding: 0;
background-color: lightgreen;
}
.zoom {
display: block;
max-height: max-content;
max-width: max-content;
height: 100%;
width: 100%;
margin: auto;
background-color: aqua;
}
img {
max-height: 100%;
max-width: 100%;
height: 100%;
width: 100%;
object-fit: contain;
}
<div style="height: 500px; display: flex; flex-direction: row; overflow: scroll; background-color: lightgray">
<div class="container">
<div class="zoom" onclick="log()">
<img src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Eiffel_Tower_Vertical.JPG" />
</div>
</div>
<div class="container">
<div class="zoom" onclick="log()">
<img src="https://upload.wikimedia.org/wikipedia/commons/f/f6/Eiffel_tower_at_dawn_horizontal.jpg" />
</div>
</div>
</div>
Desired outcome:
In this scenario, I have two divs - container (green) and zoom (aqua). The size of the container is dependent on the user's window size and the zoom div contains an image that should fill as much of the container's area while maintaining its aspect ratio and being centered. This might result in some empty space around the sides or top/bottom of the picture based on the image's aspect ratio. I need the size and position of the zoom div to match exactly with the image as an event handler is attached to it.
The green section represents the container while the aqua section displays either a portrait or landscape image. The goal is to ensure that the dimensions and positioning of zoom align perfectly with the image.