Codectionary / Developer documentation / CSS

object-fit and object-position

object-fit controls how a replaced element's content (like an <img> or <video>) is resized to fit its box, similar to background-size but for actual media elements. cover crops to fill the box, contain scales to fit entirely within it, and object-position controls which part of the content stays visible when cropping occurs.

Syntax

object-fit: cover | contain | fill;
object-position: value;

Examples

object-fit: cover for Consistent Thumbnails

Filling a fixed-size box without distorting the image's proportions.

.thumbnail {
  width: 300px;
  height: 200px;
  object-fit: cover; /* fills the box completely, cropping as needed */
}

object-fit: contain and object-position

Showing an entire image without cropping, and controlling crop focus.

.logo {
  width: 200px;
  height: 100px;
  object-fit: contain; /* shows the whole image, letterboxing if needed */
}

.portrait-crop {
  object-fit: cover;
  object-position: top; /* keeps the top of the image visible when cropping */
}

Parameters and values

  • cover (keyword): Fills the box completely, cropping the content as needed
  • contain (keyword): Scales to fit entirely within the box, no cropping
  • fill (keyword): Stretches to fill the box exactly, ignoring original proportions (default)
  • object-position (position): Controls which part of the content remains visible when cropped

Best practices

  • Use object-fit: cover for consistent thumbnail grids where images come in varying original proportions
  • Use object-fit: contain for logos or images that must never be cropped, even if it means empty space around them
  • Combine object-position with cover to control exactly which part of an image stays visible when cropping occurs, like keeping faces in frame
  • Remember object-fit only applies to replaced elements like <img>, <video>, and <canvas> - it has no effect on regular elements like <div>

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation