How does the object-fit property works on images

Hello guys, can someone explain how the object-fit property works on images, more precisely, what is the difference between the values it takes like cover and contain please
36 Replies
Jochem
Jochem3w ago
cover makes it so that the image will cover every pixel of the container, cropping the sides or top/bottom if necessary. contain makes it so that the entire image is visible inside the container as big as possible without cropping, but it might leave part of the container uncovered
Chris Bolson
Chris Bolson3w ago
Just to add to Jochems answer, both “cover” and “contain” maintain the original aspect ratio of the image. That is to say, the image won’t be distorted.
Jochem
Jochem3w ago
yes, good addition, thanks!
Faker
FakerOP3w ago
is there any useful case scenario where one is better than the other? Like why make use of cover if their will be empty spaces left
Jochem
Jochem3w ago
you can use both simultaneously. If you make a thumbnail gallery with both vertical and horizontal images in it, I'd set the foreground image to contain so you can see the entire thing, then have a background with cover and a blur applied so you always have the full thing covered with relevant pixels. Generally, you'd use contain if the image is important, and cover if the image is just there to make the site look better
Faker
FakerOP3w ago
hmmm have a look at this picture for e.g, I'm trying to make an image gallery, for the image at the bottom, I use cover because when I use contain, what was happening is that the images were leaving empty pixels.... what can I add in order to use both the cover and contain property here please
No description
Jochem
Jochem3w ago
I forget the details, but you pretty much have to duplicate the image and set one to contain and one to cover
Faker
FakerOP3w ago
oh I see what you mean like in the big square, probably it will be cover right ?
Jochem
Jochem3w ago
I'd use contain because you'd want to see the entire image
ἔρως
ἔρως3w ago
you can use cover for the thumbnails also, combine it with an appropriatedly-sized image you don't want (or need) 16mpx images for thumbnails
Jochem
Jochem3w ago
to be clear: this is a choice, and yeah, a lot of people just use cover for thumbs
ἔρως
ἔρως3w ago
yup, but you can combine both: png thumbnails rendered in contain mode (using some software for it) and then show it in cover
Faker
FakerOP3w ago
when we use cover, if our image need to fill the container and it isn't big enough, the image is stretched? compare to contain which doesn't stretch it ?
Jochem
Jochem3w ago
no, it's zoomed in and cropped
ἔρως
ἔρως3w ago
"stretched" has a special meaning
Faker
FakerOP3w ago
ok ok I understand the zoom in, but for contain, what happens? it isn't zoom in ?
ἔρως
ἔρως3w ago
this is stetched
No description
Faker
FakerOP3w ago
yep, with cover, you are right, the image wasn't "distorted"
ἔρως
ἔρως3w ago
stretched the correct term is stretched
Faker
FakerOP3w ago
yeah sorry, stretched
ἔρως
ἔρως3w ago
it's important to use the right term each term means something different
Faker
FakerOP3w ago
yep got it, sorry I'm just confuse why when we use contain, we get the empty pixels while cover just covers the whole container
Jochem
Jochem3w ago
contain is zoomed out until it fits, cover is zoomed out until it covers everything
ἔρως
ἔρως3w ago
because the full image is contained inside the size available
Faker
FakerOP3w ago
ahhhh I see
ἔρως
ἔρως3w ago
and images come in all sizes and proportions
Faker
FakerOP3w ago
yeah I see I try to visualise the zoom out scenarios, I got it I think, just need to refresh..... is it correct to say the following: So cover = zoom out until image fits container, this implies part of the image might be cropped. while contain = zoom out until we fill the image without any missing part; this is why we get the empty pixels
ἔρως
ἔρως3w ago
no, cover will zoom in
Faker
FakerOP3w ago
https://codepen.io/Fakeur/pen/BaXPjoB In this codepen, the first 2 images uses cover and the third one uses contain... I don't understand how the first 2 images are different... they both use cover but are their any difference between them ? I'm not able to visualise the zoom in effect 🥲 we zoom in/out with respect to the original dimensions of the image ?
ἔρως
ἔρως3w ago
the entire left side is missing on the cover images and the right side too
Faker
FakerOP3w ago
yeah, the thing is both the first and second image are the same even though their containers have different dimensions, why is the reason for that please like I expect the second image to have more sections being cropped but this isn't the case, it has the same section as the first one
ἔρως
ἔρως3w ago
simply because the containers have different dimentions, but same proportions 200x200 and 600x600 are different, but the proportion between the width and height is still 1:1 so, you will see exactly the same zone, for the same exact image
Faker
FakerOP3w ago
ahhh I see
ἔρως
ἔρως3w ago
makes sense now?
Faker
FakerOP3w ago
this is what we mean by aspect ratio ? yeahhh makes more sense, I'm just refreshing all of what I learn just to make sure it's well ordered in my head 😂
ἔρως
ἔρως3w ago
yes, aspect ratio is the ratio between the width and height that makes sense
Want results from more Discord servers?
Add your server