You are here: Reference > JavaScript > client-side > style handling > properties > backgroundAttachment
backgroundAttachment style property
Specifies or returns whether the backgroundImage should scroll with the contents of the object or not.
With this property you have control over how the background image behaves when the contents of the element are scrolled
(stays in a fixed position, or scrolls with the contents, as it normally does).
When a fixed value is used with backgroundPosition, the position is relative to the client window of the browser. For that reason, the fixed value is used for the body object most of the cases.
Syntax:
You can find the related objects in the Supported by objects section below.
This property is read/write.
CSS page for this property: background-attachment |
Possible values:
The type of this property is string.
One of the following values:
Background image stays fixed. | |||||||
Takes the value of this property from the computed style of the parent element. | |||||||
Default. Background image scrolls with the contents of the object. |
Default: scroll.
Example HTML code 1:
This example illustrates the use of the background-attachment property:
|
||||
<head> <style> .fixed { background-image: url("bg.jpg"); background-repeat: repeat; background-attachment: fixed; overflow: scroll; width: 120px; height: 80px; } .scroll { background-image: url("bg.jpg"); background-repeat: repeat; background-attachment: scroll; overflow: scroll; width: 120px; height: 80px; } </style> </head> <body> <div class="fixed"> Scroll the contents of the page. The background image will stay in a fixed position. </div> <div class="scroll"> Scroll the contents of the page. The background image will scroll. </div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the use of the backgroundAttachment property in JavaScript:
|
||||
<head> <style> .example { background-image: url("bg.jpg"); background-repeat: no-repeat; background-attachment: fixed; background-position-x: center; background-position-y: center; } </style> <script type="text/javascript"> function ToggleAttachment (elem) { var button = document.getElementById ("toggleButton"); var attState = document.body.style.backgroundAttachment; button.innerHTML = (attState == "scroll")? "Change Attachment to scroll!" : "Change Attachment to fixed!"; document.body.style.backgroundAttachment = (attState == "scroll")? "fixed" : "scroll"; } </script> </head> <body class="example"> <div style="height: 300px"> Scroll the contents of the page and watch the background image. </div> <button id="toggleButton" onclick="ToggleAttachment (this);">Change Attachment to scroll!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CSSStyleDeclaration, htmlElement.currentStyle, htmlElement.runtimeStyle, htmlElement.style
HTML elements:
a, abbr, acronym, address, b, bdo, big, blink, blockquote, body, button, caption, center, cite, code, col, colgroup, dd, del, dfn, dir, div, dl, dt, em, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, label, legend, li, listing, marquee, menu, nobr, ol, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, u, ul, var, xmp
Related pages:
background
backgroundColor
backgroundImage
backgroundPosition
backgroundPositionX
backgroundPositionY
backgroundRepeat
backgroundColor
backgroundImage
backgroundPosition
backgroundPositionX
backgroundPositionY
backgroundRepeat
External links:
background-attachment (MSDN)
background-attachment (Mozilla Developer Center)
background-attachment (Safari Reference Library)
background-attachment (W3C)
background-attachment (Mozilla Developer Center)
background-attachment (Safari Reference Library)
background-attachment (W3C)
User Contributed Comments