Skip to main content

Object fit

How it works

Change the value of the object-fit property with our responsive object-fit utility classes. This property tells the content to fill the parent container in a variety of ways, such as preserving the aspect ratio or stretching to take up as much space as possible.

Classes for the value of object-fit are named using the format .object-fit-{value}. Choose from the following values:

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

Examples

Add the object-fit-{value} class to the replaced element:

Result
Loading...
Live Editor
<div className="d-flex overflow-auto">
	<Placeholder
		width="140"
		height="120"
		className="object-fit-contain border rounded"
		text="Object fit contain"
		markup="img"
	/>
	<Placeholder
		width="140"
		height="120"
		className="object-fit-cover border rounded"
		text="Object fit cover"
		markup="img"
	/>
	<Placeholder
		width="140"
		height="120"
		className="object-fit-fill border rounded"
		text="Object fit fill"
		markup="img"
	/>
	<Placeholder
		width="140"
		height="120"
		className="object-fit-scale border rounded"
		text="Object fit scale down"
		markup="img"
	/>
	<Placeholder
		width="140"
		height="120"
		className="object-fit-none border rounded"
		text="Object fit none"
		markup="img"
	/>
</div>

Responsive

Responsive variations also exist for each object-fit value using the format .object-fit-{breakpoint}-{value}, for the following breakpoint abbreviations: sm, md, lg, xl, and xxl. Classes can be combined for various effects as you need.

Result
Loading...
Live Editor
<div className="d-flex overflow-auto">
	<Placeholder
		width="140"
		height="80"
		className="object-fit-sm-contain border rounded"
		text="Contain on sm"
		markup="img"
	/>
	<Placeholder
		width="140"
		height="80"
		className="object-fit-md-contain border rounded"
		text="Contain on md"
		markup="img"
	/>
	<Placeholder
		width="140"
		height="80"
		className="object-fit-lg-contain border rounded"
		text="Contain on lg"
		markup="img"
	/>
	<Placeholder
		width="140"
		height="80"
		className="object-fit-xl-contain border rounded"
		text="Contain on xl"
		markup="img"
	/>
	<Placeholder
		width="140"
		height="80"
		className="object-fit-xxl-contain border rounded"
		text="Contain on xxl"
		markup="img"
	/>
</div>

Video

The .object-fit-{value} and responsive .object-fit-{breakpoint}-{value} utilities also work on <video> elements.

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