CSS object-fit generator — crop or letterbox images in a fixed box

Set a box size, pick how the image should fit and which part stays in view, and compare all five object-fit values side by side before copying the CSS.

Live demo · real result from this tool
Sample image

Press Generate to create a result.

Processed on your device — nothing is uploaded.

How to use the CSS object fit generator

  1. Choose an object-fit value: cover, contain, fill, none or scale-down.
  2. Pick an object-position, such as center, top or bottom right, to anchor the image.
  3. Set the Box width and Box height (80–400 px) to match the space on your page.
  4. Switch Sample image between Landscape and Portrait to see how each shape behaves.
  5. Compare the large preview with the five small tiles, then copy the img rule.

What it does and when to use it

Real images come in every shape, but layouts often need a fixed box: square avatars, equal-height product cards, video thumbnails and hero banners. object-fit decides how an image of the wrong shape fills such a box without being squashed. cover is the usual choice for thumbnails and avatars; contain is right for logos and product shots where nothing may be cut off.

To give the box a responsive shape instead of fixed pixels, pair this with the aspect ratio generator. To crop the file itself, use the image cropper, and to round the corners of the box, the border radius generator.

How it works

The tool writes an img rule with your width and height in pixels, the object-fit value, and object-position only when it is not centre. The preview shows your box with a dashed outline, then the same sample image in all five modes at 120 × 120 px, with your chosen mode marked “(selected)”.

MDN lists object-fit and object-position as Baseline widely available since January 2020.

Worked examples

Square crop, the default. 240 × 240 px with cover:

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

A tall slot that shows the whole picture. 240 × 320 px, contain, top, landscape sample: the rule adds object-fit: contain; and object-position: top;. The full image sits against the top edge with empty space below, instead of floating in the middle.

A round avatar that keeps the face. 96 × 96 px, cover, top: width: 96px; height: 96px; object-fit: cover; plus object-position: top;. On a portrait photo, cover must trim the extra height, and top makes it trim only from the bottom, so the head stays in view. Add border-radius: 50% for a circle.

Limits and tips

  • The rule targets every img. Change the selector to a class, such as .thumb, before using it in a real stylesheet.
  • Cropping hides content, so never use cover on images that contain text people must read, such as charts or posters.
  • Give the img real dimensions. object-fit only matters when the box shape differs from the image shape.

Frequently asked questions

What is the difference between cover and contain?
cover fills the whole box and crops whatever does not fit. contain shows the whole image and leaves empty bands, top and bottom or left and right, where the shapes differ.
When would I use fill, none or scale-down?
fill stretches the image to the box and distorts it if the shapes differ, so it is rarely wanted for photos. none keeps the image at its natural size and crops it. scale-down behaves like none or contain, whichever gives the smaller image, so it never enlarges a small picture.
What does object-position change?
It decides which part of the image stays visible when it is cropped, or where it sits when there are empty bands. top keeps the top of a portrait photo, which is often where faces are.
Does object-fit work on background images or divs?
No. It applies to replaced elements such as img and video. For CSS backgrounds, use background-size cover or contain and background-position instead.
Can I preview my own photo?
Not in this tool. It uses a built-in sample image in landscape or portrait shape, and says so under the preview. The CSS works the same for any image.

Sources

  1. object-fit - CSS | MDN — MDN Web Docs, accessed 2026-10-08
  2. object-position - CSS | MDN — MDN Web Docs, accessed 2026-10-08

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close