🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to CSS Notes
Topic #150

CSS object-fit


CSS object-fit Property

The CSS object-fit property is used to specify how an or

This property can take one of the following values:

  • fill - This is default. Does not preserve the aspect ratio. The image is resized to fill the container (the image will be stretched or squeezed to fit).
  • cover - Preserves the aspect ratio, and the image fills the container. Cuts overflowing content if needed.
  • contain - Preserves the aspect ratio, and fits the image inside the container, without cutting - leaves empty space if needed.
  • none - The image is not resized.
  • scale-down - the image is scaled down to the smallest version of none or contain.

Using object-fit: fill;

The object-fit: fill; value does not preserve the aspect ratio, and the image is resized to fill the container (the image will be stretched or squeezed to fit):

Paris

Example

  .image-container {
  width: 200px;
  height: 300px;

  border: 1px solid black;
  margin-bottom: 25px;
}

.image-container img {

  width: 100%;
  height: 100%;
  object-fit: fill;
}

Using object-fit: cover;

The object-fit: cover; value preserves the aspect ratio, and the image fills the container. The image will be clipped to fit:

Paris

Example

  .image-container {
  width: 200px;
  height: 300px;

  border: 1px solid black;
  margin-bottom: 25px;
}

.image-container img {

  width: 100%;
  height: 100%;
  object-fit: cover;
}

Using object-fit: contain;

The object-fit: contain; value preserves the aspect ratio, and fits the image inside the container, without cutting - will leave empty space if needed:

Paris

Example

  .image-container {
  width: 200px;
  height: 300px;

  border: 1px solid black;
  margin-bottom: 25px;
}

.image-container img {

  width: 100%;
  height: 100%;
  object-fit: contain;
}

Using object-fit: none;

The object-fit: none; value does not resize or scale the image:

Paris

Example

  .image-container {
  width: 200px;
  height: 300px;

  border: 1px solid black;
  margin-bottom: 25px;
}

.image-container img {

  width: 100%;
  height: 100%;
  object-fit: none;
}

Using object-fit: scale-down;

The object-fit: scale-down; value scales the image down to the smallest version of none or contain:

Paris

Example

  .image-container {
  width: 200px;
  height: 300px;

  border: 1px solid black;
  margin-bottom: 25px;
}

.image-container img {

  width: 100%;
  height: 100%;
  object-fit: scale-down;
}

Another Example

Here we have two images and we want them to fill the width of 50% of the browser window and 100% of the height of the container.

In the following example we do NOT use object-fit, so when we resize the browser window, the aspect ratio of the images will be destroyed:

Example

<div>
<div style="width:100%;height:300px;">
<img alt="Norway" src="rock600x400.jpg" style="float:left;width:50%;height:100%;"/>
<img alt="Paris" src="paris.jpg" style="float:left;width:50%;height:100%;"/>
</div>
<br/>
</div>

In the next example, we use object-fit: cover;, so when we resize the browser window, the aspect ratio of the images is preserved:

Example

<div>
<div style="width:100%;height:300px;">
<img alt="Norway" src="rock600x400.jpg" style="float:left;width:50%;height:100%;object-fit:cover;"/>
<img alt="Paris" src="paris.jpg" style="float:left;width:50%;height:100%;object-fit:cover;"/>
</div>
<br/>
</div>

CSS object-* Properties

The following table lists the CSS object-* properties:

Property Description
object-fit Specifies how an or
object-position Specifies how an or

Want to go beyond the notes?

Join CodingNow 2.0's CSS course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

CSS object-fit – FAQs

Quick answers about learning CSS object-fit in CSS.

This free note from CodingNow 2.0 explains CSS object-fit in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every CSS topic on CodingNow 2.0, including CSS object-fit, is 100% free with no signup required.
With focused practice, most students grasp CSS object-fit in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now