Skip to main contentSkip to docs navigation

Object fit

Responsive object-fit utilities for controlling how replaced element content fills its container.

@layer utilities

Overview

object-fit utilities set the CSS object-fit property on replaced elements such as <img> and <video>, controlling how the content is scaled to fit the container. Available values:

  • contain
  • cover
  • fill
  • scale (for scale-down)
  • none

Examples

The object-fit-{value} class is applied directly to the replaced element:

Placeholder: Object fit contain Placeholder: Object fit cover Placeholder: Object fit fill Placeholder: Object fit scale down Placeholder: Object fit none
HTML
<img src="..." class="object-fit-contain border rounded" alt="...">
<img src="..." class="object-fit-cover border rounded" alt="...">
<img src="..." class="object-fit-fill border rounded" alt="...">
<img src="..." class="object-fit-scale border rounded" alt="...">
<img src="..." class="object-fit-none border rounded" alt="...">

Responsive

Responsive variants follow the format .{breakpoint}:object-fit-{value} and activate at the specified breakpoint and above:

Placeholder: Contain on small Placeholder: Contain on medium Placeholder: Contain on large Placeholder: Contain on xlarge Placeholder: Contain on 2xlarge
HTML
<img src="..." class="small:object-fit-contain border rounded" alt="...">
<img src="..." class="medium:object-fit-contain border rounded" alt="...">
<img src="..." class="large:object-fit-contain border rounded" alt="...">
<img src="..." class="xlarge:object-fit-contain border rounded" alt="...">
<img src="..." class="2xlarge:object-fit-contain border rounded" alt="...">

Video

The same utilities apply to <video> elements:

HTML
<video src="..." class="object-fit-contain" autoplay></video>
<video src="..." class="object-fit-cover" autoplay></video>
<video src="..." class="object-fit-fill" autoplay></video>
<video src="..." class="object-fit-scale" autoplay></video>
<video src="..." class="object-fit-none" autoplay></video>

CSS

Object fit utilities are generated via the utilities API.

Reference

The table lists all object fit utilities and their CSS properties.

ClassProperties
.object-fit-contain-o-object-fit: contain;
object-fit: contain;
.object-fit-cover-o-object-fit: cover;
object-fit: cover;
.object-fit-fill-o-object-fit: fill;
object-fit: fill;
.object-fit-scale-o-object-fit: scale-down;
object-fit: scale-down;
.object-fit-none-o-object-fit: none;
object-fit: none;

Utilities API

Object fit utilities are declared in scss/utilities/_index.scss using the utilities API.

"object-fit": (
  responsive: true,
  property: object-fit,
  values: (
    contain: contain,
    cover: cover,
    fill: fill,
    scale: scale-down,
    none: none,
  )
),