{"id":194850,"date":"2015-02-02T04:01:46","date_gmt":"2015-02-02T11:01:46","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=194850"},"modified":"2020-07-01T12:54:02","modified_gmt":"2020-07-01T19:54:02","slug":"object-fit","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/o\/object-fit\/","title":{"rendered":"object-fit"},"content":{"rendered":"
The This is how we might set that property:<\/p>\n Because the second image has an aspect ratio that is different than the original image on the left it will stretch outside the realm of its content box, cropping the top and bottom parts of the image.<\/p>\n It\u2019s worth noting that by default the image is centered within its content box but this can be altered with the The demo below shows five examples detailing how we might want an image to squish into a content box which is sometimes smaller or larger than its original width (resize the browser for a better idea of how this might work):<\/p>\n See the Pen object-fit<\/a> by Robin Rendle (@robinrendle<\/a>) on CodePen<\/a>.<\/p>\n If the content of the image does not fill the content box for whatever reason then the unfilled space will show the element’s background, in this instance a light grey background.<\/p>\nobject-fit<\/code> property defines how an element responds to the height and width of its content box. It’s intended for images, videos and other embeddable media formats in conjunction with the
object-position<\/a><\/code> property. Used by itself,
object-fit<\/code> lets us crop an inline image by giving us fine-grained control over how it squishes and stretches inside its box.<\/p>\n
object-fit<\/code> can be set with one of these five values:<\/p>\n
\n
fill<\/code>: this is the default value which stretches the image to fit the content box, regardless of its aspect-ratio.<\/li>\n
contain<\/code>: increases or decreases the size of the image to fill the box whilst preserving its aspect-ratio.<\/li>\n
cover<\/code>: the image will fill the height and width of its box, once again maintaining its aspect ratio but often cropping the image in the process.<\/li>\n
none<\/code>: image will ignore the height and width of the parent and retain its original size.<\/li>\n
scale-down<\/code>: the image will compare the difference between
none<\/code> and
contain<\/code> in order to find the smallest concrete object size.<\/li>\n<\/ul>\n
img {\n height: 120px;\n}\n\n.cover {\n width: 260px;\n object-fit: cover;\n}<\/code><\/pre>\n
object-position<\/a><\/code> property.<\/p>\n
Demo<\/h3>\n
Related properties<\/h3>\n