CSS object-fit & object-position
When you set a fixed size on an image element, how should the browser fill that
space? Stretch it and distort it? Crop it cleanly? Show the whole thing? object-fit answers this question — and object-position gives you precise control over the cropping focal point.
object-fit is and why it
matterscover, contain, fill, none, scale-downcover vs contain — when to use eachobject-position — keywords, %,
and pixelsWhat is object-fit?
object-fit is a property for replaced
elements — mainly <img> and <video>. When you give an image both a fixed width and height, the browser must
decide how to fill that box with the image content. Without object-fit,
the default fill value stretches the image — often destroying its aspect
ratio.
object-fit: cover, they all distort. With it, they all crop
cleanly to the same box.object-fit only applies when the element
has a fixed width and height (or
aspect-ratio). Without dimensions, the image sizes to its content
naturally.
object-fit and object-position work identically on <video> elements — useful for full-screen video backgrounds.All 5 object-fit Values
The same wide landscape photo placed in a tall narrow box. Watch how each value handles the size mismatch differently:
| Value | Preserves ratio? | Fills box? | Shows full image? | Best for |
|---|---|---|---|---|
| fill | No — distorts | Yes | Yes | Avoid — default only |
| cover | Yes | Yes | No — may crop | Cards, heroes, avatars |
| contain | Yes | No — may gap | Yes | Logos, diagrams, product images |
| none | Yes | Maybe | Maybe | Rare — natural size display |
| scale-down | Yes | No | Yes | Small images that shouldn't be upscaled |
cover vs contain — Deep Dive
These two values are the ones you will use 95% of the time. Both preserve aspect ratio — but they make opposite trade-offs. The same portrait photo in a 16:9 wide container:
/* Card thumbnail — fill the space, crop is fine */
.card-img {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
display: block;
}
/* Logo — show the full logo, gaps are fine */
.logo {
width: 140px;
height: 60px;
object-fit: contain;
display: block;
}
/* Avatar circle */
.avatar {
width: 48px;
aspect-ratio: 1;
border-radius: 50%;
object-fit: cover;
display: block;
}
/* Hero / banner */
.hero-img {
width: 100%;
height: 100svh;
object-fit: cover;
display: block;
}
object-position — Control the Focal Point
When object-fit: cover crops an image, object-position decides which part of the image stays visible.
Without it the default is center. The same portrait photo cropped nine
ways — notice how the visible area shifts:
The center cell is highlighted — it's the default. Notice
how top keeps the face visible while bottom shows only the torso.
object-position: top — it
keeps faces in frame even when the container crop the bottom.
Landscape photo — different positions
Mountain sunrise photo in a short wide crop. Notice how each position anchors a different part of the scene:
.img {
width: 100%;
height: 300px;
object-fit: cover;
object-position: center; /* default */
}
/* ── Keyword values ── */
.portrait { object-position: top; } /* keep face */
.landscape { object-position: center bottom; } /* keep horizon */
.left-subj { object-position: left center; } /* keep left subject */
.bottom-right{ object-position: bottom right; }
/* ── Percentage — x% y% from top-left ── */
.fine { object-position: 30% 20%; }
/* ── Pixel offset ── */
.offset { object-position: 50px 80px; }
/* ── Edge-relative (CSS spec) ── */
.edge { object-position: right 20% top 10%; }
Real-World Pattern — Card Grid
The most common use of object-fit: cover — a grid
of cards where every thumbnail is the same size regardless of the original image dimensions. Hover to see
the zoom-in effect:
.card-grid {
display: grid;
grid-template-columns:repeat(auto-fill, minmax(240px, 1fr));
gap: 20px;
}
.card { overflow: hidden; border-radius: 12px; }
.card-thumbnail {
width: 100%;
aspect-ratio: 16 / 9; /* all same height */
object-fit: cover; /* fill, clip to box */
display: block;
transition: transform .4s ease;
}
.card:hover .card-thumbnail {
transform: scale(1.06); /* zoom on hover */
}
Avatar Patterns
object-fit: cover + border-radius: 50% + aspect-ratio: 1 is
the complete avatar formula. Works on any source image regardless of its original proportions:
/* Basic avatar */
.avatar {
width: 48px;
aspect-ratio: 1; /* keeps it square */
border-radius: 50%; /* makes it a circle */
object-fit: cover; /* fills circle, no distortion */
display: block;
}
/* With ring */
.avatar-ring {
width: 48px;
aspect-ratio: 1;
border-radius: 50%;
object-fit: cover;
border: 3px solid #264de4;
}
/* Stacked overlapping group */
.avatar-group img {
width: 36px;
aspect-ratio: 1;
border-radius: 50%;
object-fit: cover;
border: 3px solid white;
margin-left: -10px; /* overlap */
}
.avatar-group img:first-child { margin-left: 0; }
Hero Image Pattern
A live hero section built with object-fit: cover +
object-position: center top + a gradient overlay. The image always fills
the full container regardless of viewport size:
.hero {
position: relative;
width: 100%;
height: 100svh; /* full screen */
overflow: hidden;
}
.hero-img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center top; /* keep faces in frame */
display: block;
}
/* Gradient overlay for text legibility */
.hero::after {
content: '';
position: absolute;
inset: 0;
background: linear-gradient(to bottom, transparent 30%, rgba(0,0,0,.65));
}
.hero-content {
position: absolute;
bottom: clamp(24px, 5vw, 60px);
left: clamp(16px, 5vw, 80px);
z-index: 1;
color: #fff;
}
Try It Yourself
Change the object-fit and object-position values in the editor and watch the real Unsplash photos
update in the preview:
Quick Check — Chapter 19
object-fit solves image distortion in fixed-dimension
containers.When you set both
width and height on an image, the default fill
behaviour stretches the image to fill the box — squashing or stretching the aspect ratio. This produces
distorted, unprofessional-looking images, especially in card grids where photos come from various
sources at varying proportions.object-fit is needed any time you:— Apply a fixed height to a full-width image
— Display a grid of cards with uniform thumbnail heights
— Crop an image to a circle (avatar)
— Make a background-like image that fills a container
It only applies to replaced elements (
<img>, <video>) and requires explicit dimensions on the element.
cover — scales the image until it completely
fills the container. If the image and container have different aspect ratios, the image is
cropped on the sides or top/bottom. There is no empty space — the container is fully covered. Use for:
card thumbnails, hero images, avatars, full-bleed sections.contain — scales the image until the entire
image is visible inside the container. If aspect ratios differ, empty space appears around the
image (showing the container's background-color). There is no cropping. Use for: logos, product images,
diagrams, technical illustrations where every pixel matters.Memory rule: cover = fill the box (may crop). contain = show all (may gap).
object-fit: cover crops an image, object-position controls which part of the image stays visible in the
container. It works exactly like background-position.Default:
center — the centre of the image is
preserved. Both cropped edges are equal.Keyword values:
top, bottom, left, right, center, combinations like
top left.Percentage values:
30% 20% — x% from left, y% from
top. Fine-grained control for a specific subject position.Practical example: A headshot used as a card thumbnail. Without
object-position: top, the default center may show only the shoulders in a short container. With object-position: top, the face stays in frame.
width: 48px; — explicit widthaspect-ratio: 1; — forces equal height (keeps it square)border-radius: 50%; — clips to a circleobject-fit: cover; — fills the circle without distortingWithout
object-fit: cover, a portrait photo in a square container
would be stretched horizontally. Without aspect-ratio: 1, setting
only width would let height default to the image's natural height — no longer a circle. All four
properties work together.
scale-down behaves as whichever is smaller — none or contain:— If the image is smaller than the container: it renders at its natural size (same as
none), not upscaled— If the image is larger than the container: it scales down to fit (same as
contain), not croppedThis is useful for small icons or thumbnails that should appear at their natural pixel size but must never overflow their container. It prevents both upscaling (which would pixelate) and overflow (which would break layout).
It is less commonly used than
cover or contain, but useful for icon libraries where image sizes vary and you
want consistent, crisp rendering at natural size.
object-fit value fills the container and
may crop the image?object-fit?object-position: top do on a
portrait photo with object-fit: cover?object-fit value is best for logos where
the full image must always be visible?object-fitcontrols how image content fills a fixed-dimension container. Requires explicitwidth+height(oraspect-ratio) on the element.- 5 values:
fill(default, distorts),cover(fills box, may crop),contain(full image, may gap),none(natural size),scale-down(smaller of none/contain). - cover for card thumbnails, hero images, avatars. contain for logos, product shots, diagrams. Both preserve aspect ratio.
object-positioncontrols the focal point of a cover-cropped image. Default =center. Usetopfor portrait photos to keep faces in frame.- Accepts keywords, percentages or pixels — same syntax as
background-position. - Avatar formula:
width + aspect-ratio:1 + border-radius:50% + object-fit:cover. - Card grid formula:
aspect-ratio:16/9 + object-fit:cover + display:blockon the img. Wrap in a container withoverflow:hiddenfor hover-zoom. - Hero formula:
width:100% + height:100svh + object-fit:cover + object-position:center top.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.