CSS3 background-size Property

Here is the brief explanation on how to specify the size of a background image using the CSS3 background-size property.

Explanation

Property :

background-size: auto|length|cover|contain|initial|inherit;

Where,
auto - Default value. The background-image contains its width and height
length - Sets the width and height of the background image. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto"
percentage - Sets the width and height of the background image in percent of the parent element. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto"
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
contain - Scale the image to the largest size such that both its width and its height can fit inside the content area
initial - Sets this property to its default value.
inherit - Inherits this property from its parent element.

Usage:


background-size:40px 40px;
background-size:cover;
background-size:contain;
background-size:initial;

Definition:

The background-size property is used to specify the size of the background image.

It takes the following values.
  • length : Sets the width and height of the background image. Either set value as pixels or percentage.
  • cover : Scale the background image to be as large as possible so that the background area is completely covered by the background image.
  • contain : Scale the image to the largest size such that both its width and its height can fit inside the content area
  • initial : Default value.

Example 1:


<div style='background-image: url(test4.jpg); background-size: 50px 50px;
background-repeat: no-repeat;
padding-top: 40px;padding: 35px;
width:200px;height:100px;'>
This is an example for CSS3 background-size property.</div>

Result:
This is an example for CSS3 background-size property.


Example 2:


<div style='background-image: url(test4.jpg); background-size: contain;
background-repeat: no-repeat;
padding-top: 40px;padding: 35px;
width:200px;height:100px;'>
This is an example for CSS3 background-size property.</div>

Result:
This is an example for CSS3 background-size property.



Ask Questions

Ask Question