DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

CSS object-fit: How to Fit Images in Their Containers

Use CSS object-fit to choose between filling an image container with a crop and showing the whole image without distortion.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To fit an image in a fixed-size container without stretching it, set the image’s dimensions and choose object-fit: cover to fill the box with a crop, or object-fit: contain to show the whole image with possible empty space. Use object-position to adjust which part remains visible when cropping.

Choose the result you need

object-fit controls how media content is rendered inside a replaced element’s content box. For an <img>, the choice is mainly between covering the box and preserving the entire image. The CSS Images Module Level 3 defines five values for the property: fill, contain, cover, none, and scale-down.

As an Amazon Associate I earn from qualifying purchases.

Value What happens Use it when
cover The image keeps its aspect ratio and fills the box. If the image and box have different aspect ratios, part of the image is clipped. A card, thumbnail, or frame must be completely filled and cropping is acceptable.
contain The whole image fits inside the box without changing its aspect ratio. Unfilled space may remain. The complete asset must be visible, such as a logo, diagram, or product image.
fill The image fills the box and may be stretched if its natural aspect ratio differs. This is the initial value. Distortion is acceptable, or the source already has the box’s aspect ratio.
none The image is not resized to fit the box. You want the object’s original size, even if it does not fill the box.
scale-down The browser uses whichever is smaller: the result of none or contain. The image should not be enlarged beyond the smaller of those two outcomes.

In practice, ask three questions: must the entire image remain visible, must the box be fully covered, and is cropping acceptable? Choose contain for the whole image, cover for a fully filled frame, and fill only if stretching is acceptable.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Give the image a box to fit

object-fit changes how the image is drawn inside the image element; it does not set the element’s layout dimensions. Give the element a useful width and height through CSS or layout, then set the fit behavior:

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
.card-image {
  display: block;
  width: 100%;
  height: 12rem;
  object-fit: cover;
  object-position: center;
}

This example makes the image fill a 12-rem-high card slot. The default centered position crops evenly around the center when the source and box ratios differ. Change cover to contain if the whole image must show, accepting that the box may have unfilled space.

When only one dimension is specified, the browser normally calculates the other while preserving the media’s intrinsic aspect ratio. Setting both dimensions can create a box with a different ratio from the source. With the default fill, that can make the image look stretched; cover and contain preserve the source ratio and differ in whether they crop or leave space.

Adjust the visible crop with object-position

object-position aligns the image content inside its element’s box. Its initial position is centered, equivalent to 50% 50%. If centered cropping cuts off a face or product, shift the visible area toward the subject:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.hero-image {
  width: 100%;
  height: 24rem;
  object-fit: cover;
  object-position: 60% 30%;
}

Positions can also use keywords such as top or right, or length and percentage values. Choose a position by checking the actual source image against the target frame; a crop that works for one image may fail for another. With contain, any uncovered area shows the element’s background.

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Use object-fit on the right elements

The property applies to replaced elements, including typical <img> and <video> elements. It does not affect every element that can display content: MDN notes that it has no effect on <iframe>, <embed>, or <fencedframe>. object-fit is not inherited, and its initial value is fill. See the CSS Images Module Level 3 and MDN’s object-fit reference.

MDN marks object-fit “Baseline Widely available.” Its reference also notes that the formal grammar reflects a newer standard and not every browser may implement every part. If you rely on newer syntax rather than the established values shown here, check compatibility for your target browsers in MDN’s compatibility information.

Troubleshoot a fit that looks wrong

  • The image looks stretched: Check whether both its rendered width and height are constrained while object-fit is still fill. Use cover or contain according to whether filling the box or showing all content matters more.
  • The image does not fill the slot: Confirm that the image element itself has the intended dimensions. object-fit controls the content inside its box, not the box’s size. If full coverage is required, use cover.
  • An important subject is cropped: Keep cover if the frame must stay filled, then adjust object-position to place the subject in the visible area.
  • Empty bands appear around the image: This is expected with contain when the ratios differ. Use the element’s background for the remaining space, or choose cover if cropping is acceptable.

Or skip the browser setup

If you need a screenshot of a page to inspect the rendered result, ScreenshotNeo provides a one-request API. For example, this cURL command captures a page as WebP:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before the shot; bot checks, blank pages, and failed loads are not billed. Its MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Learn about ScreenshotNeo, or sign up for the free plan.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does object-fit set an image’s width and height?

No. It controls how the image content fits inside the element’s content box; set the element’s dimensions separately.

Which value prevents cropping?

Use contain to keep the whole image visible. The box may have unfilled space.

Can I use object-fit on an iframe?

No. MDN states that object-fit has no effect on <iframe>.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.