For object-fit the image itself requires width and height . In OP CSS, images do not have width and / or height, so fitting objects cannot work.
Hint: width and height should NOT be the size of the image itself! Think of it as a div : if you want the div fill your container, you will set
width:100%; height:100%;
... and the browser will know that this div should completely fill the container space.
In the case of img browser takes two steps:
- The browser creates a bounding box : by default, the dimensions of the rectangle will be the exact dimensions of the image itself. But we can tell the browser that the image size should be 100% of the width of the container and 100% of the height of the container. Then it will create a box that completely fills the container space.
- The browser places the image pixels in this field : by default, the image will be compressed / stretched so that the image width matches the block width and the image height matches the block height. But with
object-fit you can choose how to match image sizes and frames . For example, using the object-fit:cover commands to enlarge / reduce the image to completely fill the field, while maintaining its aspect ratio.
As for the OP, I would just set:
main > section.posts > article > img { width: 100%; height: 100%; object-fit: cover; }
Last caveat: when using% values ββto determine the size, the container must have a certain width and height in order for the object to work. The OP would have to define height in main > section.posts > article .
The conspiracy
source share