AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
CSS Background-Size Property |
The CSS background-size property specifies the size of the background image. There are four ways to do this:
Specify length The first way is to specify the dimension of the background image explicitly. This can be specified with one or two values. One value When only one length value is specified, it represents the width of the background image. The height of the background image is automatically adjusted to keep the aspect ratio of the image constant. An aspect ratio is the ratio between the length and the width of an image. Two values When two length values are specified, the first value represents the width of the background image, and the second value represents the height of the image. Example 1: Single length value CSS Declaration
which results in the following: Example 2: Double length value CSS Declaration
which results in the following: Notice that in this case, the background image appears distorted. This is because the specified background size has a different aspect ratio than the original image. Specify percentages The percentage value specifies the size of the background image in relation to the element it is in. This can be specified with one or two values. One value When only one percentage value is specified, it represents the percentage of the space the background image will occupy horizontally within the element. For example, if 50% is used, it means the image will take up half of the element horizontally. The height of the background image is automatically adjusted to keep the same aspect ratio. Two values When two percentage values are specified, the first value represents the % of element the background image will occupy horizontally, and the second value represents the % of element the background image will occupy vertically. If we want to scale the background only as a percentage of the element's height, we use the keyword "auto" as the first value. Example 3: Single percentage value CSS Declaration
which results in the following: In Example 3, the background image's width is 80% of the element's width. Example 4: Double percentage value CSS Declaration
which results in the following: In Example 4, the background image's width is 50% of the element's width, and the background image's height is 80% of the element's height. Cover When the keyword "cover" is used, the background image is resized (with the same aspect ratio) so that the background image covers the entire element. In this case, there can be two results:
Example 5: Cover CSS Declaration
which results in the following: Notice that in Example 5, the background image covers the entire element, leaving no empty space. The height of the background image is the same as the height of the element, and the width of the background image is longer than the width of the element. Contain When the keyword "contain" is used, the background image is resized so that the full background image can be shown within the element. In this case, there can be two results:
Example 6: Contain CSS Declaration
which results in the following: In Example 6, the background image does not cover the entire element. The width of the background image is the same as the width of the element, and the height of the background image is shorter than the height of the element. |
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.