I have a container element (chocolate color on screenshot), that contains a grid of tiles 3x2. The tiles contains image element with 16/9 resolution image and some header and footer. The tiles span can be 1-3 with 4px gap in between. The image is set to object-fit:contain, The container element (chocolate color on screenshot) takes 100% width and height of parent (grey color), which causes, that the image tiles ratio do not matches the image ratio, and the image has bars around (blue aqua color on picture). I need to scale the container element (chocolate color), so its content will gets such dimensions, that the image element will perfectly fit.
Here is stackblitz. I've tried to play with the aspect-ratio of the image parent, which worked for medium sized resolutions, but not for small or big, then the image gets the aqua bars again.
For simplification, the stackblitz contains only one image element tile, which should perfectly fit into the chocolate element. You can ignore the grid inside the image.
https://stackblitz.com/edit/angular-ivy-xu92pf?file=src/app/app.component.css
Current (chocolate container takes 100% width and height from parent):
expected (chocolate container has width and height ratio, so that its image content 16/9 will perfectly fit. In real world, the image content will have not fixed w/h ratio):