CSS Object Fit for Media

Control how images and video fill containers using CSS object-fit and object-position for responsive media display.

CssData Fetching

Code

.cover {
  width: 100%;
  height: 300px;
  object-fit: cover;
  object-position: center top;
}

.contain {
  object-fit: contain;
  max-width: 100%;
}

CSS Object Fit for Media

Control how images and video fill containers using CSS object-fit and object-position for responsive media display.

Code

.cover {
  width: 100%;
  height: 300px;
  object-fit: cover;
  object-position: center top;
}

.contain {
  object-fit: contain;
  max-width: 100%;
}

Explanation

object-fit controls how an image or video fills its box (cover crops, contain letterboxes) and object-position shifts the focal point. Expected behavior: the element’s allocated box stays fixed while the media is scaled inside it. Limitation: object-fit does nothing unless the element has an explicit width and height, so intrinsic-only sizing will not show the effect.

Interactive Tools for This Topic