CSS object-fit Property

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The object-fit property sets how the content of a replaced element — usually an <img> or <video> — is resized to fit the element’s own box.

Give the media a width and height first. Then choose a keyword: stretch with fill, crop to fill with cover, or show the whole image with contain. Crop focus is controlled by object-position. It has no effect on <iframe>, <embed>, or <fencedframe>.

Baseline · Widely available.

Not deprecated and not experimental. Safe in current browsers since Safari 10. Internet Explorer has no support.

CSS Syntax

syntax.css
object-fit: fill | contain | cover | none | scale-down | initial | inherit;

Property Values

ValueDescriptionTry it
fillDefault. Stretch to fill the box. Aspect ratio may changeTry it
containScale to fit inside the box. Keeps ratio. May leave empty spaceTry it
coverScale to fill the box. Keeps ratio. May crop edgesTry it
noneDo not resize. Use the media’s intrinsic sizeTry it
scale-downUse none or contain, whichever makes the content smallerTry it
initialSets this property to its default value, fillTry it
inheritInherits this property from the parent elementTry it

How to Use object-fit

Size the media box, then pick how the content scales inside it.

  1. Use a replaced element such as <img> or <video>.
  2. Give it a defined box with width and height (or fill a sized parent).
  3. Set object-fit on that element. It is not inherited.
  4. If cropping hides the subject, add object-position.
PieceRequired?Role
Replaced elementYesimg, video, and similar — not iframe
Defined width and heightYesWithout a sized box, every keyword looks the same
KeywordYescover, contain, fill, none, or scale-down
object-positionNoChooses which part stays visible when cover crops

Live Preview

The same image sits in a fixed frame. Change object-fit and watch how it scales.

Gray is empty space inside the frame. Cropped edges mean the image is larger than the box.

Frame
Demo photo inside a fixed frame

object-fit: cover

CSS Examples

Each snippet adds only what object-fit needs: a sized box and one keyword. Use Try It Yourself for a live editor.

Example 1 — Cover the Box

Fill a fixed card frame. Extra edges are cropped so the photo never stretches.

CSS
<style>
  img {
    width: 240px;
    height: 140px;
    object-fit: cover;
    border: 1px solid #ccc;
  }
</style>

<img src="/images/bg-image.webp" alt="Landscape photo">
Try It Yourself

How It Works

  1. Define the box. width and height give the image a fixed frame that may not match the photo’s ratio.
  2. Keep the aspect ratio. cover scales the photo until both sides of the box are filled.
  3. Crop the overflow. Parts that stick out of the box are clipped. Nothing stretches.
  4. Steer the crop later. Add object-position if faces or logos sit too close to an edge.

Example 2 — Show the Whole Image

Fit the entire image inside the box. Empty bands appear when the ratios differ.

CSS
<style>
  img {
    width: 240px;
    height: 140px;
    object-fit: contain;
    border: 1px solid #ccc;
    background: #f1f5f9;
  }
</style>

<img src="/images/bg-image.webp" alt="Full photo inside a frame">
Try It Yourself

How It Works

  1. Same sized box. Only the keyword changes from Example 1.
  2. Fit without cropping. contain scales until one side hits the box edge. The other side may leave a gap.
  3. Gaps show the background. The light gray fill makes those empty bands easy to see.
  4. Use for logos. Prefer contain when every pixel of the image must stay visible.

Example 3 — Stretch with fill

Force the image to match both sides of the box. The aspect ratio is not protected.

CSS
<style>
  img {
    width: 240px;
    height: 140px;
    object-fit: fill;
    border: 1px solid #ccc;
  }
</style>

<img src="/images/bg-image.webp" alt="Stretched photo">
Try It Yourself

How It Works

  1. fill is the default. Leaving object-fit unset behaves the same way.
  2. Both axes stretch. Width and height are forced to the box, even when that warps the photo.
  3. No crop and no letterbox. The frame is always completely covered.
  4. Prefer cover or contain for photos. Use fill only when distortion is acceptable.

Edge Cases & Pitfalls

If the image looks unchanged or crops the wrong part, one of these is usually why.

size

No sized box, no visible change

If width and height match the image’s natural size, every keyword looks the same. Set both dimensions, or fill a parent that already has a size.

iframe

Does not apply to iframe

object-fit has no effect on <iframe>, <embed>, or <fencedframe>. Use it on replaced media such as img and video.

crop

cover can hide the subject

Cropping may cut off faces or text. Pair with object-position, or switch to contain when the whole image matters.

bg

Not the same as background-size

object-fit styles the media element itself. background-size scales a background-image. Same keywords, different targets.

alt

Cropping does not replace alt text

A cropped photo can hide details. Keep meaningful alt text so assistive tech still describes the image.

Browser Support

object-fit is a browser CSS property for replaced elements. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available since 2020

object-fit

Chrome 31+, Firefox 36+, Safari 10+, Edge 16+, Opera 19+. Internet Explorer has no native support.

97% Modern browser support
Google Chrome 31+ · Desktop & Mobile
Full support
Mozilla Firefox 36+ · Desktop & Mobile
Full support
Apple Safari 10+ · macOS & iOS
Full support
Microsoft Edge 16+ · Chromium & Legacy
Full support
Opera 19+ · Modern versions
Full support
Samsung Internet 3+ · Android
Full support
Internet Explorer No support
None
object-fit Widely supported

Bottom line: Safe for modern responsive layouts. For Internet Explorer, size the box and accept stretch, or use a background-image fallback. Pair with aspect-ratio for predictable media frames.

Key Takeaways

  • Role: scale replaced content inside its own width and height box.
  • Everyday picks: cover for cards and heroes; contain for logos and product shots.
  • Default: fill stretches. Prefer cover or contain for photos.
  • Companion: object-position steers the crop. Not inherited.

One line: size the media box, then let object-fit decide stretch, crop, or letterbox.

Frequently Asked Questions

It controls how replaced content such as an image or video is resized inside its own box. You pick fill, contain, cover, none, or scale-down.
fill. The content stretches to match both width and height, which can distort the aspect ratio.
cover fills the box and may crop edges. contain keeps the whole image visible and may leave empty space. Both keep the aspect ratio.
The element needs a defined width and height that differ from the media's natural size. Without a sized box, there is nothing to fit into.
No. Per the CSS Images specification and MDN, object-fit has no effect on iframe, embed, or fencedframe. Use it on img, video, and similar replaced elements.
It is Baseline and widely available: Chrome 31+, Firefox 36+, Safari 10+, Edge 16+, and Opera 19+. Internet Explorer has no support. It is a browser CSS property, not a Node.js feature.

Next: object-position

Learn how to steer which part of a cropped image stays in view.

object-position tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful