The object-fit property is used to specify how an <img> or <video> should be resized to fit its container.
Normally, if you set a fixed width and height on an image, the browser might stretch it and ruin the aspect ratio. object-fit allows you to control the cropping and scaling behavior.
Here is how you apply the object-fit property to an image. Imagine you have a container that is 400x300 pixels, but your image is square.
.container {
width: 400px;
height: 300px;
}
.container img {
width: 100%;
height: 100%;
/* This makes the image fill the container nicely without stretching */
object-fit: cover;
}
none or contain.object-position alongside object-fit to decide which part of the image is visible when it is cropped (e.g. object-position: top center;).