CSS Background Attachment

The background-attachment property is used to specify that the background image is fixed or scroll with the rest of the page in the browser window.

This property has three values scroll, fixed, and local. Its default value is scroll, which causes the element to not scroll with its content. The local value of this property causes the element to scroll with the content. If we set the value to fixed, the background image will not move during scrolling in the browser.

This CSS property can support multiple background images. We can specify a different value of the background-attachment property for each background-image, separated by commas. Every image will match with the corresponding value of this property.

Syntax

The values of this property are defined as follows.

Property Values

The `background-attachment` property in the CSS determines if a background picture will move with the content or stay still. It may accept any of the subsequent values. It can take one of the following values:

1. Scroll: This is the setting by default. When the user scrolls down the page, the background image will move along with the information.

2. Fixed: No matter how far the user scrolls, the background picture will stay fixed in the position. The backdrop effect becomes "fixed" as a result.

3. Local: The background picture will not scroll with the page but rather with the contents of the element. When utilizing background pictures on the elements that have a fixed size, such as an element with a specified height and the overflow set to scroll, this will become more pertinent.

4. Initial: Returns the property's initial value to it.

5. Inherit: Inherited from its parent element at the first instance.

Examples

Example 1:

This is an illustration of the code about how it could be applied in a CSS rule:

The example background picture is set to 'background.jpg,' and because of the background-attachment: fixed; attribute, it will be stay fixed when the user scrolls down the page. To make sure the background picture covers the entire element, you can use the optional background-size: cover; attribute.

Remember that different browsers, especially older ones, may act differently with regard to the background-attachment parameter. If cross-browser compatibility is an issue for your project, make sure always to be ready to check for it.

  • Parallax Effect: To produce a parallax effect, the background-attachment: fixed; property is frequently used. When the user scrolls, background images move at a different pace than the foreground content, creating the illusion of depth.
  • Performance Considerations: Although the parallax effect can provide a website with a visually appealing feature, it's important to take the performance into account, particularly when using mobile devices. On some devices, fixed backdrop graphics can affect scrolling performance and increase memory use.
  • Shorthand Property: The shorthand background property allows you to specify several background-related variables in a single line. It also includes the background-attachment property to it.

As an illustrated code :

In this example, the center/cover determines the background image's positioning and size, fixed relates to the background-attachment, and no-repeat indicates that the background image should not be repeated.

  • Multiple Backgrounds: You can set different background-attachment values for each background image if you're using the background property to use multiple background images.

For example:

The first background image in this example is centered at the top, covers the element, and has a fixed attachment. Located at the bottom left of the element, the second background image scrolls along with the content.

Recall that the design objectives and user experience factors unique to your project will determine how useful the background-attachment property is. A consistent and aesthetically pleasing outcome can be ensured by experimenting and testing on various browsers and devices.

  • Browser Support and Compatibility: Although most modern browsers support background-attachment, it's important to verify compatibility, particularly if your project needs to work with older browsers. Note that certain mobile browsers may not support fixed backgrounds in the same way.
  • Scrolling Elements: You can apply the background-attachment property to other elements in addition to the body element. For example, designate a fixed background to give a particular area of your webpage a standout visual element.
  • Combining with Other Background Properties: To obtain more exact control over the appearance of your background images, you can combine background-attachment with other background-related properties such as background-position and background-size.

The background image in this example is fixed, placed in the top center, and has its size set to 50% of the container's height and auto width.

Example 2

In this example, we are using the scroll value of the background-attachment property, which is the default behavior. So when the page is scrolled, the background scrolls with it.

Test it Now

Output

CSS background-attachment property

Example 3: Using fixed value

In this example, we are using the fixed value of the background-attachment property. This value fixed the background image, and the image will not move even the rest of the document scrolls.

Test it Now

Output

CSS background-attachment property

Example 4: Using local value

In this example, we are using the local value of the background-attachment property. Here, the background-image will scroll with the scrolling of the element's content.

Test it Now

Output

CSS background-attachment property

Now, let's see another example in which we are using more than one background image for an element.

Example 5:

Here, there are two background images on which we are applying the background-attachment property. The attachment for the first image is set to fixed, whereas the attachment for the second image is set to scroll.

Test it Now

Output

CSS background-attachment property

Conclusion

In conclusion, you can manipulate the behavior of a background image when a user scrolls through a web page using the background-attachment property in CSS. The options include scroll, which causes the image to move with the content, fixed, which creates a fixed background effect, local, which allows scrolling inside an element, initial, which sets the default behavior, and inherit, which allows it to inherit from its parent. Based on the desired visual effect for your website or web application, select the appropriate value.