object-fit
Quick Summary for object-fit
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.
Code Usage for object-fit
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: unset; 
More Details for object-fit

object-fit

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.

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

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: unset; 

The object-fit property is specified as a single keyword chosen from the list of values below.

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

Initial valuefill
Applies toreplaced elements
Inheritedno
Computed valueas specified
Animation typediscrete

Formal syntax

fill | contain | cover | none | scale-down

Examples

Setting object-fit for an image

HTML
<section>   <h2>object-fit: fill</h2>   <img class="fill" src="mdn_logo_only_color.png" alt="MDN Logo">    <img class="fill narrow" src="mdn_logo_only_color.png" alt="MDN Logo">    <h2>object-fit: contain</h2>   <img class="contain" src="mdn_logo_only_color.png" alt="MDN Logo">    <img class="contain narrow" src="mdn_logo_only_color.png" alt="MDN Logo">    <h2>object-fit: cover</h2>   <img class="cover" src="mdn_logo_only_color.png" alt="MDN Logo">    <img class="cover narrow" src="mdn_logo_only_color.png" alt="MDN Logo">    <h2>object-fit: none</h2>   <img class="none" src="mdn_logo_only_color.png" alt="MDN Logo">    <img class="none narrow" src="mdn_logo_only_color.png" alt="MDN Logo">    <h2>object-fit: scale-down</h2>   <img class="scale-down" src="mdn_logo_only_color.png" alt="MDN Logo">    <img class="scale-down narrow" src="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 #000;   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

Specifications

Specification
CSS Images Module Level 3 # the-object-fit

See also

Other image-related CSS properties: object-position, image-orientation, image-rendering, image-resolution. background-size Select your preferred language English (US)DeutschEspañolFrançais日本語한국어Português (do Brasil)Русский中文 (简体)正體中文 (繁體) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
Treasure every moment that you have and treasure it more because you shared it with someone special, special enough to spend your time...and remember that time waits for no one.
Unknown
Random CSS Property

padding-bottom

The padding-bottom CSS property sets the height of the padding area on the bottom of an element.
padding-bottom css reference