Object fit

Source: https://getbootstrap.com/docs/6.0/utilities/object-fit

Use the object fit utilities to modify how the content of a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Replaced_element" target="_blank" rel="noopener noreferrer">replaced element</a>, such as an <img> or <video>, should be resized to fit its container.

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:

<Placeholder width="140" height="120" class="object-fit-none border rounded" text="Object fit none" markup="img" />`} />

Responsive

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

<Placeholder width="140" height="80" class="2xl:object-fit-contain border rounded" text="Contain on 2xl" markup="img" />`} />

Video

The .object-fit-{value} and responsive .{breakpoint}:object-fit-{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>

CSS

Sass utilities API

Object fit utilities are declared in our utilities API in scss/_utilities.scss. Learn how to use the utilities API.