background-attachment

  • Revision slug: Web/CSS/background-attachment
  • Revision title: background-attachment
  • Revision id: 440927
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{CSSRef}}

Summary

If a {{Cssxref("background-image")}} is specified, the background-attachment CSS property determines whether that image's position is fixed within the viewport, or scrolls along with its containing block.

{{cssbox("background-attachment")}}

Syntax

Formal syntax:   {{csssyntax("background-attachment")}}
background-attachment: scroll
background-attachment: fixed
background-attachment: local

background-attachment: inherit

Values

scroll
This keyword means that the background image will scroll within the viewport along with the block the image is contained within.
fixed
This keyword means that the background image will not scroll with its containing element, instead remaining stationary within the viewport.
local
This keyword means that the background image will not scroll with its containing element, but will scroll when the element's content scrolls: it is fixed regarding the element's content.

Examples

{{CSSLiveSample('background-attachment.html')}}

body {
  background-image: url("images/cartooncat.png");
  background-attachment: fixed;
}

Multiple background image support

This property supports multiple background images. You can specify a different <attachment> for each background, separated by commas:

body {
  background-image: url("img1.png"), url("img2.png");
  background-attachment: scroll, fixed;
}

Each image is matched with the corresponding attachment type, from first specified to last.

Specifications

Specification Status Comment
{{SpecName('CSS3 Backgrounds', '#the-background-attachment', 'background-attachment')}} {{Spec2('CSS3 Backgrounds')}} The shorthand property has been extended to support multiple backgrounds and the local value.
{{SpecName('CSS2.1', 'colors.html#propdef-background-attachment', 'background-attachment')}} {{Spec2('CSS2.1')}} No significant change.
{{SpecName('CSS1', '#background-attachment', 'background-attachment')}} {{Spec2('CSS1')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.0")}} 4.0 3.5 1.0
Multiple backgrounds 1.0 {{CompatGeckoDesktop("1.9.2")}} 9.0 10.5 1.3
local 4.0 {{CompatGeckoDesktop("25")}} 9.0 10.5 5.0
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 2.1 {{CompatGeckoMobile("1.9.2")}} {{CompatUnknown}} 10.0 3.2
Multiple backgrounds 2.1 {{CompatGeckoMobile("1.9.2")}} {{CompatUnknown}} 10.0 3.2
local {{CompatUnknown}} {{CompatGeckoMobile("25")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<div>
  {{CSSRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>If a {{Cssxref("background-image")}} is specified, the <code>background-attachment</code> <a href="/en-US/docs/CSS" title="CSS">CSS</a> property determines whether that image's position is fixed within the viewport, or scrolls along with its containing block.</p>
<p>{{cssbox("background-attachment")}}</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="twopartsyntaxbox">
<a href="/en-US/docs/CSS/Value_definition_syntax" title="/en-US/docs/CSS/Value_definition_syntax">Formal syntax</a>:   {{csssyntax("background-attachment")}}
</pre>
<pre>
background-attachment: scroll
background-attachment: fixed
background-attachment: local

background-attachment: inherit
</pre>
<h3 id="Values">Values</h3>
<dl>
  <dt>
    <code>scroll</code></dt>
  <dd>
    This keyword means that the background image will scroll within the viewport along with the block the image is contained within.</dd>
  <dt>
    <code>fixed</code></dt>
  <dd>
    This keyword means that the background image will not scroll with its containing element, instead remaining stationary within the viewport.</dd>
  <dt>
    <code>local</code></dt>
  <dd>
    This keyword means that the background image will not scroll with its containing element, but will scroll when the element's content scrolls: it is fixed regarding the element's content.</dd>
</dl>
<h2 id="Examples" name="Examples">Examples</h2>
<p>{{CSSLiveSample('background-attachment.html')}}</p>
<pre class="brush:css; highlight:[3];">
body {
  background-image: url("images/cartooncat.png");
  background-attachment: fixed;
}
</pre>
<h3 id="Multiple_background_image_support" name="Multiple_background_image_support">Multiple background image support</h3>
<p>This property supports multiple background images. You can specify a different <code>&lt;attachment&gt;</code> for each background, separated by commas:</p>
<pre class="brush:css; highlight:[3];">
body {
  background-image: url("img1.png"), url("img2.png");
  background-attachment: scroll, fixed;
}</pre>
<p>Each image is matched with the corresponding attachment type, from first specified to last.</p>
<h2 id="Specifications" name="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', '#the-background-attachment', 'background-attachment')}}</td>
      <td>{{Spec2('CSS3 Backgrounds')}}</td>
      <td>The shorthand property has been extended to support multiple backgrounds and the <code>local</code> value.</td>
    </tr>
    <tr>
      <td>{{SpecName('CSS2.1', 'colors.html#propdef-background-attachment', 'background-attachment')}}</td>
      <td>{{Spec2('CSS2.1')}}</td>
      <td>No significant change.</td>
    </tr>
    <tr>
      <td>{{SpecName('CSS1', '#background-attachment', 'background-attachment')}}</td>
      <td>{{Spec2('CSS1')}}</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>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1.0</td>
        <td>{{CompatGeckoDesktop("1.0")}}</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>10.5</td>
        <td>1.3</td>
      </tr>
      <tr>
        <td><code>local</code></td>
        <td>4.0</td>
        <td>{{CompatGeckoDesktop("25")}}</td>
        <td>9.0</td>
        <td>10.5</td>
        <td>5.0</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>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>2.1</td>
        <td>{{CompatGeckoMobile("1.9.2")}}</td>
        <td>{{CompatUnknown}}</td>
        <td>10.0</td>
        <td>3.2</td>
      </tr>
      <tr>
        <td>Multiple backgrounds</td>
        <td>2.1</td>
        <td>{{CompatGeckoMobile("1.9.2")}}</td>
        <td>{{CompatUnknown}}</td>
        <td>10.0</td>
        <td>3.2</td>
      </tr>
      <tr>
        <td><code>local</code></td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatGeckoMobile("25")}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>{{CSS_Reference:Background}}</li>
  <li><a href="/en-US/docs/CSS/Multiple_backgrounds" title="CSS/Multiple backgrounds">Multiple backgrounds</a></li>
</ul>
Revert to this revision