CSS background-size Property
Last Updated :
13 Sep, 2024
The background-size
property specifies the size of background images. It accepts values in pixels, percentages, or keywords such as cover
(to ensure the image covers the entire element) and contain
(to fit the image within the element), enabling flexible and responsive designs.
Syntax
background-size: auto | length | cover | contain | initial | inherit ;
The background-size
property can be specified using the following syntaxes:
1. Keyword Values:
auto
: Sets the background image to its original size.cover
: Ensures the image covers the entire element without distortion.contain
: Fits the image within the element, maintaining its aspect ratio.
2, Single-Value Syntax:
- Set the width (and optionally the height) of the background image using specific length units (e.g., pixels, points, ems).
3. Double-Value Syntax:
- Specify both the width and height of the background image.
- Use percentages, pixels, or
auto
for each value.
Default Value: Its default value is auto.
Property Values
All the properties are described well with the example below.
Value | Description |
---|
auto | Displays the background image in its original size. |
length | Sets the background image’s width and height. If one value is provided, height defaults to “auto”. |
percentage | Sets the background image’s width and height as a percentage of the parent element. If one value is provided, height defaults to “auto”. |
cover | Scales the background image to cover the entire container, even if part of the image is cut off. |
contain | Resizes the background image to ensure it’s fully visible within the container. |
initial | Sets the background image to its default value. |
inherit | Inherits the background image property from the parent element. |
Let’s discuss commonly used CSS background-size property values in detail below:
1. Using auto
It is used to set the background-size property to its default value. It is used to display the background-image to its original size.
Syntax:
background-size: auto;
This example illustrates the use of the background-size property whose value is set to auto.
HTML
<!DOCTYPE html>
<html>
<head>
<title> background-size Property </title>
<style>
body {
background-image: url(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png');
background-size: auto;
background-repeat: no-repeat;
}
</style>
</head>
<!DOCTYPE html>
<html>
<head>
<title> background-size Property </title>
<style>
body {
background-image: url(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png');
background-size: auto;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<h2>background-size: auto;</h2>
</body>
</html>
<body>
<h2>background-size: auto;</h2>
</body>
</html>
Output:

2. Setting a Specific Length
It is used to set the width and height of the background-image. The first value indicates the width, and the second value indicates the height of the background image in terms of px, pt, em, etc. If any value is not given then it is set to auto.
Syntax:
background-size: length;
Example: This example illustrates the use of the background-size property whose value is set to a specific length value.
HTML
<!DOCTYPE html>
<html>
<head>
<title> background-size Property </title>
<style>
body {
background-image: url(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png');
background-size: 400px 450px;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<h2>background-size: length;</h2>
</body>
</html>
Output:

3. Setting a Specific percentage
It is used to set the width and height in terms of percentage as related to the parent element. The first value indicates the width, and the second value indicates the height of the background image. If any value is not given it is set to auto.
Syntax:
background-size: percentage;
Example: This example illustrates the use of the background-size property whose value is specified in percentage.
HTML
<!DOCTYPE html>
<html>
<head>
<title> background-size Property </title>
<style>
body {
background-image: url(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png');
background-size: 50%;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<h2>background-size: percentage (%);</h2>
</body>
</html>
Output:

4. Setting Up background size to cover
It is used to resize the background image to cover a whole container element.
Syntax:
background-size: cover;
Example: This example illustrates the use of the background-size property whose value is set to cover.
HTML
<!DOCTYPE html>
<html>
<head>
<title> background-size Property </title>
<style>
body {
background-image: url(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png');
background-size: cover;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<h2>background-size: cover;</h2>
</body>
</html>
Output:

- initial: It is used to set an element’s CSS property to its default value.
- inherit: It is used to inherit a property to an element from its parent element property value.
Supported Browsers:
The browsers supported by the background-size property are listed below:
Note: Most modern browsers, including Chrome, Firefox, Edge, and Safari, fully support the background-size
property. However, older versions of Internet Explorer (before version 9) and Opera Mini do not support this property
Similar Reads
CSS @charset Rule
The @charset rule specifies the character encoding used in the style sheet. The @charset must be the first element in the style sheet and if several @charset rules are defined then the only first one is used. It can not be used in the <style> element where the character set of the HTML page is
3 min read
CSS @keyframes Rule
The CSS @keyframes rule defines animations by specifying keyframes that describe the styles to be applied at various points during the animation duration. It allows for smooth transitions and transformations in web elements, controlled through percentages or from-to values. Note:For optimal browser
3 min read
CSS @media Rule
The @media CSS at-rule is used to apply a different set of styles for different media/devices using the Media Queries. A Media Query is mainly used to check the height, width, resolution, and orientation(Portrait/Landscape) of the device. This CSS rule is a way out for making more out of responsive
5 min read
CSS align-content property
The align-content property changes the behavior of the flex-wrap property. It aligns flex lines. It is used to specify the alignment between the lines inside a flexible container. This property defines how each flex line is aligned within a flexbox and is only applicable if flex-wrap: wrap is applie
5 min read
CSS align-items Property
The align-items property in CSS is used to align flex items along the cross-axis within a flex container. It accepts values like flex-start, flex-end, center, baseline, and stretch, controlling the vertical alignment of items in a flexbox. Syntaxalign-items: normal | stretch | center | flex-start |
7 min read
CSS align-self Property
The align-self property in CSS is used to align the selected items in the flexible container in many different manners such as flex-end, center, flex-start, etc. Syntax: align-self: auto|normal|self-start|self-end|stretch|center |baseline, first baseline, last baseline|flex-start |flex-end|baseline
5 min read
CSS all Property
The all property in CSS is the shorthand property used to set all the element's values to their initial or inherited values or in some cases used to set the values to another spreadsheet origin. This property is used to reset all the CSS properties in a document. Syntax:Â all: initial|inherit|unset|
3 min read
CSS animation-delay Property
The animation-delay property specifies a delay for the start of an animation. Defined in seconds (s) or milliseconds (ms), this value determines how long to wait before beginning the animation. Negative values start the animation as if it had already been playing for the specified duration. Syntaxan
3 min read
CSS animation-direction Property
The animation-direction CSS property sets whether an animation should play forward, backward, or alternate back and forth between playing the sequence forward and backward. It controls the visual flow and repetition behavior of animations in web pages, enhancing dynamic content presentation. Syntaxa
4 min read
CSS animation-duration Property
The animation-duration property in CSS is essential for controlling the length of time an animation takes to complete one cycle, making it a vital tool for creating dynamic and engaging web designs. Syntax:animation-duration: time | initial | inherit;Property Value:time: This value is used to specif
3 min read