background-size

  • Revision slug: CSS/background-size
  • Revision title: background-size
  • Revision id: 296390
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{ CSSRef() }}

Summary

The background-size CSS property specifies the size of the background images.

Note: If the value of this property is not set in a {{ cssxref("background") }} shorthand property that is applied to the element after the background-size CSS property, the value of this property is then reset to its initial value by the shorthand property.

  • {{ Xref_cssinitial() }}: auto auto
  • Applies to: all elements
  • {{ Xref_cssinherited() }}: no
  • Percentages: see below
  • Media: {{ Xref_cssvisual() }}
  • {{ Xref_csscomputed() }}: for <length> the absolute value, otherwise a percentage

 

Syntax

background-size:  background-size[, background-size]*

where :

background-size
Is either one of the keywords:
  • contain, which specifies that the background image should be scaled to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area.
  • cover, which specifies that the background image should be scaled to be as small as possible while ensuring both its dimensions are greater than or equal to the corresponding dimensions of the background positioning area.
Or, one or two of the following values, denoting the horizontal size and the vertical size respectively (if only one is specified, the second defaults to auto) :
  • {{ Xref_csspercentage() }} value that scales the background image in the corresponding dimension to the specified percentage of the background positioning area, which is determined by the value of {{ cssxref("background-origin") }}. The background positioning area is, by default, the area containing the content of the box and its padding; the area may also be changed to just the content or to the area containing borders, padding, and content. If the background's {{ cssxref("background-attachment","attachment") }} is fixed, the background positioning area is instead the entire area of the browser window, not including the area covered by scrollbars if they are present. Negative percentages are not allowed.
  • A {{ Xref_csslength() }} value that scales the background image to the specified length in the corresponding dimension. Negative lengths are not allowed.
  • The auto keyword that scales the background image in the corresponding direction such that its intrinsic proportion is maintained.

The interpretation of possible values depends on the image's intrinsic dimensions (width and height) and intrinsic proportion (ratio of width and height).  A bitmap image always has intrinsic dimensions and an intrinsic proportion.  A vector image may have both intrinsic dimensions (and thus must have an intrinsic proportion).  It also may have one or no intrinsic dimensions, and in either case it might or might not have an intrinsic proportion.  Gradients are treated as images with no intrinsic dimensions or intrinsic proportion.

{{ gecko_callout_heading("8.0") }}

This behavior changed in Gecko 8.0 {{ geckoRelease("8.0") }}. Before this, gradients were treated as images with no intrinsic dimensions, with an intrinsic proportion identical to that of the background positioning area.

Background images generated from elements using {{ cssxref("-moz-element") }} (which actually match an element) are currently treated as images with the dimensions of the element, or of the background positioning area if the element is SVG, with the corresponding intrinsic proportion.

Note: This is not the currently-specified behavior, which is that the intrinsic dimensions and proportion should be those of the element in all cases.

The rendered size of the background image is then computed as follows:

If both components of background-size are specified and are not auto:
The background image renders at the specified size.
If the background-size is contain or cover:
The image is rendered, preserving its intrinsic proportion, at the largest size contained within, or covering, the background positioning area.  If the image has no intrinsic proportion, then it is rendered at the size of the background positioning area.
If the background-size is auto or auto auto:
If the image has both intrinsic dimensions, it is rendered at that size.  If it has no intrinsic dimensions and no intrinsic proportion, it is rendered at the size of the background positioning area.  If it has no dimensions but has a proportion, it's rendered as if contain had been specified instead.  If the image has one intrinsic dimension and a proportion, it's rendered at the size determined by that one dimension and the proportion.  If the image has one intrinsic dimension but no proportion, it's rendered using the intrinsic dimension and the corresponding dimension of the background positioning area.
If the background-size has one auto component and one non-auto component:
If the image has an intrinsic proportion, then render it using the specified dimension, and compute the other dimension from the specified dimension and the intrinsic proportion.  If the image has no intrinsic proportion, use the specified dimension for that dimension.  For the other dimension, use the image's corresponding intrinsic dimension if there is one.  If there is no such intrinsic dimension, use the corresponding dimension of the background positioning area.

Note that background sizing for vector images that lack intrinsic dimensions or proportion is not yet fully implemented in all browsers.  Be careful about relying on the behavior described above, and test in multiple browsers (specifically including versions of Firefox 7 or earlier and Firefox 8 or greater) to be sure different renderings are acceptable.

Examples

This demonstration of background-size: cover and this demonstration of background-size: contain are meant to be opened in new windows so that you can see how contain and cover behave when the background positioning area's dimensions vary. This series of demos of how background-size works and interacts with other background-* properties should pretty much cover the remaining ground in how to use background-size alone and in conjunction with other properties.

Notes

If you are specifying a gradient as background and have specified a background-size to go with it, it's best not to specify a size that uses a single auto component, or is specified using only a width value (for example, background-size: 50%).  Rendering of gradients in such cases changed in Firefox 8, and at present it is generally inconsistent across browsers, which do not all implement rendering in full accordance with the CSS3 background-size specification and with the CSS3 Image Values gradient specification.

.bar {
       width: 50px; height: 100px;
       background-image: gradient(...);

       /* NOT RECOMMENDED */
       background-size: 25px;
       background-size: 50%;
       background-size: auto 50px;
       background-size: auto 50%;

       /* OKAY */
       background-size: 25px 50px;
       background-size: 50% 50%;
}

Note that it's particularly not recommended to use a pixel dimension and an auto dimension with a gradient, because it's impossible to replicate rendering in versions of Firefox prior to 8, and in browsers not implementing Firefox 8's rendering, without knowing the exact size of the element whose background is being specified.

Specifications

Specification Status Comment
{{ SpecName('CSS3 Backgrounds', '#the-background-size', 'background-size') }} {{ Spec2('CSS3 Backgrounds') }}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0{{ property_prefix("-webkit") }} [2] {{ CompatGeckoDesktop("1.9.2") }}{{ property_prefix("-moz") }} [4] 9.0 9.5{{ property_prefix("-o") }}
with some bugs [1]
3.0 (522){{ property_prefix("-webkit") }}
but from an older CSS3 draft [2]
   
3.0 {{ CompatGeckoDesktop("2.0") }} 10.0 4.1 (532)
Support for
contain and cover
3.0 {{ CompatGeckoDesktop("1.9.2") }} 9.0 10.0 4.1 (532)
Support for SVG backgrounds {{ CompatUnknown() }} {{ CompatGeckoDesktop("8.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Feature Android Firefox Mobile (Gecko) Windows Phone Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }}{{ property_prefix("-webkit") }}
2.3
{{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Support for SVG backgrounds {{ CompatUnknown() }} {{ CompatGeckoMobile("8.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

[1] Opera 9.5's computation of the background positioning area is incorrect for fixed backgrounds.  Opera 9.5 also interprets the two-value form as a horizontal scaling factor and, from appearances, a vertical clipping dimension. This has been fixed in Opera 10.

[2] WebKit-based browsers originally implemented an older draft of CSS3 background-size in which an omitted second value is treated as duplicating the first value; this draft does not include the contain or cover keywords.

[3] Konqueror 3.5.4 supports -khtml-background-size.

[4] While this property is new in Gecko 1.9.2 (Firefox 3.6), it is possible to stretch a image fully over the background in Firefox 3.5 by using {{ cssxref("-moz-border-image") }}.

.foo {
        background-image: url(bg-image.png);

       -moz-background-size: 100% 100%;           /* Gecko 1.9.2 (Firefox 3.6) */
         -o-background-size: 100% 100%;           /* Opera 9.5 */
    -webkit-background-size: 100% 100%;           /* Safari 3.0 */
            background-size: 100% 100%;           /* Gecko 2.0 (Firefox 4.0) and other CSS3-compliant browsers */
 
       -moz-border-image: url(bg-image.png) 0;    /* Gecko 1.9.1 (Firefox 3.5) */
}

See also

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary">Summary</h2>
<p>The<code> background-size </code>CSS property specifies the size of the background images.</p>
<div class="note">
  <strong>Note:</strong>&nbsp;If the value of this property is not set in a {{ cssxref("background") }} shorthand property that is applied to the element after the <code>background-size</code> CSS property, the value of this property is then reset to its initial value by the shorthand property.</div>
<p><nobr></nobr></p>
<ul class="cssprop">
  <li><nobr><dfn>{{ Xref_cssinitial() }}:</dfn><code> auto auto</code></nobr></li>
  <li><nobr><dfn>Applies to:</dfn> all elements</nobr></li>
  <li><nobr><dfn>{{ Xref_cssinherited() }}:</dfn> no</nobr></li>
  <li><nobr><dfn>Percentages:</dfn> see below</nobr></li>
  <li><nobr><dfn>Media:</dfn> {{ Xref_cssvisual() }}</nobr></li>
  <li><nobr><dfn>{{ Xref_csscomputed() }}:</dfn> for &lt;length&gt; the absolute value, otherwise a percentage</nobr></li>
</ul>
<p>&nbsp;</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
background-size:  <em>background-size</em>[, <em>background-size</em>]*
</pre>
<p>where <strong>: </strong></p>
<dl>
  <dt>
    <em><strong>background-size</strong></em></dt>
  <dd style="margin-left: 40px;">
    Is either one of the keywords:
    <ul>
      <li style="margin-left: 40px;"><code>contain</code>, which specifies that the background image should be scaled to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area.</li>
      <li style="margin-left: 40px;"><code>cover</code>, which specifies that the background image should be scaled to be as small as possible while ensuring both its dimensions are greater than or equal to the corresponding dimensions of the background positioning area.</li>
    </ul>
    Or, one or two of the following values, denoting the horizontal size and the vertical size respectively (if only one is specified, the second defaults to <code>auto</code>) :
    <ul>
      <li>A&nbsp;<code>{{ Xref_csspercentage() }}</code> value that scales the background image in the corresponding dimension to the specified percentage of the background positioning area, which is determined by the value of {{ cssxref("background-origin") }}. The background positioning area is, by default, the area containing the content of the box and its padding; the area may also be changed to just the content or to the area containing borders, padding, and content. If the background's {{ cssxref("background-attachment","attachment") }} is <code>fixed</code>, the background positioning area is instead the entire area of the browser window, not including the area covered by scrollbars if they are present. Negative percentages are not allowed.</li>
      <li>A <code>{{ Xref_csslength() }}</code> value that scales the background image to the specified length in the corresponding dimension. Negative lengths are not allowed.</li>
      <li>The <code>auto</code> keyword that scales the background image in the corresponding direction such that its intrinsic proportion is maintained.</li>
    </ul>
  </dd>
</dl>
<p>The interpretation of possible values depends on the image's intrinsic dimensions (width and height) and intrinsic proportion (ratio of width and height).&nbsp; A bitmap image always has intrinsic dimensions and an intrinsic proportion.&nbsp; A vector image may have both intrinsic dimensions (and thus must have an intrinsic proportion).&nbsp; It also may have one or no intrinsic dimensions, and in either case it might or might not have an intrinsic proportion.&nbsp; Gradients are treated as images with no intrinsic dimensions or intrinsic proportion.</p>
<div class="geckoVersionNote" style="undefined">
  <p>{{ gecko_callout_heading("8.0") }}</p>
  <p>This behavior changed in Gecko 8.0 {{ geckoRelease("8.0") }}. Before this, gradients were treated as images with no intrinsic dimensions, with an intrinsic proportion identical to that of the background positioning area.</p>
</div>
<p>Background images generated from elements using {{ cssxref("-moz-element") }} (which actually match an element) are currently treated as images with the dimensions of the element, or of the background positioning area if the element is SVG, with the corresponding intrinsic proportion.</p>
<div class="note">
  <strong>Note:</strong> This is not the currently-specified behavior, which is that the intrinsic dimensions and proportion should be those of the element in all cases.</div>
<p>The rendered size of the background image is then computed as follows:</p>
<dl>
  <dt>
    If both components of <code>background-size</code> are specified and are not <code>auto</code>:</dt>
  <dd>
    The background image renders at the specified size.</dd>
  <dt>
    If the <code>background-size</code> is <code>contain</code> or <code>cover</code>:</dt>
  <dd>
    The image is rendered, preserving its intrinsic proportion, at the largest size contained within, or covering, the background positioning area.&nbsp; If the image has no intrinsic proportion, then it is rendered at the size of the background positioning area.</dd>
  <dt>
    If the <code>background-size</code> is <code>auto</code> or <code>auto auto</code>:</dt>
  <dd>
    If the image has both intrinsic dimensions, it is rendered at that size.&nbsp; If it has no intrinsic dimensions and no intrinsic proportion, it is rendered at the size of the background positioning area.&nbsp; If it has no dimensions but has a proportion, it's rendered as if <code>contain</code> had been specified instead.&nbsp; If the image has one intrinsic dimension and a proportion, it's rendered at the size determined by that one dimension and the proportion.&nbsp; If the image has one intrinsic dimension but no proportion, it's rendered using the intrinsic dimension and the corresponding dimension of the background positioning area.</dd>
  <dt>
    If the background-size has one <code>auto</code> component and one non-<code>auto</code> component:</dt>
  <dd>
    If the image has an intrinsic proportion, then render it using the specified dimension, and compute the other dimension from the specified dimension and the intrinsic proportion.&nbsp; If the image has no intrinsic proportion, use the specified dimension for that dimension.&nbsp; For the other dimension, use the image's corresponding intrinsic dimension if there is one.&nbsp; If there is no such intrinsic dimension, use the corresponding dimension of the background positioning area.</dd>
</dl>
<p>Note that background sizing for vector images that lack intrinsic dimensions or proportion is not yet fully implemented in all browsers.&nbsp; Be careful about relying on the behavior described above, and test in multiple browsers (specifically including versions of Firefox 7 or earlier and Firefox 8 or greater) to be sure different renderings are acceptable.</p>
<h2 id="Examples">Examples</h2>
<p><a class="external" href="http://whereswalden.com/files/mozilla/background-size/page-cover.html" target="_blank">This demonstration of <code>background-size: cover</code></a> and <a class="external" href="http://whereswalden.com/files/mozilla/background-size/page-contain.html" target="_blank">this demonstration of <code>background-size: contain</code></a> are meant to be opened in new windows so that you can see how <code>contain</code> and <code>cover</code> behave when the background positioning area's dimensions vary. <a class="external" href="http://whereswalden.com/files/mozilla/background-size/more-examples.html">This series of demos of how <code>background-size</code> works and interacts with other <code>background-*</code> properties</a> should pretty much cover the remaining ground in how to use <code>background-size</code> alone and in conjunction with other properties.</p>
<h2 id="Notes">Notes</h2>
<p>If you are specifying a gradient as background and have specified a <code>background-size</code> to go with it, it's best not to specify a size that uses a single auto component, or is specified using only a width value (for example, <code>background-size: 50%</code>).&nbsp; Rendering of gradients in such cases changed in Firefox 8, and at present it is generally inconsistent across browsers, which do not all implement rendering in full accordance with <a class="external" href="http://www.w3.org/TR/css3-background/#the-background-size" title="http://www.w3.org/TR/css3-background/#the-background-size">the CSS3 <code>background-size</code> specification</a> and with <a class="external" href="http://dev.w3.org/csswg/css3-images/#gradients" title="http://dev.w3.org/csswg/css3-images/#gradients">the CSS3 Image Values gradient specification</a>.</p>
<pre class="brush: css">
.bar {
       width: 50px; height: 100px;
       background-image: gradient(...);

       /*&nbsp;NOT&nbsp;RECOMMENDED&nbsp;*/
       background-size: 25px;
       background-size: 50%;
       background-size: auto 50px;
       background-size: auto 50%;

       /*&nbsp;OKAY&nbsp;*/
       background-size: 25px 50px;
       background-size: 50% 50%;
}
</pre>
<p>Note that it's particularly not recommended to use a pixel dimension and an <code>auto</code> dimension with a gradient, because it's impossible to replicate rendering in versions of Firefox prior to 8, and in browsers not implementing Firefox 8's rendering, without knowing the exact size of the element whose background is being specified.</p>
<h2 id="Browser_Compatibility" name="Browser_Compatibility">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{ SpecName('CSS3 Backgrounds', '#the-background-size', 'background-size') }}</td>
      <td>{{ Spec2('CSS3 Backgrounds') }}</td>
      <td>&nbsp;</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_Compatibility" name="Browser_Compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th colspan="3">Safari (WebKit)</th>
      </tr>
      <tr>
        <td rowspan="2">Basic support</td>
        <td>1.0{{ property_prefix("-webkit") }} [2]</td>
        <td>{{ CompatGeckoDesktop("1.9.2") }}{{ property_prefix("-moz") }} [4]</td>
        <td rowspan="2">9.0</td>
        <td>9.5{{ property_prefix("-o") }}<br />
          with some bugs [1]</td>
        <td>3.0 (522){{ property_prefix("-webkit") }}<br />
          but from an older CSS3 draft [2]</td>
        <td rowspan="2">&nbsp;</td>
        <td rowspan="2">&nbsp;</td>
      </tr>
      <tr>
        <td>3.0</td>
        <td>{{ CompatGeckoDesktop("2.0") }}</td>
        <td>10.0</td>
        <td>4.1 (532)</td>
      </tr>
      <tr>
        <td>Support for<br />
          <code>contain</code> and <code>cover</code></td>
        <td>3.0</td>
        <td>{{ CompatGeckoDesktop("1.9.2") }}</td>
        <td>9.0</td>
        <td>10.0</td>
        <td colspan="3">4.1 (532)</td>
      </tr>
      <tr>
        <td>Support for SVG backgrounds</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoDesktop("8.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td colspan="3">{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>Windows Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}{{ property_prefix("-webkit") }}<br />
          2.3</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td>Support for SVG backgrounds</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("8.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>[1] Opera 9.5's computation of the background positioning area is incorrect for fixed backgrounds.&nbsp; Opera 9.5 also interprets the two-value form as a horizontal scaling factor and, from appearances, a vertical <em>clipping</em> dimension. This has been fixed in Opera 10.</p>
<p>[2] WebKit-based browsers originally implemented an older draft of CSS3<code> background-size </code>in which an omitted second value is treated as <em>duplicating</em> the first value; this draft does not include the<code> contain </code>or<code> cover </code>keywords.</p>
<p>[3] Konqueror 3.5.4 supports<code> -khtml-background-size</code>.</p>
<p>[4] While this property is new in Gecko 1.9.2 (Firefox 3.6), it is possible to stretch a image fully over the background in Firefox 3.5 by using {{ cssxref("-moz-border-image") }}.</p>
<pre class="deki-transform">
.foo {
        background-image: url(bg-image.png);

       -moz-background-size: 100% 100%;           /* Gecko 1.9.2 (Firefox 3.6) */
         -o-background-size: 100% 100%;           /* Opera 9.5 */
    -webkit-background-size: 100% 100%;           /* Safari 3.0 */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; background-size: 100% 100%;           /* Gecko 2.0 (Firefox 4.0) and other CSS3-compliant browsers */
 
       -moz-border-image: url(bg-image.png) 0;    /* Gecko 1.9.1 (Firefox 3.5) */
}
</pre>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en/CSS/CSS_Reference" title="CSS Reference">CSS Reference</a></li>
  <li>Background-related CSS properties: {{ CSS_Reference:Background() }}</li>
  <li><a href="/en/CSS/Multiple_backgrounds" title="en/CSS/Multiple backgrounds">Multiple backgrounds</a></li>
</ul>
Revert to this revision