Skip to main content

CSS background-attachment

The background-attachment property sets whether the background image scrolls with the page or stays fixed in place.

Fixed background

The background image is fixed, so it does not move when you scroll the page.

Prompt
An HTML page with a fixed background image that does not scroll with the page. In an internal <style> block, set the body background-image to url("img_tree.png"), with background-repeat no-repeat, background-position right top, and background-attachment fixed. Add a heading and enough paragraphs to make the page scroll, so the fixed background stays in place.
Output: complete HTML page

Scrolling background

The background image scrolls together with the rest of the page.

Prompt
An HTML page with a background image that scrolls with the page. In an internal <style> block, set the body background-image to url("img_tree.png"), with background-repeat no-repeat, background-position right top, and background-attachment scroll. Add a heading and enough paragraphs to make the page scroll, so the background moves with the content.
Output: complete HTML page

The CSS Background Attachment Property

The property covered on this page is background-attachment, which sets whether a background image is fixed or scrolls with the rest of the page.