Full answer
When an img or video has a fixed box, for example 100px by 100px, but the file has a different shape, the browser must decide what to do. By default it stretches the picture to fit (object-fit: fill), which distorts it.
object-fit chooses the rule.
fill: stretch to the box. This is the default and distorts the picture.contain: scale the picture to fit fully inside the box, keeping its shape. Empty bars may appear on the sides or top and bottom.cover: scale the picture to cover the whole box, keeping its shape. Part of the picture is cropped.none: do not resize. The picture keeps its natural size and is cropped.scale-down: usenoneorcontain, whichever gives the smaller result.
object-position decides which part stays visible when the picture is cropped. The default is 50% 50%, the centre. For a portrait photo in a wide box, object-position: top keeps the faces and crops the legs.
These properties apply to replaced elements, such as img, video and canvas. For a CSS background image, the matching property is background-size.
All three wide pictures sit in a 100 by 100 box. The first is squashed, the second shows the full picture with space above and below, and the third fills the box and crops the right side because of left top. The avatar is a tall photo shown as a round 64px circle, and cover keeps the proportions.
This is the standard pattern for profile pictures and card thumbnails: fix the box size, add object-fit: cover.
<style>
img { width: 100px; height: 100px; border: 1px solid #999; margin-right: 8px; }
.fill { object-fit: fill; }
.contain { object-fit: contain; }
.cover { object-fit: cover; object-position: left top; }
.avatar { width: 64px; height: 64px; border-radius: 50%; object-fit: cover; }
</style>
<img id="a" class="fill" alt="Wide banner" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='200'%3E%3Crect width='400' height='200' fill='%2399ccff'/%3E%3C/svg%3E">
<img id="b" class="contain" alt="Wide banner" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='200'%3E%3Crect width='400' height='200' fill='%2399ccff'/%3E%3C/svg%3E">
<img id="c" class="cover" alt="Wide banner" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='200'%3E%3Crect width='400' height='200' fill='%2399ccff'/%3E%3C/svg%3E">
<img id="d" class="avatar" alt="Student photo" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='500'%3E%3Crect width='300' height='500' fill='%23cc99ff'/%3E%3C/svg%3E">Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- fill is the default and distorts the picture.
- cover fills the box and crops; contain shows everything and may leave space.
- object-position picks the visible part when cropping.
- It applies to replaced elements such as img and video.
- background-size is the matching property for background images.
Common mistakes
- Setting width and height on an image and not setting object-fit, so the picture looks stretched.
- Using cover and cropping out the important part, for example a face. Adjust object-position.
- Expecting object-fit to work on a div with a background image. Use background-size there.
Follow-ups
- What is the background-size equivalent of object-fit: cover?
- How do you keep the top of a tall photo visible in a wide box?
Tips
- Explain cover and contain with one sentence each: cover fills and crops, contain fits and leaves space.
- Pair object-fit: cover with an aspect-ratio or a fixed height so card thumbnails line up.