/

HTML / CSS / JavaScript Tutorial

CSS Property: background-attachment

[this page | pdf | back links]

The CSS (CSS1) background-attachment property indicates whether a background image is fixed or scrolls with the rest of the page.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

fixed

Background is fixed in relation to viewport

local

Background scrolls along with the element’s contents

scroll

(default value). Background scrolls along with the element

 

Default Value:

scroll

JavaScript syntax:

e.g. object.style.backgroundAttachment="fixed"

Inherited:

No

Animatable:

No

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
p {width: 200px; background-image: url(NematrianLogo.png);}
p.x1 {background-attachment: fixed;}
</style>
</head>
<body>
1. Element with default property<br>
<p>1</p><br><br>
2. Element set using in-file HTML style
<p class="x1">2</p><br><br>
3. Element set using JavaScript
<p id="x2">3</p>

<script>
document.getElementById("x2").style.backgroundAttachment =
  "fixed";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyBackgroundAttachment() {
  var x = document.createElement("DIV"); x.style.backgroundAttachment = "fixed"; return (window.getComputedStyle(x, null).backgroundAttachment == "fixed");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile