Background-size - CSS Property

background-size

Summary of characteristics of the background-size property

Quick description
Sizing the background image.
Status
Standard
Predefined values
auto | contain | cover
Percentages
Calculated based on the element's dimensions.
Initial value
auto
Inherited by default
No.
Repeatable list.
Per grammar: serialization in the order of syntax.
W3C Module
CSS Backgrounds and Borders Module
Document status: CR (Candidate Recommendation)

Syntax diagram of background-size.

Schéma syntaxique de la propriété CSS background-sizeSyntaxe de background-size (taille de l'arrière-plan), which sizes the background image. auto auto cover cover contain contain x | %x x | %x , , y | %y y | %ybackground-size:;background-size:;
Syntax diagram of the background-size property.
The links in the diagram provide more details about the values.
Download the diagram in SVG.

In the diagram, the bold terms are the predefined words of CSS, the other terms are described below:

  • x and y are positive or zero numerical values, followed by one of CSS dimension units.
  • %x is a percentage calculated based on the width of the element.
  • %y is calculated relative to the height of the element.
  • If there are multiple backgrounds, the values can be repeated, separating them with a comma.

Description of the background-size property.

background-size eis the CSS property that defines the dimensions of the background image.

The behavior of the background-size property is closely linked to whether or not dimensions are set in the chosen background (intrinsic dimensions).

  • Images (jpeg, jpg, png, gif files...) necessarily have intrinsic dimensions.
  • Gradients don't have any intrinsic dimensions.
  • SVG files may or may not have intrinsic dimensions.


If you want to check out general explanations about backgrounds in CSS, refer to the page background.

Values for background-size.

  • background-size: auto;

    auto is the default value for background-size.
    If the background is an image, so with intrinsic dimensions, the background dimensions will be those of the original image.
    If the chosen background doesn't have intrinsic dimensions, the auto value is equivalent to contain.

    background-size:auto;
    PNG file (with intrinsic dimensions).
    background-size:auto;
    SVG file without intrinsic dimensions.
  • background-size: cover;

    With the value cover, background-size resizes the background to cover the entire area of the element.
    If the aspect ratio of the background image is different from that of the element, part of the image won't be displayed.

    background-size:cover;
    PNG file (with intrinsic dimensions).
    background-size:cover;
    SVG file without intrinsic dimensions.
  • background-size: contain;

    The background is resized to be fully visible.
    If the background is an image and the image's ratio is different from that of the element, the image may be repeated or part of the element might not be covered by the image (see background-repeat to manage background repetition).

    background-size:contain;
    PNG file (with intrinsic dimensions).
    background-size:contain;
    SVG file without intrinsic dimensions.
  • background-size: 100px;

    background-size used with a single numeric value: a number, positive or zero, with its unit of measurement (see CSS dimension units). If it's a percentage, it will be calculated relative to the element's width.
    This value will be used to size the background image's width.
    The height will be calculated to keep the original image's proportions.

    background-size:50%;
    PNG file (with intrinsic dimensions).
    background-size:50%;
    SVG file without intrinsic dimensions.
  • background-size: 100px 50px;

    background-size used with two numeric values separated by a space. Two numbers, positive or zero, with their unit of measurement (see CSS dimension units).
    The first value indicates the width of the background; the second value its height.
    If these are percentages, the first one is calculated relative to the element's width, and the second relative to its height.

    background-size:50% 100%;
    PNG file (with intrinsic dimensions).
    background-size:50% 100%;
    SVG file without intrinsic dimensions.
  • background-size: 100px 50px, cover, 100% 100%;

    background-size used with a series of values, each of them separated from the others by a comma. This syntax can be used when multiple backgrounds have been defined. Refer to the page background.

    background-size:25%,100%;
  • background-size: initial; (auto) background-size: inherit; background-size: revert; background-size: revert-layer; background-size: revert-rule; background-size: unset;

    See the following pages for more details: initial, inherit, revert, revert-layer, revert-rule, unset.

Animation of the background-size property.

The animation of background-size can be done gradually if you only use numeric values (no predefined values like cover or contain).

Access the background-size property via a program

Change the background size with Javascript.

In JavaScript, the value of the property background-size can be changed with one of the codes below. You can see that JavaScript offers a syntax with the {abbrKebabCase} notation typical of CSS (a dash to separate words), and another syntax with the camel-case notation more common in JavaScript (a capital letter to separate words).

Javascript
let el = document.getElementById('id'); el.style['background-size'] = 'cover'; // or let el = document.getElementById('id'); el.style.backgroundSize = 'cover';

Read the background size in Javascript.

In JavaScript, here's how to read the value of background-size. The value can only be retrieved if the property was assigned directly to the element itself, not via a CSS selector. The value is returned exactly as it was set (a keyword, a pair of values, etc.).

Javascript
let el = document.getElementById('id'); let value = el.style['background-size']; // or let el = document.getElementById('id'); let value = el.style.backgroundSize;

Read the computed value of background-size in Javascript.

The computed value of background-size can be found with the following code. The result is either a keyword (auto, cover, contain, etc.) or a pair of numerical values in pixels, regardless of the unit used in the style sheet.

Javascript
let el = document.getElementById('id'); let value = window.getComputedStyle(el).getPropertyValue('background-size');

Read or change the background size with JQuery.

With jQuery, here's a code example to read or modify the value of the background-size property.

JQuery

// Read the value:
$('#id').prop('background-size');
// Write the value:
$('#id').prop('background-size', 'cover');
$('#id').prop('{background-size': 'cover'});

Read the computed value of background-size with JQuery.

With JQuery, reading the computed value of the background-size property can be done with the following code.

JQuery
let value = $('#id').css('background-size');

Other code examples.

Find more examples of Javascript and JQuery code on the page Javascript and CSS.

Test it yourself.

The buttons below apply the entered value to the background-size property and then display either the value as it was applied or the computed value. This second option lets you see how the value of background-size is stored (serialized). You’ll notice in particular that percentages are correctly stored as percentages (see the chapter on inheritance). All other units are converted to pixels. Predefined values, of course, are kept as they are.

Simulator.

Choix de l'arrière-plan :
background-size :

Browsers compatibility with background-size.

The background-size property doesn’t have any compatibility issues across all current browsers.

Column 1
Support for the background-size property to set the size of background images.
1
background-size
property
Estimated overall support.
96%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Safari

Safari sur IOS

Opéra

Chrome

Edge

Firefox

Chrome pour Androïd

Samsung Internet

Androïd Brower

Firefox pour Androïd

KaiOS Browser

Opéra mini

Historic of the background-size property.

  • CSS Backgrounds and Borders Module Level 3

    Level 3 of the specification also explains how to position the background relative to the background area, and how to crop it.
    Regarding borders, the concept of image borders is introduced, as well as the ideas of rounded corners and box shadows.

    Regarding background-size Initial definition of the background-size property.
    WD
    August 02, 2002
    Working Draft.
    CR
    December 17, 2009
    Candidate Recommendation.
    PR
    REC

See also, about backgrounds.

Everything related to backgrounds and borders is described in the specification CSS Backgrounds and Borders Module of the W3C. Here are the main properties described in this standard.

Properties:

background
Summary of background properties.
background-attachment
Background image scrolling mode.
background-clip
Defines the extent of the background, especially in relation to the border and padding.
background-color
Sets the background color.
background-image
Defines the background image or gradient (multiple images or gradients can coexist).
background-origin
Defines a reference to position the background image.
background-position
Defines how the background image is positioned.
background-position-block
Sets the background positioning in the block direction.
background-position-inline
Sets the background positioning along the lines.
background-position-x
Sets how the background image is positioned horizontally.
background-position-y
Defines how the background image is positioned in the vertical direction.
background-repeat
Defines how the background image is repeated.
Background-size
Sizing the background image.
border
A shorthand property that defines the set of border parameters: width, style and color.
border-bottom
Sets the parameters of the bottom border of the element (thickness, line style, color).
border-bottom-color
Sets the border line color for the bottom of the element.
border-bottom-left-radius
Sets the radius of the rounded corner at the bottom left.
border-bottom-right-radius
Sets the radius of the rounded corner in the lower right.
border-bottom-style
Sets the line style for the border below the element.
border-bottom-width
Sets the thickness of the border line located below the element.
border-color
Sets the border color for all four sides of the element.
border-end-end-radius
Sets the radius of the corner rounding at the end of line and end of block.
border-end-start-radius
Sets the radius of the corner rounding at the end of line and beginning of the block.
border-image
Langue française
Shorthand of border properties made with images.
border-image-outset
Langue française
Sets the overflow of the border image.
border-image-repeat
Langue française
Defines how the border image is repeated or expanded to cover the desired area.
border-image-slice
Langue française
Defines how the border image is clipped.
border-image-source
Sets the path and file name for the image used to build the border.
border-image-width
Langue française
Sets the thickness of the border when the border is made with an image.
border-left
Sets the three parameters of the left border of the element (thickness, line style, color).
border-left-color
Sets the color of the left border line of the element.
border-left-style
Sets the border style for the left side of the element.
border-left-width
Sets the thickness of the border on the left side of the element.
border-radius
Radius of rounded corners.
border-right
Sets the three parameters of the right border of the element (thickness, line style, color).
border-right-color
Sets the color of the border line on the right side of the element.
border-right-style
Sets the line style for the right border of the element.
border-right-width
Sets the thickness of the border on the right side of the element.
border-start-end-radius
Sets the radius of the corner rounding at the beginning of the line and the end of the block.
border-start-start-radius
Sets the radius of the corner rounding at the beginning of the line and the beginning of the block.
border-style
Sets the line type for some or all four borders around the element (solid, double, dotted...).
border-top
Sets the parameters of the top border of the element (thickness, line style, color).
border-top-color
Sets the color of the border line above the element.
border-top-left-radius
Sets the radius of the rounded corner at the top left.
border-top-right-radius
Sets the radius of the rounded corner in the upper-right.
border-top-style
Sets the line style for the left border of the element.
border-top-width
Sets the thickness of the border line above the element.
border-width
Sets the thickness of the border line around the element.
box-shadow
Applies a shading effect to the frames by indicating all the parameters: color, shadow shift, blur, etc.