Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to choose: <img> with object-fit:cover vs background-size: cover? [closed]

Tags:

html

css

image

I’m facing a need to have a “cover” background on one of the elements.

What are pros and cons of using CSS background-size: cover vs actual img with object-fit: cover?

like image 211
Runnick Avatar asked Sep 25 '18 10:09

Runnick


People also ask

What is better IMG or background image?

Img Tag. It is widely known that using the image tag is better SEO practice than using background images because of the screen reader implications.

What is the best size for a background image?

Best background image size For background images, the best size is 1920 pixels wide x 1080 pixels high. This ideal ratio of 16:9 will fill the surface of the webpage without compromising the quality of the image. In terms of pixels per inch (ppi), the image should be at least 72.

What is difference between cover and contain in background-size?

cover tells the browser to make sure the image always covers the entire container, even if it has to stretch the image or cut a little bit off one of the edges. contain , on the other hand, says to always show the whole image, even if that leaves a little space to the sides or bottom.

Does object-fit work with background image?

Adding a Background to an Image With object-fit: contain # We would benefit from that when also using object-fit: contain . In the example below, we have a grid of images. When the aspect ratios of the image and the container are different, the background color will appear.


1 Answers

In addition to what others have said about compatibility (background-size: cover is supported on all modern browsers, where object-fit: cover is not), an unwanted side-effect of using a div element with background-size: cover is that if the user tries to print your page, a lot of times the image won't be printed as it will be deemed a background by the browser and ignored.

That might not matter to you at all, but it is something to be aware of.

There are some ways to get around this, but I don't know of a single, cross-browser silver bullet method.

like image 140
Michael Avatar answered Nov 15 '22 11:11

Michael