CSS Object Fit
The object-fit CSS property sets how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container.
Why use object-fit?
By default, images stretch to fill their width and height, often distorting the aspect ratio. object-fit allows you to control this behavior, similar to how background-size works for background images.
Key Values
- fill: The default. The image is resized to fill the given dimensions. If necessary, the image will be stretched or squished to fit.
- contain: The image keeps its aspect ratio, but is resized to fit within the given dimension. The image will be letterboxed if the aspect ratio doesn't match.
- cover: The image keeps its aspect ratio and fills the given dimension. The image will be clipped to fit.
- none: The image is not resized.
- scale-down: The image is sized as if none or contain were specified, whichever results in a smaller concrete object size.
Common Use Cases
User Avatars: Use cover to ensure profile pictures fill a square or circular container without distortion.
Hero Images: Use cover for full-width banner images to ensure they cover the screen area regardless of device width.
