The right crop control for images and video inside fixed containers
object-fit decides whether an image gets stretched, cropped, or shown unchanged inside its container, while object-position sets the focal point of that crop. Together they replace server-side image cropping for most layout cases, and unlike background-size, they still work with real img and video elements, including alt text and loading optimizations.
Table of Contents
- 1. The problem: fitting images into fixed containers without distortion
- 2. The five object-fit values in detail: cover, contain, fill, none, scale-down
- 3. object-position: controlling the focal point of the crop
- 4. object-fit versus background-size: which tool for which job
- 5. object-fit on video elements: poster images and autoplay backgrounds
- 6. Recipe: responsive galleries with a consistent aspect ratio
- 7. Art direction: combining different crops with picture
- 8. Performance considerations: object-fit versus server-side cropping
- 9. Accessibility: setting alt text correctly for cropped images
- 10. Summary
- 11. FAQ
1. The problem: fitting images into fixed containers without distortion
As soon as an image gets forced into a container with a fixed width and height, say a square product tile or a card preview, it either distorts the aspect ratio or overflows the container, unless something else steps in. In the past, this was almost always solved server-side, by cropping images to the exact target size and storing them repeatedly in multiple formats, which costs both processing time and storage space.
object-fit solves the same problem entirely in the browser, without altering the image itself. The original image stays at full resolution in the DOM, the CSS property only controls how it renders within the box the container defines. That makes responsive layouts far more flexible, because a single image can be reused across arbitrarily many different container shapes, without generating a separate file for every shape.
2. The five object-fit values in detail: cover, contain, fill, none, scale-down
cover scales the image so it fills the entire container, even if that crops parts of the image outside the visible area. The aspect ratio is always preserved. contain does the opposite: the whole image stays visible, with visible letterboxing inside the container if the aspect ratios do not exactly match.
fill is the only value that ignores the aspect ratio and stretches or squeezes the image to the exact container size, which almost always produces visible distortion and is therefore rarely the right choice. none shows the image at its original size with no scaling at all, regardless of the container size. scale-down behaves like none or contain, whichever produces the smaller image, which makes it useful for thumbnails that should never be enlarged past their original size.
.product-image-cover { width: 300px; height: 300px; object-fit: cover; }
.product-image-contain { width: 300px; height: 300px; object-fit: contain; }
.product-image-fill { width: 300px; height: 300px; object-fit: fill; } /* often distorts */
.thumbnail { width: 120px; height: 120px; object-fit: scale-down; }
3. object-position: controlling the focal point of the crop
With object-fit: cover, the browser crops the image centered by default, which for a portrait photo can mean cutting off exactly the head while leaving plenty of free background visible. object-position fixes this by defining which area of the image stays preferentially visible when cropping, analogous to background-position for background images.
The values work the same way as background-position: keywords like top, center, or bottom, or percentage values for finer control. For portrait shots in square product tiles, object-position: top or a percentage value like 50% 20% is often the better choice over default centering, since faces usually sit in the upper third of the image.
.team-photo {
width: 240px;
height: 240px;
object-fit: cover;
object-position: 50% 20%; /* keeps the head visible, not the center */
}
4. object-fit versus background-size: which tool for which job
background-size: cover solves a visually similar problem, but only for CSS background images, which do not exist as a standalone element in the DOM. That means no alt text, no native loading optimization via loading="lazy", and no direct access for screen readers or search engine crawlers that want to evaluate image content.
object-fit, in contrast, works with real img or video elements and thus keeps all the semantic and functional advantages: alt text for accessibility, native lazy loading, srcset for responsive image sources, and correct search engine indexing. For content-meaningful images, such as product photos, object-fit is therefore almost always the right choice, while background-size fits purely decorative backgrounds better.
5. object-fit on video elements: poster images and autoplay backgrounds
object-fit works identically on <video> elements as it does on images, which is especially useful for background hero videos that need to fill a container at full width and height, regardless of the source file's aspect ratio. A 16:9 video inside a square or very wide container gets automatically cropped rather than distorted with cover.
What matters here is that both the video's poster attribute and the video itself need the same object-fit value, otherwise the visible crop visibly jumps during the transition from the still frame to the playing video. This consistency between poster and video is frequently overlooked in practice and causes a short but clearly noticeable visual glitch.
<video
class="hero-video"
poster="/media/hero-poster.jpg"
autoplay muted loop playsinline>
<source src="/media/hero.mp4" type="video/mp4">
</video>
<style>
.hero-video { width: 100%; height: 480px; object-fit: cover; object-position: center; }
</style>
6. Recipe: responsive galleries with a consistent aspect ratio
For galleries with images from different sources and different original aspect ratios, combining aspect-ratio on the container with object-fit: cover on the image produces a visually consistent grid without manually cropping every image beforehand. aspect-ratio defines the target shape, object-fit fills it cleanly regardless of the original format.
This combination is significantly easier to maintain than server-side cropping, because new images can be added without an extra processing step and still immediately fit the existing grid layout. If the gallery's desired aspect ratio changes later, a single CSS change to aspect-ratio is enough, instead of regenerating every image file.
.gallery-image {
aspect-ratio: 4 / 3;
width: 100%;
object-fit: cover;
border-radius: 0.75rem;
}
7. Art direction: combining different crops with picture
object-fit solves cropping within a fixed container shape, but it does not change the image source itself. When an image should show a different crop on small screens than on large ones, say a close-up on mobile and a wide shot on desktop, the <picture> element with multiple <source> elements is the right tool, combined with object-fit on the contained img.
This combination cleanly separates two independent concerns: <picture> decides which image file gets loaded at all, while object-fit decides how that loaded file renders within its container. Whoever mixes both concepts, for instance by server-side cropping for every screen size, loses the flexibility to adjust the layout later without generating new image files.
8. Performance considerations: object-fit versus server-side cropping
object-fit saves server processing time because no crop needs to be pre-generated per target size, but it has a downside: the browser still loads the full original image, even if only a crop of it ends up visible. For very large source images inside small crop containers, that can transfer more data than actually necessary.
The best compromise combines both approaches: srcset already delivers multiple appropriately scaled image sizes, and object-fit handles the fine crop control per container within those sizes. That keeps the transferred data volume small while the layout still stays flexible, without having to pre-render every conceivable container shape individually.
9. Accessibility: setting alt text correctly for cropped images
Because object-fit only changes the visual rendering, the image's alt text stays unaffected by it and should still describe the full image content, not just the visibly cropped portion. A screen reader reads out the alt text regardless of how heavily the image is visually cropped, so the text must not go stale just because the visible crop changed.
For purely decorative images that carry no content value, such as background textures inside a carousel card, an empty alt="" is still correct regardless of the chosen object-fit value. The decision for or against descriptive alt text depends on the semantic weight of the image, not on its CSS rendering.
| Value | Aspect ratio | Visible content | Typical use |
|---|---|---|---|
cover |
preserved | may be cropped | Product tiles, hero images, team photos |
contain |
preserved | fully visible | Logos, icons, images with important edges |
fill |
ignored | fully visible, but distorted | Rarely useful, usually avoided |
none |
preserved | original size, may overflow | Fixed image size without scaling |
scale-down |
preserved | whichever is smaller, none or contain | Thumbnails without enlargement |
Mironsoft
Modern CSS, layout architecture and rendering performance
CSS that stays maintainable instead of breaking with every change?
We review existing stylesheets for specificity chaos and layout thrashing, then build a CSS architecture with cascade layers, custom properties and modern layout primitives that still makes sense after the tenth feature.
CSS Audit
Systematically uncovering specificity issues, cascade conflicts and unused selectors.
Architecture Refactoring
Introducing cascade layers, custom properties and design tokens cleanly.
Performance Tuning
Fixing layout thrashing, expensive selectors and rendering bottlenecks.
10. Summary
object-fit and object-position: The Essentials at a Glance
Core idea
object-fit controls how an image renders inside its container without altering the image file itself.
cover vs. contain
cover fills the container completely with possible cropping, contain shows the whole image with possible letterboxing.
object-position
Controls the focal point of the crop, important for portraits so heads do not get cut off.
Compared to background-size
object-fit preserves alt text, srcset and lazy loading, background-size only works for pure CSS backgrounds.