Search code examples
cssbackground-size

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%?


Solution

  • 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