Utilities
Object fit
Use the object fit utilities to modify how the content of a replaced element, 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:
containcoverfillscale(for scale-down)none
The utility only sets object-fit. The element still needs a width and height to fit into — here
the sizing and ratio helpers do the job.
Examples
Add the object-fit-{value} class to the replaced element. The placeholders are inline SVGs with
a fixed viewBox, which behave exactly like a 320×240 photo would.
HTML
<svg class="object-fit-contain border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-contain"><rect width="320" height="240" fill="var(--bs-navy-500)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-500)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="20">contain</text></svg>
<svg class="object-fit-cover border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-cover"><rect width="320" height="240" fill="var(--bs-navy-500)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-500)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="20">cover</text></svg>
<svg class="object-fit-fill border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-fill"><rect width="320" height="240" fill="var(--bs-navy-500)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-500)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="20">fill</text></svg>
<svg class="object-fit-scale border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-scale"><rect width="320" height="240" fill="var(--bs-navy-500)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-500)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="20">scale</text></svg>
<svg class="object-fit-none border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-none"><rect width="320" height="240" fill="var(--bs-navy-500)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-500)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="20">none</text></svg><svg> with a viewBox is a replaced element for the purpose of
object-fit only when its preserveAspectRatio is left at the default. With
fill and none the browser lets the SVG scale through its own
viewBox rules, so those two may look identical to contain. On an
<img> or <video> every value behaves as described.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.
HTML
<svg class="object-fit-sm-contain border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-sm-contain"><rect width="320" height="240" fill="var(--bs-navy-700)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-300)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="18">sm-contain</text></svg>
<svg class="object-fit-md-contain border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-md-contain"><rect width="320" height="240" fill="var(--bs-navy-700)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-300)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="18">md-contain</text></svg>
<svg class="object-fit-lg-contain border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-lg-contain"><rect width="320" height="240" fill="var(--bs-navy-700)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-300)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="18">lg-contain</text></svg>
<svg class="object-fit-xl-contain border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-xl-contain"><rect width="320" height="240" fill="var(--bs-navy-700)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-300)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="18">xl-contain</text></svg>
<svg class="object-fit-xxl-contain border rounded" width="200" height="120" viewBox="0 0 320 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: object-fit-xxl-contain"><rect width="320" height="240" fill="var(--bs-navy-700)"/><circle cx="160" cy="120" r="80" fill="var(--bs-gold-300)"/><text x="160" y="126" fill="var(--bs-navy-900)" text-anchor="middle" font-size="18">xxl-contain</text></svg>All the classes:
.object-fit-contain,.object-fit-cover,.object-fit-fill,.object-fit-scale,.object-fit-none.object-fit-sm-contain,-cover,-fill,-scale,-none.object-fit-md-contain,-cover,-fill,-scale,-none.object-fit-lg-contain,-cover,-fill,-scale,-none.object-fit-xl-contain,-cover,-fill,-scale,-none.object-fit-xxl-contain,-cover,-fill,-scale,-none
Video
The .object-fit-{value} and responsive .object-fit-{breakpoint}-{value} utilities
also work on <video> elements. A full-bleed arena-cam loop that must never letterbox is
the usual case:
<video class="object-fit-cover w-100" style="height: 240px" autoplay muted loop playsinline>
<source src="arena-cam.mp4" type="video/mp4">
</video>
Pair it with .ratio when the box should keep a fixed aspect instead of a fixed height.
CSS
"object-fit": (
responsive: true,
property: object-fit,
values: (
contain: contain,
cover: cover,
fill: fill,
scale: scale-down,
none: none,
)
),