问题
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%?
回答1:
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.
See http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-size&preval=cover for example
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/24654996/what-is-the-difference-between-background-size-cover-and-background-size-100