The Wayback Machine - https://web.archive.org/web/20250303221740/https://www.geeksforgeeks.org/css-background-attachment-property/
Open In App

CSS background-attachment Property

Last Updated : 23 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The background-attachment property sets whether a background image scrolls with the rest of the page or is fixed. It has three possible values: scroll (default, the image moves with content), fixed (image stays in place), and `local` (image scrolls within the element).

Syntax

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

Property Value

PropertyDescription
scrollSets the background image to stay fixed relative to the containing element and scroll with the page. Default value.
fixedSets the background image to stay fixed relative to the viewport.
localSets the background image to scroll along with the content of its container element.
initialSets the background-attachment property to its default value.
inheritInherits the property from its parent element.

Example 1: Using background-attachment: scroll

In this example, we are using background-attachment: scroll property.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        background-attachment property
    </title>

    <style>
        #example {
            background-image:
                url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: scroll;
        }
    </style>
</head>

<body style="text-align:center">
    <h1>GeeksforGeeks</h1>
    <h2> background-attachment: scroll;</h2><br><br>

    <div id="example">
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful.
        </p>

        <br>
        <br>
        <br>
        <p>
            This course is especially designed for the
            Java apprentices who want to hone their
            skills in Java for Coding Interviews and
            Competitive Programming. No matter if you
            are a school student or college student,
            if you have the zeal of programming, this
            is the right time to start.
        </p>

        <br>
        <br>
        <br>
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful
        </p>
        <br>
        <br>
        <br>
        <p>
            This course is especially designed for the
            Java apprentices who want to hone their
            skills in Java for Coding Interviews and
            Competitive Programming. No matter if you
            are a school student or college student,
            if you have the zeal of programming, this
            is the right time to start.
        </p>

        <br>
        <br>
        <br>
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful
        </p>

    </div>
</body>

</html>

Output:

Example 2: Using background-attachment: fixed

In this example, we are using the background-attachment : fixed property.

html
<!DOCTYPE html>
<html>
  
<head>
    <title>
        background-attachment property
    </title>

    <style>
        #example {
            background-image:
                url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: fixed;
        }
    </style>
</head>

<body style="text-align:center">
    <h1>GeeksforGeeks</h1>
    <h2> background-attachment: fixed;</h2><br><br>

    <div id="example">

        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful.
        </p>

        <br>
        <br>
        <br>
        <p>
            This course is especially designed for the
            Java apprentices who want to hone their
            skills in Java for Coding Interviews and
            Competitive Programming. No matter if you
            are a school student or college student,
            if you have the zeal of programming, this
            is the right time to start.
        </p>

        <br>
        <br>
        <br>
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful
        </p>

        <br>
        <br>
        <br>
        <p>
            This course is especially designed for the
            Java apprentices who want to hone their
            skills in Java for Coding Interviews and
            Competitive Programming. No matter if you
            are a school student or college student,
            if you have the zeal of programming, this
            is the right time to start.
        </p>

        <br>
        <br>
        <br>
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful
        </p>
    </div>
</body>
  
</html>

Output:

Example 3: Using background-attachment: local

In this example, we are using background-attachment: local property.

html
<!DOCTYPE html>
<html>
<head>
    <title>
        background-attachment property
    </title>

    <style>
        #example {
            background-image:
                url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: local;
        }
    </style>
</head>

<body style="text-align:center">
    <h1>GeeksforGeeks</h1>
    <h2> background-attachment: local;</h2><br><br>

    <div id="example">

        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful.
        </p>

        <br>
        <br>
        <br>
        <p>
            This course is especially designed for the
            Java apprentices who want to hone their
            skills in Java for Coding Interviews and
            Competitive Programming. No matter if you
            are a school student or college student,
            if you have the zeal of programming, this
            is the right time to start.
        </p>

        <br>
        <br>
        <br>
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful
        </p>

        <br>
        <br>
        <br>
        <p>
            This course is especially designed for the
            Java apprentices who want to hone their
            skills in Java for Coding Interviews and
            Competitive Programming. No matter if you
            are a school student or college student,
            if you have the zeal of programming, this
            is the right time to start.
        </p>

        <br>
        <br>
        <br>
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful
        </p>

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

Output:

Example 4: Using background-attachment: initial

In this example, we are using background-attachment: initial property.

html
<!DOCTYPE html>
<html>
  
<head>
    <title>
        background-attachment property
    </title>

    <style>
        #example {
            background-image:
                url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: initial;
        }
    </style>
</head>

<body style="text-align:center">
    <h1>GeeksforGeeks</h1>
    <h2> background-attachment: initial;</h2><br><br>

    <div id="example">
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful.
        </p>

        <br>
        <br>
        <br>
        <p>
            This course is especially designed for the
            Java apprentices who want to hone their
            skills in Java for Coding Interviews and
            Competitive Programming. No matter if you
            are a school student or college student,
            if you have the zeal of programming, this
            is the right time to start.
        </p>

        <br>
        <br>
        <br>


        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful
        </p>

        <br>
        <br>
        <br>
        <p>
            This course is especially designed for the
            Java apprentices who want to hone their
            skills in Java for Coding Interviews and
            Competitive Programming. No matter if you
            are a school student or college student,
            if you have the zeal of programming, this
            is the right time to start.
        </p>

        <br>
        <br>
        <br>
        <p>
            Prepare for the Recruitment drive of product
            based companies like Microsoft, Amazon,
            Adobe etc with a free online placement
            preparation course. The course focuses on
            various MCQ's & Coding question likely to
            be asked in the interviews & make your
            upcoming placement season efficient and
            successful
        </p>

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

Output:

Supported Browsers

The browser supported by background-attachment property are listed below:

CSS background-attachment Property – FAQs

What is the background-attachment property in CSS?

The background-attachment property in CSS determines whether a background image scrolls with the page, stays fixed, or scrolls within its container.

What values can be used with the background-attachment property?

The possible values are:

  • scroll: The background image scrolls with the content (default).
  • fixed: The background image stays fixed in place when scrolling.
  • local: The background image scrolls with the content inside the element’s container.

What is the difference between background-attachment: fixed and scroll?

With fixed, the background image stays in place while the content scrolls over it. With scroll, the background image moves along with the content as the user scrolls.

When should I use background-attachment: fixed?

The fixed value is commonly used for creating a parallax effect where the background stays in place while content moves over it. It can enhance visual depth on websites.

Why isn’t background-attachment working on mobile devices?

On some mobile devices, background-attachment: fixed may not work due to performance optimizations. Consider using alternative techniques like CSS transforms or JavaScript for similar effects on mobile.



Next Article

Similar Reads

three90RightbarBannerImg