CSS object-fit Property

The CSS object-fit property controls how the content of a replaced element, such as an image or video, is resized to fit its content box.

It determines whether the content stretches, preserves its aspect ratio, fills the available space, or remains at its natural size. The property is commonly used for image galleries, thumbnails, profile pictures, cards, and responsive media layouts.

object-fit Syntax

The object-fit property accepts one keyword that determines how the replaced content fits inside its box.

Basic Syntax

selector {
  object-fit: cover;
}

Available Values

img {
  object-fit: fill;
}
img {
  object-fit: contain;
}
img {
  object-fit: cover;
}
img {
  object-fit: none;
}
img {
  object-fit: scale-down;
}

CSS-Wide Keywords

img {
  object-fit: initial;
}
img {
  object-fit: inherit;
}
img {
  object-fit: unset;
}
img {
  object-fit: revert;
}
img {
  object-fit: revert-layer;
}

The initial value is fill. This stretches the content to match the element's content box, potentially changing its original aspect ratio.

Property Values

Value Description
fill Stretches the content to fill the element's content box. The original aspect ratio may change.
contain Scales the content uniformly so the entire object fits inside the content box while preserving its aspect ratio.
cover Scales the content uniformly to cover the entire content box while preserving its aspect ratio. Some content may be clipped.
none Does not resize the content to fit the box. The content retains its intrinsic dimensions when available.
scale-down Uses whichever produces the smaller concrete object size: none or contain.
initial Resets the property to fill.
inherit Uses the computed value from the parent element.
unset Resets the property to fill because it is not inherited.
revert Reverts the property according to cascade origin rules.
revert-layer Reverts the property according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value fill
Inherited No
Applies to Replaced elements
Computed value As specified
Animation type Discrete
Common elements <img> and <video>

How It Works

The object-fit property controls the size of the replaced content within an element's content box. It does not directly determine the dimensions of the element itself.

Define the Element's Dimensions

img {
  width: 300px;
  height: 200px;
  object-fit: cover;
}

The image element is 300 pixels wide and 200 pixels tall. The image content is scaled to cover that area while preserving its aspect ratio.

Preserve the Entire Image

img {
  width: 300px;
  height: 200px;
  object-fit: contain;
}

The entire image fits inside the box. If the image and box have different aspect ratios, unused space remains.

Fill the Box Completely

img {
  width: 300px;
  height: 200px;
  object-fit: fill;
}

The image content stretches to match both dimensions, potentially causing distortion.

Aspect Ratio

The aspect ratio describes the relationship between an object's width and height. The contain, cover, and scale-down values preserve the content's intrinsic aspect ratio when one is available.

Important Consideration

The property is most noticeable when the element's content box and the original media have different aspect ratios. If they already match, several values may produce the same visible result.

fill Value

The fill value stretches the replaced content to fill the element's content box in both dimensions.

Example

.image {
  width: 300px;
  height: 200px;
  object-fit: fill;
}

The image content fills the entire 300-by-200-pixel box. If its original aspect ratio differs, the content appears stretched or compressed.

When to Use fill

This value may be appropriate for graphics where distortion is acceptable or where the source already matches the target dimensions.

Important Consideration

Avoid stretching photographs, logos, or illustrations when preserving their original proportions is important.

contain Value

The contain value scales the content uniformly until the entire object fits inside the element's content box.

Example

.product-image {
  width: 300px;
  height: 200px;
  object-fit: contain;
}

The entire image remains visible without distortion. Unused space may appear along the sides or above and below the image.

Add a Background Color

.product-image {
  width: 300px;
  height: 200px;
  object-fit: contain;
  background-color: #f1f5f9;
}

The background color fills the element's box, including any space not occupied by the image content.

When to Use contain

This value is useful for product images, logos, diagrams, and other media where the entire object must remain visible.

Important Consideration

contain does not guarantee that the image fills both dimensions. Preserving the entire image can leave unused space inside the box.

cover Value

The cover value scales the content uniformly until it completely covers the element's content box.

Example

.card-image {
  width: 300px;
  height: 200px;
  object-fit: cover;
}

The image fills the entire box without distortion. Portions extending beyond the box are clipped.

Control the Visible Area

.card-image {
  width: 300px;
  height: 200px;
  object-fit: cover;
  object-position: center top;
}

The image is positioned toward the top of the box, changing which portion remains visible when cropping occurs.

When to Use cover

This value is useful for image galleries, card thumbnails, profile pictures, and other layouts that require consistent image dimensions.

Important Consideration

Important details near the edges of an image may be clipped. Use object-position to adjust the visible area when necessary.

none Value

The none value displays the replaced content at its intrinsic dimensions instead of scaling it to fit the element's content box.

Example

.image {
  width: 300px;
  height: 200px;
  object-fit: none;
}

If the original image is larger than the box, only part of it may be visible. If it is smaller, unused space may remain.

Position the Unscaled Image

.image {
  width: 300px;
  height: 200px;
  object-fit: none;
  object-position: left top;
}

The unscaled image is aligned with the upper-left corner of its content box.

When to Use none

This value is useful when media should retain its natural dimensions, such as pixel-based artwork or a deliberately cropped image detail.

Important Consideration

none does not mean the image element has no dimensions. It controls the sizing of the content inside the element.

scale-down Value

The scale-down value chooses whichever produces the smaller concrete object size: none or contain.

Example

.logo {
  width: 300px;
  height: 200px;
  object-fit: scale-down;
}

If the original logo fits inside the box, it retains its natural size. If it is too large, it scales down proportionally to fit.

Prevent Unnecessary Enlargement

.thumbnail {
  width: 250px;
  height: 180px;
  object-fit: scale-down;
  background-color: #f8fafc;
}

Small images remain at their intrinsic size, while larger images shrink to fit inside the available space.

When to Use scale-down

This value is useful for logos, icons, and mixed-size image collections where small images should not be enlarged unnecessarily.

Important Consideration

scale-down never enlarges content beyond the size it would have with object-fit: none.

object-fit vs. background-size

The object-fit and background-size properties can produce similar visual results, but they apply to different types of content.

object-fit

<img class="photo" src="photo.jpg" alt="Peacock">
.photo {
  width: 300px;
  height: 200px;
  object-fit: cover;
}

The property controls the image content inside the <img> element.

background-size

<div class="photo-background"></div>
.photo-background {
  width: 300px;
  height: 200px;
  background-image: url("photo.jpg");
  background-size: cover;
  background-position: center;
}

The property controls how a CSS background image is sized inside the element's background positioning area.

Comparison

Feature object-fit background-size
Applies to Replaced content CSS background images
Common elements <img>, <video> Elements with background images
Preserves aspect ratio With contain, cover, and scale-down With contain and cover
Positioning property object-position background-position
Semantic image content Yes, when used with an appropriate HTML image element No, CSS backgrounds are generally decorative

Use an HTML image with object-fit when the image is meaningful content. Use CSS backgrounds for decorative imagery that does not need alternative text.

object-position

The object-position property controls the alignment of replaced content within its content box.

Center Position

.photo {
  width: 300px;
  height: 200px;
  object-fit: cover;
  object-position: center;
}

The image is centered within its box. This is the default positioning behavior.

Top Position

.photo {
  object-fit: cover;
  object-position: center top;
}

The image is aligned toward the top of its box, which is useful when important content appears near the upper edge.

Percentage Position

.photo {
  object-fit: cover;
  object-position: 70% 30%;
}

The percentages adjust the image's alignment within its box, changing the visible area when cropping occurs.

Important Consideration

object-fit determines how the content is sized. object-position determines where the sized content is placed.

Responsive Images

The object-fit property is useful for responsive layouts that require consistent media dimensions while accommodating images with different aspect ratios.

Responsive Image

.responsive-image {
  display: block;
  width: 100%;
  height: 250px;
  object-fit: cover;
}

The image fills the available width while maintaining a consistent height. Its content is cropped when necessary.

Aspect Ratio

.responsive-image {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

The element maintains a 16:9 aspect ratio, and the image content covers the available area.

Responsive Card Image

.card-image {
  display: block;
  width: 100%;
  height: 220px;
  object-fit: cover;
}
@media (max-width: 767px) {
  .card-image {
    height: 180px;
  }
}

The image height changes at smaller viewport widths while maintaining the same fitting behavior.

Natural Image Proportions

.natural-image {
  display: block;
  max-width: 100%;
  height: auto;
}

When the goal is to preserve the entire image at its natural aspect ratio, responsive width with automatic height may be sufficient without object-fit.

Important Consideration

The property does not select different image files for different screens. Use HTML responsive image features such as srcset, sizes, or <picture> when different image resources are required.

Practical Uses

The object-fit property is commonly used to standardize media presentation across different interface components.

Profile Picture

.avatar {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  object-fit: cover;
}

The image fills a circular frame without distortion. Parts of the original image may be cropped.

Product Image

.product-image {
  width: 100%;
  height: 240px;
  object-fit: contain;
  background-color: #f8fafc;
}

The entire product image remains visible inside a consistent display area.

Image Gallery

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}
.gallery img {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

Gallery images maintain consistent proportions even when their source dimensions differ.

Video Thumbnail

.video-preview {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

The video content fills a widescreen box. Some of the video may be cropped if its aspect ratio differs.

Logo Display

.brand-logo {
  width: 180px;
  height: 100px;
  object-fit: scale-down;
}

The logo retains its natural size when it fits, or scales down proportionally when necessary.

object-fit Example

This example compares all five object-fit values using the same image. Each image element has identical dimensions, making the different fitting behaviors easy to compare.

HTML Example

<div class="object-fit-demo">
  <div class="fit-item">
    <h3>fill</h3>
    <img class="fit-fill" src="img/peacock.jpg" alt="Peacock">
  </div>
  <div class="fit-item">
    <h3>contain</h3>
    <img class="fit-contain" src="img/peacock.jpg" alt="Peacock">
  </div>
  <div class="fit-item">
    <h3>cover</h3>
    <img class="fit-cover" src="img/peacock.jpg" alt="Peacock">
  </div>
  <div class="fit-item">
    <h3>none</h3>
    <img class="fit-none" src="img/peacock.jpg" alt="Peacock">
  </div>
  <div class="fit-item">
    <h3>scale-down</h3>
    <img class="fit-scale-down" src="img/peacock.jpg" alt="Peacock">
  </div>
</div>

CSS Example

.object-fit-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 20px;
}
.fit-item h3 {
  margin: 0 0 10px;
  font-size: 1rem;
}
.fit-item img {
  display: block;
  width: 100%;
  height: 140px;
  background-color: #e2e8f0;
  border: 2px solid #64748b;
  box-sizing: border-box;
}
.fit-fill {
  object-fit: fill;
}
.fit-contain {
  object-fit: contain;
}
.fit-cover {
  object-fit: cover;
}
.fit-none {
  object-fit: none;
}
.fit-scale-down {
  object-fit: scale-down;
}
Browser Result

fill

Peacock

contain

Peacock

cover

Peacock

none

Peacock

scale-down

Peacock

The fill value stretches the image to match the box. contain preserves the entire image, while cover fills the box and crops excess content. none retains the image's intrinsic size, and scale-down reduces the image only when necessary.

Object Position Example

<img class="positioned-image" src="peacock.jpg" alt="Peacock">
.positioned-image {
  width: 300px;
  height: 180px;
  object-fit: cover;
  object-position: center top;
}

The image fills the box while aligning its content toward the top, changing which portion remains visible.

Common Mistakes

Expecting object-fit to Set Element Dimensions

img {
  object-fit: cover;
}

The property controls how content fits inside the element. It does not establish a particular width or height. Define the element's dimensions or aspect ratio when a specific display box is required.

Using object-fit on Ordinary Containers

.container {
  object-fit: cover;
}

The property does not resize ordinary HTML container content. It applies to replaced elements such as images and videos.

Confusing contain with cover

contain keeps the entire object visible but may leave unused space. cover fills the box but may crop the object.

Using fill When Proportions Matter

The fill value can distort images when the source and display box have different aspect ratios. Use contain or cover to preserve proportions.

Ignoring Cropped Content

The cover value may clip important details near the edges of an image. Adjust object-position when necessary.

Expecting scale-down to Enlarge Small Images

The scale-down value does not enlarge content beyond its intrinsic size when that size is available.

Confusing object-fit with background-size

object-fit controls replaced content. background-size controls CSS background images.

Expecting Smooth Transitions Between Values

img {
  object-fit: contain;
  transition: object-fit 0.3s;
}
img:hover {
  object-fit: cover;
}

The property uses discrete animation. The fitting mode changes rather than smoothly interpolating between the two values.

Important Considerations

Keep the following points in mind when using object-fit:

  • The property controls how replaced content fits inside its content box.
  • Its initial value is fill.
  • The property is not inherited.
  • It applies to replaced elements, including images and videos.
  • It does not directly set the dimensions of the element itself.
  • fill stretches content and may distort its proportions.
  • contain preserves the entire object and may leave unused space.
  • cover fills the box and may crop content.
  • none does not resize content to fit the box.
  • scale-down selects the smaller result of none and contain.
  • contain, cover, and scale-down preserve intrinsic aspect ratios when available.
  • object-position controls alignment of the content inside its box.
  • background-size applies to CSS background images rather than replaced content.
  • The property is useful for thumbnails, galleries, cards, avatars, and video layouts.
  • Consistent image dimensions can improve the visual alignment of repeated components.
  • Use aspect-ratio when a consistent box proportion is required.
  • Use responsive image attributes when different image resources are needed.
  • The property does not permanently modify the original image or video.
  • It uses discrete animation rather than continuous interpolation between fitting modes.
  • Test cropping and positioning with different source image dimensions.

Browser Support

The CSS object-fit property is Baseline - Widely available. It is supported in current versions of major browsers, including Chrome, Edge, Firefox, and Safari.

The standard values fill, contain, cover, none, and scale-down have broad support in modern browsers.

For current compatibility information, visit Can I Use? and search for object-fit.

When supporting older browsers, verify the behavior of replaced elements and provide appropriate fallbacks when consistent image cropping is essential.

Specifications

The object-fit property is defined in CSS Images Module Level 3.

CSS Images Module Level 3: The object-fit Property

The specification defines how replaced content is sized within an element's content box and how the available fitting values affect the result.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: object-fit