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

不想你离开。 提交于 2019-11-27 16:14:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!