HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

CSS The object-fit Property

The object-fit property in CSS is used to specify how an <img> or <video> element should be resized to fit its container.

The CSS object-fit Property

It determines how the content of the replaced element (such as an image or video) should be resized to fit within its container box. Here are the possible values for the object-fit property:

  • fill: The replaced element is stretched to fill the content box. This may cause distortion of the image's aspect ratio.
  • contain: The replaced element is scaled to maintain its aspect ratio while fitting within the content box. It may leave some empty space within the container if the aspect ratios don't match.
  • cover: The replaced element is scaled to maintain its aspect ratio while completely filling the content box. This might result in cropping of the image if its aspect ratio doesn't match the container's.
  • none: The replaced element is not resized to fit the content box. Its size remains unchanged, and it may overflow the content box if it is larger.
  • scale-down: The content is sized as if none or contain, whichever results in a smaller concrete object size.

Using object-fit: fill;

Using object-fit: fill; means that the replaced element (in this case, an image) will be stretched to fill the container, disregarding its aspect ratio. Here's how you can use it:

Example

.container {
  width: 300px;
  height: 200px;
  border: 1px solid #ccc;
  overflow: hidden; /* Ensures that the image doesn't overflow the container */
}

img {
  width: 100%;
  height: 100%;
  object-fit: fill; /* Image is stretched to fill the entire container */
}           

Explanation:

Using object-fit: contain;

Using object-fit: contain; means that the replaced element (e.g., an image) will be scaled to maintain its aspect ratio while fitting entirely within the container without cropping. Here's how you can use it:

Example

.container {
  width: 300px;
  height: 200px;
  border: 1px solid #ccc;
  overflow: hidden; /* Ensures that the image doesn't overflow the container */
}

img {
  width: 100%;
  height: 100%;
  object-fit: contain; /* Image is scaled to fit entirely within the container */
}
             

Explanation:

Using object-fit: cover;

Using object-fit: cover; ensures that the image covers the entire container while maintaining its aspect ratio, even if it means some parts of the image will be cropped. Here's a more detailed example:

Example

.container {
  width: 300px;
  height: 200px;
  border: 1px solid #ccc;
  overflow: hidden; /* Ensures that the image doesn't overflow the container */
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* Image covers the entire container */
}
             

Explanation:

Using object-fit: none;

Using object-fit: none; means that the replaced element (e.g., an image) will not be resized to fit the container. Its size remains unchanged, and it may overflow the container if it is larger. Here's how you can use it:

Example

.container {
  width: 300px;
  height: 200px;
  border: 1px solid #ccc;
  overflow: hidden; /* Ensures that the image doesn't overflow the container */
}

img {
  object-fit: none; /* Image is not resized to fit the container */
}
             

Explanation:

Using object-fit: scale-down;

Using object-fit: scale-down; resizes the content as if the value were either contain or none, whichever results in a smaller concrete object size.

Example

.container {
  width: 300px;
  height: 200px;
  border: 1px solid #ccc;
  overflow: hidden; /* Ensures that the image doesn't overflow the container */
}

img {
  object-fit: scale-down; /* Resizes content as if it were either 'contain' or 'none' */
}

Explanation: