object-fit, object-positioning and absolute positioning

The image is a replaced element so the use of top/left/right/bottom will not work like it will do with a non-replaced element (a simple div for example). Here is the relevant parts from the specification: https://www.w3.org/TR/CSS2/visudet.html#abs-replaced-width https://www.w3.org/TR/CSS2/visudet.html#abs-replaced-height To make it easier the computed height/width of your image aren’t defined by the top/bottom and right/left values … Read more

CSS object-fit: contain; is keeping original image width in layout

What you have is logical, you simply need to understand how object-fit works. Let’s start with an easier example: .box { width:300px; height:300px; border:1px solid; } img { width:100%; height:100%; object-fit:contain; } <div class=”box”> <img src=”https://picsum.photos/300/200?image=1069″> </div> As you can see I have used a 300×200 image that I stretch inside the 300×300 box thus … Read more