The object-fit
property in CSS specifies how the content of an element, such as an image or video, is scaled to fit its container. It is useful for fitting images into containers with fixed dimensions.
Examples of using object-fit
:
img { width: 300px; height: 200px; object-fit: cover; }
object-fit
values:
fill
- default value, stretches the image to fill the container.contain
- scales the image to fit within the container, maintaining its aspect ratio.cover
- scales the image to cover the container, maintaining its aspect ratio, may crop the image.none
- the image is not scaled and is displayed at its original size.scale-down
- scales the image to the smaller of eithernone
orcontain
.
Example:
<img src="example.jpg" style="object-fit: cover;">