CSS object-fit Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Images & Media

What You’ll Learn

The object-fit property controls how images, videos, and other replaced content fit inside a container. It is one of the most useful tools for responsive layouts because it prevents awkward stretching while keeping media looking polished.

01

Replaced Content

Works on images and video.

02

Syntax

One keyword controls fit.

03

cover

Fill and crop cleanly.

04

contain

Show the whole image.

05

fill Default

Stretch to the box.

06

Responsive UI

Cards, avatars, heroes.

Introduction

The object-fit property in CSS is used to control how the content of a replaced element, such as an <img> or <video>, is resized to fit its container.

This property is particularly useful for maintaining aspect ratios and preventing distortion when images or videos are scaled to fit a specific size.

Definition and Usage

The object-fit CSS property tells the browser how replaced content should be sized relative to its box. You usually set explicit width and height on the image or its container, then choose a fit mode such as cover or contain.

This makes it easy to build card thumbnails, hero banners, profile photos, and video embeds that stay visually consistent across screen sizes.

💡
Beginner Tip

For most layouts, start with object-fit: cover when cropping is acceptable, and object-fit: contain when the entire image must remain visible.

📝 Syntax

The syntax for the object-fit property is straightforward. It can be applied to any replaced element, such as an image, video, or iframe:

syntax.css
element {
  object-fit: value;
}

Here, value determines how the content is resized to fit the container.

Basic Example

object-fit.css
.container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Syntax Rules

  • Set a defined width and height on the media element or its container.
  • Choose one keyword: fill, contain, cover, none, or scale-down.
  • Pair with object-position when you need to control cropping focus.
  • Use overflow: hidden on the container when cropped edges should not spill out.

🎯 Default Value

The default value of the object-fit property is fill. This means the content will stretch to fill the entire container, which can sometimes result in distortion if the aspect ratio of the content does not match the container.

That is why many responsive layouts explicitly set object-fit: cover or object-fit: contain instead of relying on the default.

⚡ Quick Reference

QuestionAnswer
Initial valuefill
Applies toReplaced elements such as img, video, and iframe
InheritedNo
AnimatableNo
Common useResponsive thumbnails, hero images, avatars, and video frames

💎 Property Values

The object-fit property accepts five keyword values. Each one changes how content is scaled inside its box.

ValueExampleDescription
fillobject-fit: fill;Stretches content to fill the container. Aspect ratio may change.
containobject-fit: contain;Scales content to fit inside the box while keeping the full image visible.
coverobject-fit: cover;Scales content to fill the box. Extra edges may be cropped.
noneobject-fit: none;Keeps the original size. Content is not resized.
scale-downobject-fit: scale-down;Uses whichever is smaller: none or contain.
cover — crop to fill contain — show all fill — stretch none — original size scale-down — shrink only

Supported Elements

object-fit is designed for replaced elements — content whose size comes from an external resource rather than CSS text flow:

  • <img> — the most common use case for cards, galleries, and avatars.
  • <video> — useful for background videos and fixed-ratio players.
  • <iframe> — can help embedded content fit responsive frames.

For best results, give the element a defined box using width, height, or a parent container with a fixed aspect ratio.

👀 Live Preview

The same landscape image is placed in identical frames. Compare how each object-fit value behaves:

fill
contain
cover
none
scale-down

Examples Gallery

Use object-fit to control how images and videos behave inside fixed-size containers.

🖼️ Responsive Images

These patterns appear constantly in card grids, profile photos, and hero sections.

Example 1 — Card Thumbnail with cover

In this example, an image fills a fixed-size container while keeping its aspect ratio. Extra edges are cropped instead of stretching.

cover-thumbnail.html
<style>
  .container {
    width: 300px;
    height: 200px;
    border: 1px solid #ddd;
    overflow: hidden;
  }

  .container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
</style>

<div class="container">
  <img src="/images/bg-image.webp" alt="Example image">
</div>
Try It Yourself

How It Works

The image fills the container completely. Because cover preserves aspect ratio, the browser crops overflow instead of stretching the photo.

Example 2 — Logo Box with contain

Use contain when the entire image must stay visible, such as logos or product photos inside a fixed frame.

contain-logo.css
.logo-box {
  width: 160px;
  height: 100px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
}

.logo-box img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
Try It Yourself

How It Works

The image scales down to fit inside the box. Empty space may appear on the sides or top and bottom, but nothing is cropped.

📐 Fit Modes

Compare stretching, original sizing, and shrink-only behavior when a container is smaller than the media.

Example 3 — Default fill Behavior

This shows the default fill behavior. The image stretches to match the container even when that distorts the aspect ratio.

fill-stretch.css
.banner img {
  width: 100%;
  height: 180px;
  object-fit: fill;
}
Try It Yourself

How It Works

Both width and height are forced to match the container. This is rarely ideal for photos, which is why cover and contain are more common.

Example 4 — Responsive Video Frame

Combine a sized container with object-fit: cover so videos behave like polished media cards.

video-frame.css
.video-frame {
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border-radius: 0.75rem;
}

.video-frame video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
Try It Yourself

How It Works

The frame keeps a 16 / 9 ratio, and the video fills it without letterboxing when cover is used.

Pair with object-position

When you use object-fit: cover, part of the image may be cropped. The related object-position property lets you choose which area stays visible, such as object-position: top center for portrait photos or product shots.

cover-position.css
.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top center;
}

♿ Accessibility

  • Always provide alt text — Cropping with cover can hide important visual details, so describe the image meaningfully.
  • Do not hide critical content — If text or key subject matter gets cropped, adjust the frame ratio or use object-position.
  • Avoid distortion — Stretched images from fill can look unclear and reduce readability of embedded text or logos.
  • Test responsive layouts — A crop that looks fine on desktop may remove important content on mobile.

🧠 How object-fit Works

1

You define a media box

Set width and height on the image, video, or container so the browser knows the target area.

Layout
2

You choose a fit mode

Keywords like cover, contain, and fill tell the browser how to scale the media.

object-fit
3

The browser scales the content

The media is resized, cropped, or letterboxed inside the box while preserving aspect ratio when required.

Rendering
=

Consistent responsive media

Images and videos look intentional across cards, grids, and hero sections.

Browser Compatibility

The object-fit property is supported in all modern browsers, including current versions of Chrome, Firefox, Safari, Edge, and Opera. Always test across the browsers your audience uses.

Baseline · Modern browsers

Reliable support for responsive media

Chrome, Edge, Firefox, Safari, and Opera all support object-fit in current versions.

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

Fallback behavior

When unsupported, images fall back to default stretching behavior similar to fill.

💻
Internet Explorer No support · Use a sized container and background-image fallback if needed
None
object-fit property 97% supported

Bottom line: Use object-fit confidently in modern responsive layouts. Pair with aspect-ratio for predictable media frames.

Conclusion

The object-fit property provides a simple yet powerful way to control how content like images and videos are displayed within their containers.

By understanding and utilizing the various values of this property, you can ensure that your media elements look great across different screen sizes and layouts, enhancing the visual appeal and user experience of your website.

💡 Best Practices

✅ Do

  • Use object-fit: cover for card thumbnails and hero images
  • Use object-fit: contain for logos and product photos
  • Pair with aspect-ratio for consistent media frames
  • Add overflow: hidden when cropped edges should not show
  • Use object-position to control cropping focus

❌ Don’t

  • Stretch photos with the default fill value unless distortion is intentional
  • Forget width and height — object-fit needs a defined box
  • Crop out important subject matter without adjusting position
  • Rely on object-fit alone for layout — size the container first
  • Skip alt text because part of the image is cropped away

Key Takeaways

Knowledge Unlocked

Five things to remember about object-fit

Use these points when building responsive media layouts.

5
Core concepts
📐 02

cover vs contain

Crop or show all.

Values
📐 03

fill Default

Stretches to the box.

Default
📦 04

Defined Box

Needs width and height.

Layout
🎯 05

object-position

Controls crop focus.

Companion

❓ Frequently Asked Questions

object-fit controls how replaced content such as an image or video is resized to fit its container while preserving or adjusting aspect ratio.
The default value is fill, which stretches the content to fill the container even if that distorts the aspect ratio.
cover fills the container and may crop edges. contain keeps the entire image visible inside the container and may leave empty space.
It works on replaced elements such as img, video, and iframe when they have explicit width and height or fill a sized container.
Yes. object-fit decides how content fits, and object-position decides which part of the image stays visible when cropping happens.

Practice in the Live Editor

Open the HTML editor, apply object-fit, and preview responsive images instantly.

HTML Editor →

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.

5 people found this page helpful