Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 967829 of background-image

  • Revision slug: Web/CSS/background-image
  • Revision title: background-image
  • Revision id: 967829
  • Created:
  • Creator: SphinxKnight
  • Is current revision? No
  • Comment removed csslivesample macro

Revision Content

{{CSSRef}}

Summary

The CSS background-image property sets one or several background images for an element. The images are drawn on stacking context layers on top of each other. The first layer specified is drawn as if it is closest to the user.

The borders of the element are then drawn on top of them, and the {{cssxref("background-color")}} is drawn beneath them. How the images are drawn relative to the box and its borders is defined by the {{cssxref("background-clip")}} and {{cssxref("background-origin")}} CSS properties.

If a specified image cannot be drawn (for example, when the file denoted by the specified URI cannot be loaded), browsers handle it as they would a none value.

Note: Even if the images are opaque and the color won't be displayed in normal circumstances, web developers should always specify a {{cssxref("background-color")}}. If the images cannot be loaded—for instance, when the network is down—the background color will be used as a fallback.

{{cssinfo}}

Syntax

background-image: none;
background-image: url(https://www.example.com/bck.png);

background-image: inherit;

Values

none
Is a keyword denoting the absence of images.
<image>
Is an {{Xref_cssimage}} denoting the image to display. There can be several of them, separated by commas, as multiple backgrounds are supported.

Formal syntax

{{csssyntax}}

Examples

Note that the star image is partially transparent and is layered over the cat image.

HTML

<div>
    <p class="catsandstars">
        This paragraph is full of cats<br />and stars.
    </p>
    <p>This paragraph is not.</p>
    <p class="catsandstars">
        Here are more cats for you.<br />Look at them!
    </p>
    <p>And no more.</p>
</div>

CSS

pre, p {
    font-size: 1.5em;
    color: #FE7F88;
    background-color: transparent;
}

div {
  background-image: url("https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png");
}

p {
  background-image: none;
}

.catsandstars {
  background-image:  url("https://mdn.mozillademos.org/files/11991/startransparent.gif"), 
                     url("https://mdn.mozillademos.org/files/7693/catfront.png");
  background-color: transparent;
}

Result

{{EmbedLiveSample('Examples')}}

Specifications

Specification Status Comment
{{SpecName('CSS3 Backgrounds', '#background-image', 'background-image')}} {{Spec2('CSS3 Backgrounds')}} From CSS2 Revision 1, the property has been extended to support multiple backgrounds and any {{xref_cssimage}} CSS data type.
{{SpecName('CSS2.1', 'colors.html#propdef-background-image', 'background-image')}} {{Spec2('CSS2.1')}} From CSS1, the way images with and without intrinsic dimensions are handled is now described.
{{SpecName('CSS1', '#background-image', 'background-image')}} {{Spec2('CSS1')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.0")}}[1] 4.0 3.5 1.0
Multiple backgrounds 1.0 {{CompatGeckoDesktop("1.9.2")}} 9.0 {{CompatVersionUnknown}} 1.3
Gradients 1.0{{property_prefix("-webkit")}} {{CompatGeckoDesktop("1.9.2")}}{{property_prefix("-moz")}} 10 11{{property_prefix("-o")}} 4.0{{property_prefix("-webkit")}}
SVG images 8.0 {{CompatGeckoDesktop("2.0")}} 9.0 9.5 5.0 [2]
element {{CompatUnknown}} {{CompatVersionUnknown}}{{property_prefix("-moz")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
image-rect {{CompatNo}} {{CompatVersionUnknown}}{{property_prefix("-moz")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Any {{xref_cssimage}} value. {{CompatUnknown}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Safari Mobile Opera Mini Opera Mobile Android
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Multiple backgrounds {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Gradients {{CompatVersionUnknown}} {{property_prefix("-webkit")}}
[3]
{{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}} {{property_prefix("-webkit")}} [4]
SVG images {{CompatVersionUnknown}} [2] {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatNo}}
element {{CompatVersionUnknown}}{{property_prefix("-moz")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
image-rect {{CompatVersionUnknown}}{{property_prefix("-moz")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Any {{xref_cssimage}} value {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

[1] If the browser.display.use_document_colors user preference in about:config is set to false, background images will not de displayed.

[2] Support of SVG in CSS background is incomplete in the current version of iOS Safari (5.0). The same for Safari prior 5.0.

[3] Old webkit syntax for iOS 4.2 and older

[4] Old webkit syntax

See also

  • CSS Image Sprites
  • {{HTMLElement("img")}}
  • Image-related articles: {{xref_cssimage}}, {{cssxref("linear-gradient")}}, {{cssxref("radial-gradient")}}, {{cssxref("repeating-linear-gradient")}}, {{cssxref("repeating-radial-gradient")}}, {{cssxref("element")}}.

Revision Source

<div>{{CSSRef}}</div>

<h2 id="Summary">Summary</h2>

<p>The <a href="/en-US/docs/CSS" title="/en-US/docs/CSS">CSS</a> <strong><code>background-image</code></strong> property sets one or several background images for an element. The images are drawn on stacking context layers on top of each other. The first layer specified is drawn as if it is closest to the user.</p>

<p>The <a href="/cn/CSS/border" title="border">borders</a> of the element are then drawn on top of them, and the {{cssxref("background-color")}} is drawn beneath them. How the images are drawn relative to the box and its borders is defined by the {{cssxref("background-clip")}} and {{cssxref("background-origin")}} CSS properties.</p>

<p>If a specified image cannot be drawn (for example, when the file denoted by the specified URI cannot be loaded), browsers handle it as they would a <code>none</code> value.</p>

<div class="note"><strong>Note:</strong> Even if the images are opaque and the color won't be displayed in normal circumstances, web developers should always specify a {{cssxref("background-color")}}. If the images cannot be loaded—for instance, when the network is down—the background color will be used as a fallback.</div>

<p>{{cssinfo}}</p>

<h2 id="Syntax">Syntax</h2>

<pre class="brush: css">
background-image: none;
background-image: url(https://www.example.com/bck.png);

background-image: inherit;
</pre>

<h3 id="Values">Values</h3>

<dl>
 <dt><code>none</code></dt>
 <dd>Is a keyword denoting the absence of images.</dd>
 <dt><code>&lt;image&gt;</code></dt>
 <dd>Is an {{Xref_cssimage}} denoting the image to display. There can be several of them, separated by commas, as <a href="/en-US/docs/CSS/Multiple_backgrounds" title="Multiple backgrounds">multiple backgrounds</a> are supported.</dd>
</dl>

<h3 id="Formal_syntax">Formal syntax</h3>

<pre class="syntaxbox">
{{csssyntax}}
</pre>

<h2 id="Examples">Examples</h2>

<p>Note that the star image is partially transparent and is layered over the cat image.</p>

<h3 id="HTML_Content">HTML</h3>

<pre class="brush: html">
&lt;div&gt;
&nbsp;&nbsp;&nbsp; &lt;p class="catsandstars"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This paragraph is full of cats&lt;br /&gt;and stars.
&nbsp;&nbsp;&nbsp; &lt;/p&gt;
&nbsp;&nbsp;&nbsp; &lt;p&gt;This paragraph is not.&lt;/p&gt;
&nbsp;&nbsp;&nbsp; &lt;p class="catsandstars"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Here are more cats for you.&lt;br /&gt;Look at them!
&nbsp;&nbsp;&nbsp; &lt;/p&gt;
&nbsp;&nbsp;&nbsp; &lt;p&gt;And no more.&lt;/p&gt;
&lt;/div&gt;</pre>

<h3 id="CSS_Content">CSS</h3>

<pre class="brush: css">
pre, p {
&nbsp;&nbsp;&nbsp; font-size: 1.5em;
&nbsp;&nbsp;&nbsp; color: #FE7F88;
    background-color: transparent;
}

div {
&nbsp; background-image: url("https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png");
}

p {
&nbsp; background-image: none;
}

.catsandstars {
&nbsp; background-image:&nbsp; url("https://mdn.mozillademos.org/files/11991/startransparent.gif"), 
                     url("https://mdn.mozillademos.org/files/7693/catfront.png");
  background-color: transparent;
}
</pre>

<h3>Result</h3>

<p>{{EmbedLiveSample('Examples')}}</p>

<h2 id="Specifications">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', '#background-image', 'background-image')}}</td>
   <td>{{Spec2('CSS3 Backgrounds')}}</td>
   <td>From CSS2 Revision 1, the property has been extended to support multiple backgrounds and any {{xref_cssimage}} CSS data type.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS2.1', 'colors.html#propdef-background-image', 'background-image')}}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td>From CSS1, the way images with and without intrinsic dimensions are handled is now described.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS1', '#background-image', 'background-image')}}</td>
   <td>{{Spec2('CSS1')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="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>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatGeckoDesktop("1.0")}}[1]</td>
   <td>4.0</td>
   <td>3.5</td>
   <td>1.0</td>
  </tr>
  <tr>
   <td>Multiple backgrounds</td>
   <td>1.0</td>
   <td>{{CompatGeckoDesktop("1.9.2")}}</td>
   <td>9.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>1.3</td>
  </tr>
  <tr>
   <td>Gradients</td>
   <td>1.0{{property_prefix("-webkit")}}</td>
   <td>{{CompatGeckoDesktop("1.9.2")}}{{property_prefix("-moz")}}</td>
   <td>10</td>
   <td>11{{property_prefix("-o")}}</td>
   <td>4.0{{property_prefix("-webkit")}}</td>
  </tr>
  <tr>
   <td>SVG images</td>
   <td>8.0</td>
   <td>{{CompatGeckoDesktop("2.0")}}</td>
   <td>9.0</td>
   <td>9.5</td>
   <td>5.0 [2]</td>
  </tr>
  <tr>
   <td><code>element</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}{{property_prefix("-moz")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>image-rect</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}{{property_prefix("-moz")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Any {{xref_cssimage}} value.</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Safari Mobile</th>
   <th>Opera Mini</th>
   <th>Opera Mobile</th>
   <th>Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Multiple backgrounds</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Gradients</td>
   <td>{{CompatVersionUnknown}} {{property_prefix("-webkit")}}<br />
    [3]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}&nbsp;{{property_prefix("-webkit")}} [4]</td>
  </tr>
  <tr>
   <td>SVG images</td>
   <td>{{CompatVersionUnknown}} [2]</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>element</code></td>
   <td>{{CompatVersionUnknown}}{{property_prefix("-moz")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>image-rect</code></td>
   <td>{{CompatVersionUnknown}}{{property_prefix("-moz")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Any {{xref_cssimage}} value</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] If the <code>browser.display.use_document_colors</code> user preference in <code>about:config</code> is set to <code>false</code>, background images will not de displayed.</p>

<p>[2] Support of SVG in CSS background is incomplete in the current version of iOS Safari (5.0). The same for Safari prior 5.0.</p>

<p>[3] Old webkit syntax for iOS 4.2 and older</p>

<p>[4]&nbsp;Old webkit syntax</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/Guide/CSS/CSS_Image_Sprites">CSS Image Sprites</a></li>
 <li>{{HTMLElement("img")}}</li>
 <li>Image-related articles: {{xref_cssimage}}, {{cssxref("linear-gradient")}}, {{cssxref("radial-gradient")}}, {{cssxref("repeating-linear-gradient")}}, {{cssxref("repeating-radial-gradient")}}, {{cssxref("element")}}.</li>
</ul>
Revert to this revision