Firefox Tomorrow

css property

`object-fit` CSS property

View on MDN ↗

The object-fit CSS property sets how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container.

[!NOTE] The object-fit property has no effect on <iframe>, <embed>, and <fencedframe> elements.

You can alter the alignment of the replaced element’s content object within the element’s box using the object-position property.

Interactive exampleOpen the canonical MDN source to run this embedded demo.
object-fit: fill;
object-fit: contain;
object-fit: cover;
object-fit: none;
object-fit: scale-down;
<section id="default-example">
  <img
    class="transition-all"
    id="example-element"
    src="/shared-assets/images/examples/plumeria-146x200.jpg" />
</section>
#example-element {
  height: 100%;
  width: 100%;
  border: 2px dotted #888888;
}

Syntax

object-fit: contain;
object-fit: cover;
object-fit: fill;
object-fit: none;
object-fit: scale-down;

/* Global values */
object-fit: inherit;
object-fit: initial;
object-fit: revert;
object-fit: revert-layer;
object-fit: unset;

Values

This property is specified as one of the following keyword values:

  • contain
    • : The replaced content is scaled to maintain its aspect ratio while fitting within the element’s content box. The entire object is made to fill the box, while preserving its aspect ratio, so the object will be “letterboxed” or “pillarboxed” if its aspect ratio does not match the aspect ratio of the box.
  • cover
    • : The replaced content is sized to maintain its aspect ratio while filling the element’s entire content box. If the object’s aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.
  • fill
    • : The replaced content is sized to fill the element’s content box. This is the initial value. The entire object will completely fill the box. If the object’s aspect ratio does not match the aspect ratio of its box, then the object will be stretched to fit.
  • none
    • : The replaced content is not resized.
  • scale-down
    • : The content is sized as if none or contain were specified, whichever would result in a smaller concrete object size.

Formal definition

Formal syntax

Examples

Setting object-fit for an image

HTML

<section>
  <h2>object-fit: fill</h2>
  <img class="fill" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <img class="fill narrow" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <h2>object-fit: contain</h2>
  <img class="contain" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <img class="contain narrow" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <h2>object-fit: cover</h2>
  <img class="cover" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <img class="cover narrow" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <h2>object-fit: none</h2>
  <img class="none" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <img class="none narrow" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <h2>object-fit: scale-down</h2>
  <img class="scale-down" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />

  <img class="scale-down narrow" src="/firefox/mdn-assets/css/reference/properties/object-fit/mdn_logo_only_color.png" alt="MDN Logo" />
</section>

CSS

h2 {
  font-family: "Courier New", monospace;
  font-size: 1em;
  margin: 1em 0 0.3em;
}

img {
  width: 150px;
  height: 100px;
  border: 1px solid black;
  margin: 10px 0;
}

.narrow {
  width: 100px;
  height: 150px;
}

.fill {
  object-fit: fill;
}

.contain {
  object-fit: contain;
}

.cover {
  object-fit: cover;
}

.none {
  object-fit: none;
}

.scale-down {
  object-fit: scale-down;
}

Result

Interactive exampleOpen the canonical MDN source to run this embedded demo.

Specifications

SpecificationsStandards references are available on the canonical MDN page.

Browser compatibility

Browser compatibilityCompatibility data is available on the canonical MDN page.

See also