CSS background-size Property
CSS background-size Property
CSS background-size
property sets the size of the element's background image.
Example
Syntax
Using CSS
Using Javascript
Animatable
Yes, background-size property is animatable. CSS Animatable Properties Reference.
Default Value
Default value for CSS background-size property is auto.
Property Value
The following table provides a list of values for CSS background-size
property.
Value | Explanation |
---|---|
auto | Sets the background image in the corresponding direction such that its intrinsic proportions are maintained. |
length | Sets the width and height of the background image. Negative values are not allowed. |
percentage | Sets the width and height of the background image in percentage of the background positioning area. Negative values are not allowed. |
cover | Sets the image as large as possible without stretching the image. |
contain | Sets the image as large as possible without cropping or stretching the image. |
Using Length
In the following example, we will use pixel to adjust the size of the background image.
Example
Using percentage
In the following example, we will use percentage to adjust the size of the background image.
Example
Using cover
In the following example, we use background-size:cover
to the image as large as possible without stretching the image.
Example
Using contain
In the following example, we use background-size:contain
to set the image as large as possible without cropping or stretching the image.
Example
Using JavaScript
In the following example, we will demonstrate how to change the CSS background-size
property of an element using JavaScript.
Example
Reminder
Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in CSS.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.