Tutorials CSS object-fit & object-position
Ch 19 / 60
Chapter 19 Beginner 6 min read CSS

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.

What you'll learn
What object-fit is and why it matters
All 5 values: cover, contain, fill, none, scale-down
cover vs contain — when to use each
object-position — keywords, %, and pixels
Keeping faces and focal subjects in frame
Real-world patterns: cards, avatars, heroes, logos

What 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.

The problem it solves
You have a grid of cards. Every card thumbnail must be the same height. Images come from a CMS at all different aspect ratios — some landscape, some portrait, some square. Without object-fit: cover, they all distort. With it, they all crop cleanly to the same box.
Only works with explicit dimensions
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.
Works for video too
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:

Mountain landscape — fill
fill
Stretches to fill box. Distorts the image. This is the default — avoid it.
Mountain landscape — cover
cover ✓
Fills box completely. May crop edges. Preserves ratio. Most used value.
Mountain landscape — contain
contain
Whole image visible. May show empty space. Good for logos and diagrams.
Mountain landscape — none
none
Natural pixel size, no scaling. May overflow the container.
Mountain landscape — scale-down
scale-down
Smaller of none or contain. Never upscales a small image.
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:

object-fit: cover
Portrait woman — cover
Fills the box completely. The portrait is cropped at the sides — no empty space. The image always fills edge-to-edge. Use when filling the space matters more than showing the whole image.
object-fit: contain
Portrait woman — contain
Shows the whole image. Empty black space appears on the sides (the background-color of the container). The full portrait is visible with no cropping. Use for logos, product photos, diagrams.
CSS — object-fit
/* 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:

portrait
top left
portrait
top
portrait
top right
portrait
left
portrait
center ✓
portrait
right
portrait
bottom left
portrait
bottom
portrait
bottom right

The center cell is highlighted — it's the default. Notice how top keeps the face visible while bottom shows only the torso.

Portrait photo tip
For headshots and team photos, always use 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:

Mountain — top left
top left
Shows sky + left peak
Mountain — center
center (default)
Balanced crop
Mountain — bottom center
bottom center
Shows valley/foreground
Mountain — 50% 30%
50% 30%
Percentage fine-tune
CSS — object-position
.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:

Code editor on screen
CSS · Tutorial
Mastering Flexbox Layout
8 min read · Intermediate
Web design workspace
Design · UI
CSS Grid for Real Projects
10 min read · Advanced
Responsive design on devices
CSS · Responsive
Mobile-First Design Patterns
6 min read · Beginner
CSS animations code
CSS · Animation
CSS Transitions & Keyframes
7 min read · Intermediate
CSS — Card grid pattern
.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:

Avatar large
72px
Avatar medium
52px
Avatar medium 2
52px
Avatar small
40px
Avatar small 2
40px
s1 s2 s3
+9
stacked group
av ring
with ring + status
CSS — Avatar patterns
/* 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:

Team working on laptops — hero image
Learn CSS with Real Projects
object-fit: cover + object-position: center top
CSS — Hero image pattern
.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;
}
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering object-fit, object-position, filter and all CSS3 image properties.
Download PDF

Try It Yourself

Change the object-fit and object-position values in the editor and watch the real Unsplash photos update in the preview:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 19

Questions & Answers
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.
Both values preserve the image's aspect ratio, but they make opposite trade-offs:

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).
When 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.
Four properties are needed for a perfect avatar that works with any source image shape:

width: 48px; — explicit width
aspect-ratio: 1; — forces equal height (keeps it square)
border-radius: 50%; — clips to a circle
object-fit: cover; — fills the circle without distorting

Without 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 cropped

This 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.
Multiple Choice Questions
Question 1 of 5
Which object-fit value fills the container and may crop the image?
Question 2 of 5
What is the default value of object-fit?
Question 3 of 5
What does object-position: top do on a portrait photo with object-fit: cover?
Question 4 of 5
Which object-fit value is best for logos where the full image must always be visible?
Question 5 of 5
Which CSS property creates a perfect circle crop on an avatar image?
Chapter Summary
  • object-fit controls how image content fills a fixed-dimension container. Requires explicit width + height (or aspect-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-position controls the focal point of a cover-cropped image. Default = center. Use top for 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:block on the img. Wrap in a container with overflow:hidden for hover-zoom.
  • Hero formula: width:100% + height:100svh + object-fit:cover + object-position:center top.
~6 min read
Beginner level
Chapter 19 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel