The object-fit
CSS property specifies how a replaced element, such as an <img>
or <video>
, should be resized to fit its container.
/* Keyword values */ object-fit: fill; object-fit: contain; object-fit: cover; object-fit: none; object-fit: scale-down; /* Global values */ object-fit: inherit; object-fit: initial; object-fit: unset;
Depending on the value you specify for object-fit
, the element may be clipped, scaled, or stretched to fit its box.
<div id="container"> <img src="https://mdn.mozillademos.org/files/15191/eagle.jpg" style="object-fit:fill"/> <img src="https://mdn.mozillademos.org/files/15191/eagle.jpg" style="object-fit:cover"/> <img src="https://mdn.mozillademos.org/files/15191/eagle.jpg" style="object-fit:contain"/> <img src="https://mdn.mozillademos.org/files/15191/eagle.jpg" style="object-fit:none"/> <img src="https://mdn.mozillademos.org/files/15191/eagle.jpg" style="object-fit:scale-down"/> </div> <div id="container"> <span class="caption">fill</span> <span class="caption">cover</span> <span class="caption">contain</span> <span class="caption">none</span> <span class="caption">scale-down</span> </div>
* { box-sizing: border-box; } #container { width: 100%; display:flex; justify-content: space-between; background-color: #F4F7F8; } span { font-family: monospace; padding: 0.5em; } img { height: 300px; } img, span { margin: 5px; width: 18%; }
Initial value | fill |
---|---|
Applies to | replaced elements |
Inherited | no |
Media | visual |
Computed value | as specified |
Animation type | discrete |
Canonical order | the unique non-ambiguous order defined by the formal grammar |
Syntax
The object-fit
property is specified as a single keyword chosen from the list of values below.
Values
fill
- The replaced content is sized to fill the element’s content box. 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.
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" 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.
none
- The replaced content is not resized.
scale-down
- The content is sized as if
none
orcontain
were specified, whichever would result in a smaller concrete object size.
Formal syntax
fill | contain | cover | none | scale-down
Example
HTML Content
<div> <h2>object-fit: fill</h2> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="fill"/> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="fill narrow"/> <h2>object-fit: contain</h2> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="contain"/> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="contain narrow"/> <h2>object-fit: cover</h2> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="cover"/> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="cover narrow"/> <h2>object-fit: none</h2> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="none"/> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="none narrow"/> <h2>object-fit: scale-down</h2> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="scale-down"/> <img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="scale-down narrow"/> </div>
CSS Content
h2 { font-family: Courier New, monospace; font-size: 1em; margin: 1em 0 0.3em; } div { display: flex; flex-direction: column; flex-wrap: wrap; align-items: flex-start; height: 940px; } img { width: 150px; height: 100px; border: 1px solid #000; } .narrow { width: 100px; height: 150px; margin-top: 10px; } .fill { object-fit: fill; } .contain { object-fit: contain; } .cover { object-fit: cover; } .none { object-fit: none; } .scale-down { object-fit: scale-down; }
Output
Specifications
Specification | Status | Comment |
---|---|---|
CSS Image Values and Replaced Content Module Level 4 The definition of 'object-fit' in that specification. |
Working Draft | The from-image and flip keywords have been added. |
CSS Image Values and Replaced Content Module Level 3 The definition of 'object-fit' in that specification. |
Candidate Recommendation | Initial definition |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 31.0 | 36 (36) | No support | 11.60-o 19.0 |
(Yes) |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 4.4.4 | 36 (36) | No support | 11.5-o 24 |
(Yes) |
See also
- Other image-related CSS properties:
object-position
,image-orientation
,image-rendering
,image-resolution
.
Document Tags and Contributors
Tags:
Contributors to this page:
mfluehr,
wbamberg,
SphinxKnight,
FremyCompany,
GreenGremlin,
Sebastianz,
fscholz,
teoli,
phistuck,
stephaniehobson
Last updated by:
mfluehr,