I am currently facing a challenge in adding a face overlay to an image while ensuring that the face remains within the inner div.
The image is required to fit over a 16/9 ratio backdrop and also stay within the outer container. I have managed to achieve this when the outer div has a greater height than the 16/9 ratio, but the issue arises when it drops below 16/9 as it cuts off the top and bottom of the head.
This needs to be functional on IOS where aspect-ratio does not work on Safari, leading me to resort to using the padding-top solution for 16/9.
If anyone can provide assistance or guidance, it would be greatly appreciated.
My desired outcome looks something like this...
https://i.sstatic.net/vvc9g.png
However, what I'm currently experiencing is this...
https://i.sstatic.net/FysoW.png
.outside {
position: relative;
width: 500px;
height: 200px;
background: red;
overflow: hidden;
}
.inside {
height: 0;
overflow: hidden;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding-top: 56.25%;
background: blue;
}
.img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: contain;
}
<h1>Works when height is bigger</h1>
<div class="outside" style="height:400px!important">
<div class="inside" style="padding-top: 56.25%;">
<img src="https://solvemoji-test.s3.eu-west-2.amazonaws.com/unnamed.jpg" class="img">
</div>
</div>
<h1>Does not work when height is smaller</h1>
<div class="outside">
<div class="inside" style="padding-top: 56.25%;">
<img src="https://solvemoji-test.s3.eu-west-2.amazonaws.com/unnamed.jpg" class="img">
</div>
</div>