- CSS background-size Property
- Syntax
- Example of the background-size property:
- Result
- Example of the background-size property with the «%» value :
- Example of the background-size property with the «cover» value :
- Example of the background-size property with the «contain» value :
- Example of the background-size property with the «auto» value:
- Example of the background-size property with the «length» value:
- Values
- Browser support
- CSS background-size Property
- Definition and Usage
- Browser Support
- CSS Syntax
- Property Values
- More Examples
- Example
- Example
- Example
- Example
- Example
- Resizing background images with background-size
- Tiling a large image
- HTML
- CSS
- Result
- Stretching an image
- Scaling an image up
- Special values: «contain» and «cover»
- contain
- HTML
- CSS
- Result
- cover
- HTML
- CSS
- Result
- See also
- Found a content problem with this page?
- background — size
- Кратко
- Пример
CSS background-size Property
The background-size property is used to define the size of the background image.
The background-size property is one of the CSS3 properties.
This property has five values: auto, length, percentages, cover, contain.
Auto sets the background image in its original size. It’s the default value. The length specifies the height and width of the background image. Negative values are invalid. Percentage sets the height and the width of the background-image specified by percentages. Here also the negative values are invalid.
Cover scales the image as large as possible not stretching the image. If the proportions of the image differ from the element, it is cropped either vertically or horizontally so that no space remains.
Contain resizes the background image so that the image is fully visible.
There are images, like JPEG, that have intrinsic sizes and proportions, and images, like gradients, that don’t have intrinsic sizes and proportions. Unless otherwise stated, the second ones always take up the size of the background area of an element.
The background-size property also takes comma-separated-values, and when the element has multiple background images, each of the values will be applied to a matching background image. For example, the first value is applied to the first background-image, the second value to the second image, etc..
Initial Value | auto |
Applies to | All elements. It also applies to ::first-letter and ::first-line. |
Inherited | No. |
Animatable | Yes. The size of the background image is animatable. |
Version | CSS3 |
DOM Syntax | object.style.backgroundSize = «50% 100%»; |
Syntax
background-size: auto | length | cover | contain | initial | inherit;
Example of the background-size property:
html> html> head> title>Title of the document title> style> body < background-image: url("/uploads/media/default/0001/01/b408569013c0bb32b2afb0f0d45e93e982347951.jpeg"); background-size: 300px 200px; background-repeat: no-repeat; > style> head> body> h2>Background size example. h2> p>Here can be any information. p> body> html>
Result
In the above example, the length value is applied. It sets the width and height of the background image. The first value sets the width, and the second value sets the height. If one value is given, the second is set to «auto».
See another example where the width and the height of the background-image are defined by percentages.
Example of the background-size property with the «%» value :
html> html> head> title>Title of the document title> style> body < background-image: url("/uploads/media/default/0001/01/b408569013c0bb32b2afb0f0d45e93e982347951.jpeg"); background-size: 40% 100%; background-repeat: no-repeat; > style> head> body> h2>Background size example. h2> p>Here can be any information. p> body> html>
The «cover» value makes the image as large as possible without stretching the image.
Example of the background-size property with the «cover» value :
html> html> head> title>Title of the document title> style> body < background-image: url("/uploads/media/default/0001/01/b408569013c0bb32b2afb0f0d45e93e982347951.jpeg"); background-size: cover; background-repeat: no-repeat; > style> head> body> h2>Background size example. h2> p>Here can be any information. p> body> html>
Example of the background-size property with the «contain» value :
html> html> head> title>Title of the document title> style> body < background-image: url("/uploads/media/default/0001/01/b408569013c0bb32b2afb0f0d45e93e982347951.jpeg"); background-size: contain; background-repeat: no-repeat; > style> head> body> h2>Background size example. h2> p>Here can be any information. p> body> html>
Example of the background-size property with the «auto» value:
html> html> head> title>Title of the document title> style> body < background-image: url("/uploads/media/default/0001/01/b408569013c0bb32b2afb0f0d45e93e982347951.jpeg"); background-size: auto; background-repeat: no-repeat; > style> head> body> h2>Background size example. h2> p>Here can be any information. p> body> html>
Example of the background-size property with the «length» value:
html> html> head> title>Title of the document title> style> div < width: 260px; height: 190px; background: url("/uploads/media/default/0001/01/b408569013c0bb32b2afb0f0d45e93e982347951.jpeg") no-repeat; background-size: 260px; > style> head> body> h1>CSS background-size property h1> p>Example of the background-size property with a length value. p> div> div> body> html>
Values
Value | Description | Play it |
---|---|---|
auto | This is the default value. It sets the background-image in its original size. | Play it » |
length | Sets the width and height of the background image. The first value sets the width and the second one sets the height.If only one value is given, the second one is set to auto. It is specified by “px”, “em”. | Play it » |
percentage | Sets the width and the height by percentages. The first value sets the width and the second one sets the height.If only one value is given, the second one is set to auto. | Play it » |
cover | Scales background image as large as possible to cover all the background area. | Play it » |
contain | Scales background image to largest size, so that its width and height can fill inside the content area. | Play it » |
initial | Sets the property to its default value. | Play it » |
inherit | Inherits the property from its parent element. |
Browser support
CSS background-size Property
Specify the size of a background-image with «auto» and in pixels:
#example1 <
background: url(mountain.jpg);
background-repeat: no-repeat;
background-size: auto;
>
#example2 background: url(mountain.jpg);
background-repeat: no-repeat;
background-size: 300px 100px;
>
More «Try it Yourself» examples below.
Definition and Usage
The background-size property specifies the size of the background images.
There are four different syntaxes you can use with this property: the keyword syntax («auto», «cover» and «contain»), the one-value syntax (sets the width of the image (height becomes «auto»), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated with comma).
Default value: | auto |
---|---|
Inherited: | no |
Animatable: | yes. Read about animatable Try it |
Version: | CSS3 |
JavaScript syntax: | object.style.backgroundSize=»60px 120px» Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.
CSS Syntax
Property Values
Value | Description | Demo |
---|---|---|
auto | Default value. The background image is displayed in its original size | Demo ❯ |
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». Read about length units | Demo ❯ |
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» | Demo ❯ |
cover | Resize the background image to cover the entire container, even if it has to stretch the image or cut a little bit off one of the edges | Demo ❯ |
contain | Resize the background image to make sure the image is fully visible | Demo ❯ |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
More Examples
Example
Specify the size of a background image with percent:
#example1 <
background: url(mountain.jpg);
background-repeat: no-repeat;
background-size: 100% 100%;
>
#example2 background: url(mountain.jpg);
background-repeat: no-repeat;
background-size: 75% 50%;
>
Example
Specify the size of a background image with «cover»:
Example
Specify the size of a background image with «contain»:
Example
Here we have two background images. We specify the size of the first background image with «contain», and the second background-image with «cover»:
#example1 <
background: url(img_tree.gif), url(mountain.jpg);
background-repeat: no-repeat;
background-size: contain, cover;
>
Example
Use different background properties to create a «hero» image:
.hero-image <
background-image: url(«photographer.jpg»); /* The image used */
background-color: #cccccc; /* Used if the image is unavailable */
height: 500px; /* You must set a specified height */
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
>
Resizing background images with background-size
The background-size CSS property lets you resize the background image of an element, overriding the default behavior of tiling the image at its full size by specifying the width and/or height of the image. By doing so, you can scale the image upward or downward as desired.
Tiling a large image
Let’s consider a large image, a 2982×2808 Firefox logo image. We want (for some reason likely involving horrifyingly bad site design) to tile four copies of this image into a 300×300-pixel element. To do this, we can use a fixed background-size value of 150 pixels.
HTML
div class="tiledBackground">div>
CSS
.tiledBackground background-image: url(https://www.mozilla.org/media/img/logos/firefox/logo-quantum.9c5e96634f92.png); background-size: 150px; width: 300px; height: 300px; border: 2px solid; color: pink; >
Result
Stretching an image
You can also specify both the horizontal and vertical sizes of the image, like this:
background-size: 300px 150px;
The result looks like this:
Scaling an image up
On the other end of the spectrum, you can scale an image up in the background. Here we scale a 32×32 pixel favicon to 300×300 pixels:
.square2 background-image: url(favicon.png); background-size: 300px; width: 300px; height: 300px; border: 2px solid; text-shadow: white 0px 0px 2px; font-size: 16px; >
As you can see, the CSS is actually essentially identical, save the name of the image file.
Special values: «contain» and «cover»
Besides values, the background-size CSS property offers two special size values, contain and cover . Let’s take a look at these.
contain
The contain value specifies that, regardless of the size of the containing box, the background image should be scaled so that each side is as large as possible while not exceeding the length of the corresponding side of the container. Try resizing the example below to see this in action.
HTML
div class="bgSizeContain"> p>Try resizing this element!p> div>
CSS
.bgSizeContain background-image: url(https://www.mozilla.org/media/img/logos/firefox/logo-quantum.9c5e96634f92.png); background-size: contain; width: 160px; height: 160px; border: 2px solid; color: pink; resize: both; overflow: scroll; >
Result
cover
The cover value specifies that the background image should be sized so that it is as small as possible while ensuring that both dimensions are greater than or equal to the corresponding size of the container. Try resizing the example below to see this in action.
HTML
div class="bgSizeCover"> p>Try resizing this element!p> div>
CSS
.bgSizeCover background-image: url(https://www.mozilla.org/media/img/logos/firefox/logo-quantum.9c5e96634f92.png); background-size: cover; width: 160px; height: 160px; border: 2px solid; color: pink; resize: both; overflow: scroll; >
Result
See also
Found a content problem with this page?
This page was last modified on May 24, 2023 by MDN contributors.
Your blueprint for a better internet.
background — size
Нужно чтобы фоновая картинка заняла всю площадь элемента? Вам нужно это свойство!
Время чтения: меньше 5 мин
Кратко
Скопировать ссылку «Кратко» Скопировано
Свойство background — size позволяет изменять размер фонового изображения. Если фоновая картинка не совпадает по размеру с размерами блока, то при помощи этого свойства можно сделать так, чтобы она, картинка занимала всю площадь блока или, наоборот, была определённого размера.
Пример
Скопировать ссылку «Пример» Скопировано
Создадим блок и в качестве фона зададим ему красивую панораму:
div class="element">div>
.element height: 100vh; background-color: #f1f1f1; background-image: url("landscape.jpg"); background-repeat: no-repeat;>
.element height: 100vh; background-color: #f1f1f1; background-image: url("landscape.jpg"); background-repeat: no-repeat; >
Попробуйте поменять размеры фоновой картинки и посмотреть, как будет меняться фоновый паттерн.