CSS background-attachment Property Tutorial

In this section, we will learn what the background-attachment property is and how to use it in CSS.

Click here to run the example of background-attachment property.

CSS background-attachment Property Definition and Usage

When we set an image for the background of an element, if we scroll the page, both element and its image will scroll together.

But CSS provided a property named background-attachment by which we can stop the background image of an element from scrolling if we scrolled its element (still the element itself scroll but not its background image).

CSS background-attachment Property Syntax

background-attachment: scroll|fixed|local|initial|inherit;

CSS background-attachment Property Value

The values for background-attachment property are:

  • fixed: using this value, the background image of
    an element will stop scrolling.

  • scroll: This is the default value and if used,
    the background image of an element will scroll along its
    element.

  • local: Using this value, the background image
    will scroll as the content within the element scrolled.

Note: the two global values “initial” and “inherit” can be used as well.

Example: background-attachment in CSS

<!DOCTYPE html>
<html>
<head>
<title>CSS is fun :)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
*{
margin:unset;
box-sizing: border-box;
}
.one{
width:50vw;
height:50vh;
overflow:auto;
border:medium solid lightseagreen;
border-radius: 0.3em;
background-image:url(photos/sunset-5408739_1280.jpg);
background-repeat: repeat;
background-attachment:local;
}
.two{
width:50vw;
height:50vh;
overflow:auto;
border:medium solid lightseagreen;
border-radius: 0.3em;
background-image:url(photos/sunset-5408739_1280.jpg);
background-repeat: repeat;
background-attachment:scroll;
}
h1{
color: red;
}
</style>
</head>
<body>
<div class = "one">
<h1>If you scroll, background image will follow because background-attachment is set to local;</h1>
<p>

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

</p>
</div>
<div class = "two">
<h1>If you scroll, background image will not follow because background-attachment is not set to local;</h1>
<p>

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument.

</p>
</div>
</body>
</html>

Example: background attachment fixed

<!DOCTYPE html>
<html>
<head>
<title>CSS is fun :)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
*{
margin:unset;
box-sizing: border-box;
}
.one{
width:100vw;
height:100vh;
border:medium solid lightseagreen;
border-radius: 0.3em;
background-image:url(photos/sunset-5408739_1280.jpg);
background-repeat: no-repeat;
background-attachment:fixed;
}
.two{
width:100vw;
height:100vh;
border:medium solid lightseagreen;
border-radius: 0.3em;
background-image:url(photos/field.jpg);
background-repeat: no-repeat;
background-attachment:fixed;
}
.three{
width:100vw;
height:100vh;
border:medium solid lightseagreen;
border-radius: 0.3em;
background-image:url(photos/forest.jpg);
background-repeat: no-repeat;
background-attachment:fixed;
}
.four{
width:100vw;
height:100vh;
border:medium solid lightseagreen;
border-radius: 0.3em;
background-image:url(photos/dog-5430987_1280.jpg);
background-repeat: no-repeat;
background-attachment:fixed;
}
.five{
width:100vw;
height:100vh;
border:medium solid lightseagreen;
border-radius: 0.3em;
background-image:url(photos/woman-5409203_1280.jpg);
background-repeat: no-repeat;
background-attachment:fixed;
}
.six{
width:100vw;
height:100vh;
border:medium solid lightseagreen;
border-radius: 0.3em;
background-image:url(photos/arc-de-triomphe-5432392_1280.jpg);
background-repeat: no-repeat;
background-attachment:fixed;
}
h1{
color: red;
}
</style>
</head>
<body>
<div class = "one">
</div>
<div class = "two">
</div>
<div class = "three">
</div>
<div class = "four">
</div>
<div class = "five">
</div>
<div class = "six">
</div>
</body>
</html>