What is `object-fit` and when to use it

The `object-fit` property in CSS is used to specify how the content of a replaced element, such as an `` or `

This property can take several values:

  • fill: This is the default value. The content is stretched to fill the container, regardless of its aspect ratio.
  • contain: The content will be scaled to maintain its aspect ratio while fitting within the container. It will fit entirely within the box.
  • cover: The content will be resized to cover the entire container, maintaining its aspect ratio, possibly cropping parts of the content that overflow.
  • none: The content will not be resized at all.
  • scale-down: The content will be sized as `none` or `contain`, whichever results in a smaller content size.

You should use `object-fit` when you want to control how images or videos are displayed within their parent containers, particularly when working with responsive designs.


Keywords: `object-fit` CSS web design responsive design image display