Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between background-size: cover; and background-size: 100%;?

When i set the background-size property from an image of a div to background-size: cover; or background-size: 100%;, the both look the same.

What is the difference? When should i use cover and when 100%?

like image 418
Andreas Furster Avatar asked Jul 09 '14 13:07

Andreas Furster


People also ask

Is background-size cover the same as 100%?

Pretty sure background-size: cover; means the image will fill the element while maintaining its aspect ratio, while background-size: 100%; will just make the image fill 100% width of the element.

What does background-size cover mean?

background-size:cover; means the background image will always fit the whole div , you won't be left with any empty spots in your div. background-size:100% 100%; won't leave any empty space too, but of course this will destroy the original image aspect ratio.

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.

Can I use background-size cover?

If the background-size is contain or cover : While preserving its intrinsic proportions, the image is rendered at the largest size contained within, or covering, the background positioning area. If the image has no intrinsic proportions, then it's rendered at the size of the background positioning area.


2 Answers

here's a fiddle: http://jsfiddle.net/RS5kX/19/

background-size:100%; = background-size:100% auto; = the width is set to be 100% large and the height of the background image follows respecting the image aspect ratio.

background-size:cover; means the background image will always fit the whole div , you won't be left with any empty spots in your div

background-size:100% 100%

won't leave any empty space too, but of course this will detroy the original image aspect ratio

like image 32
valerio0999 Avatar answered Oct 11 '22 14:10

valerio0999


cover = Scale the background image to be as large as possible so that the background area is completely covered by the background image. Some parts of the background image may not be in view within the background positioning area

Basically it zooms in until the inner most edges are touching the side, which means that some of the image may be cut off unlike 100% where all of the image will be visible.

If it did not do the zoom in, you would end up with two sides that reach the edge but on the other axis you would have blank horizontal (or vertical) looking 'bars' on either side of the image in one of those directions.

Your Question: Why would they looks the same ?
Answer: If the image / container are square

See http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-size&preval=cover for example

like image 59
Michael Durrant Avatar answered Oct 11 '22 14:10

Michael Durrant