CSS The object-fit Property


The CSS property called object-fit is used to decide how to change the size of an<img> or <video> so that it fits well inside its container.


The CSS object-fit Property

The CSS property called object-fit is used to determine how to adjust the size of an <img> or <video> element so that it fits nicely inside its containing element.

This property instructs the content to fit inside a container in different ways. It can either maintain its original shape or stretch to occupy the available space fully.

Take a look at this picture from Paris. The picture is 400 pixels in width and 300 pixels in height.

Bridge

But, when we make the picture above narrower by half (200 pixels wide) and keep the height the same (300 pixels), it will appear like this:

Bridge

The picture is getting stretched or squeezed to fit into a box that is 200 pixels wide and 300 pixels tall. This makes the picture lose its original shape.

This is where we talk about the object-fit property. The "object-fit" property can have these values:

  • fill - This is default. The image is resized to fill the given dimension. 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
  • 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 scaled down to the smallest version of none or contain

Using object-fit: cover;

When we apply object-fit: cover; to an image, it retains its original shape and fills the specified size. However, parts of the image may be cut off to make it fit.

Bridge

Using object-fit: contain;

When we apply object-fit: contain; to an image, it maintains its original proportions while adjusting its size to fit within the specified dimensions.

Bridge

Using object-fit: fill;

When we apply object-fit: fill; to an image, it will be adjusted to completely occupy the specified dimensions. If needed, the image might be stretched or compressed to fit them.

Bridge

Using object-fit: none;

When we apply object-fit: none; to an image, it won't be resized:

Bridge

Using object-fit: scale-down;

When we apply object-fit: scale-down; to an image, it shrinks to the smallest size between 'none' or 'contain'.

Bridge

Another Example

We have two pictures, and we want them to take up half of the width of the web browser's window and fill the entire height of the window.

In this example, we don't use object-fit. So, when we make the browser window bigger or smaller, the pictures will lose their original shape.

In this next example, we apply object-fit: cover; to the code. This ensures that when we change the size of the browser window, the images maintain their original proportions.


 CSS object-fit More Examples

This example shows different ways to use the object-fit property in HTML with different values.


CSS Object-* Properties

Below is a table that shows the CSS properties related to objects:

Property Description
object-fit Describes how to change the size of an image or video to fit inside its containing element.
object-position Describes where to place an <img> or <video> using horizontal (x) and vertical (y) coordinates within its own content box.