Loading...

CSS Object Fit

Float wrapped text beside media. object-fit answers a different media question: when an img or video sits inside a fixed-size frame, how should the content fill that frame? Master cover and contain before stretching faces with fill.

An object-fit toolkit

📊 Object-fit at a glance

Value Everyday job
fill Stretch to the box — may distort aspect ratio
contain Fit entire media inside; may leave empty bands
cover Fill the box; may crop edges
none Keep intrinsic size; may overflow or leave gaps
scale-down Like none or contain — whichever is smaller
object-position Aims which part stays visible when cropping/fitting

object-fit only shines when the replaced element has a definite width and height (or both axes constrained). A free-flowing image with auto height has nothing to “fit into.”

Cover — fill the frame, allow crop

Cards and hero thumbs often want every pixel of the box filled. cover scales up and crops overflow. Pair a square box with cover for tidy grids.

Cover card image

css
.card__media {
width: 100%;
height: 180px;
object-fit: cover;
display: block;
}

Contain — show the whole picture

Product shots and diagrams often must stay fully visible. contain letterboxes if aspect ratios differ — better a band than a chopped logo.

Contain product shot

css
.product__img {
width: 240px;
height: 240px;
object-fit: contain;
background: #f7f7f7;
}

Fill stretches — use with care

fill forces the media to the box size even if proportions warp. Fine for abstract textures; risky for faces, logos, and charts.

Fill stretch warning

css
.texture {
width: 320px;
height: 120px;
object-fit: fill;
}

object-position aims the crop

When cover crops, the default center may hide a face or logo. Nudge with object-position — percentages or keywords like top and left.

Position the important part

css
.avatar {
width: 96px;
height: 96px;
border-radius: 50%;
object-fit: cover;
object-position: 50% 20%;
}

A calm object-fit habit

Use real <img> / <video> with object-fit for meaningful media. Use background-size when the image is decorative chrome. Size the box first, pick cover or contain, then fine-tune position.

Avatar habit

css
.avatar {
width: 64px;
height: 64px;
object-fit: cover;
object-position: center top;
border-radius: 50%;
}

Try it yourself

  1. Give an image fixed width and height, then try cover vs contain.
  2. Build a circular avatar with cover and shift object-position.
  3. Apply fill once and notice distortion — then switch back.
  4. Compare the same crop as a CSS background-size: cover decoration.
  5. Write one sentence: when is contain safer than cover?

Common mistakes

  • Expecting object-fit to work without a sized box
  • Using fill on portraits and logos
  • Confusing this lesson with float wrap or position offsets
  • Putting meaningful photos only in background-image (harder for alt text)
  • Forgetting object-position when cover hides the subject

Summary

  • object-fit fits replaced media into a sized frame
  • cover fills and may crop; contain shows all and may letterbox
  • fill can distort — use sparingly
  • object-position aims the visible region
  • Prefer object-fit for content media; background-size for decoration

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
8
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top