|
| 1 | +--- |
| 2 | +id: background-attachment |
| 3 | +title: Background Attachment |
| 4 | +sidebar_label: Background Attachment |
| 5 | +sidebar_position: 4 |
| 6 | +tags: [background, css background, background attachment, background-attachment property] |
| 7 | +description: Learn how to set the background attachment behavior of an element in CSS using the background-attachment property. |
| 8 | +keywords: |
| 9 | + [ |
| 10 | + background attachment, |
| 11 | + css background attachment, |
| 12 | + background-attachment property, |
| 13 | + css background-attachment, |
| 14 | + ] |
| 15 | +--- |
| 16 | + |
| 17 | +In CSS, the `background-attachment` property is used to specify whether the background image of an element scrolls with the content or remains fixed as the content moves. This property is used to control the attachment behavior of the background image. |
| 18 | + |
| 19 | +<AdsComponent /> |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +The syntax for the `background-attachment` property is as follows: |
| 24 | + |
| 25 | +```css title="index.css" |
| 26 | +selector { |
| 27 | + background-attachment: value; |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +- `selector`: The element to which the background attachment behavior is applied. |
| 32 | +- `background-attachment`: The CSS property used to set the background attachment behavior of an element. |
| 33 | +- `value`: Specifies the attachment behavior of the background image. It can take one of the following values: |
| 34 | + - `scroll`: The background image scrolls along with the content when the content is scrolled. |
| 35 | + - `fixed`: The background image remains fixed within the viewport as the content is scrolled. |
| 36 | + - `local`: The background image scrolls along with the element's contents, rather than the entire page. |
| 37 | + - `initial`: Sets the background attachment behavior to its default value. |
| 38 | +- The default value of the `background-attachment` property is `scroll`. |
| 39 | + |
| 40 | +## Example |
| 41 | + |
| 42 | +In the following example, the `background-attachment` property is used to set the background attachment behavior of a `<div>` element to `fixed`, which makes the background image remain fixed within the viewport as the content is scrolled: |
| 43 | + |
| 44 | +```css title="index.css" |
| 45 | +div { |
| 46 | + background-image: url('path/to/background-image.jpg'); |
| 47 | + background-attachment: fixed; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +In the HTML code below, the CSS rule will apply the `fixed` background attachment behavior to the background image of the `<div>` element: |
| 52 | + |
| 53 | +```html title="index.html" |
| 54 | +<div>This is a div with a fixed background image.</div> |
| 55 | +``` |
| 56 | + |
| 57 | +By using the `background-attachment` property, you can control how the background image behaves when the content is scrolled, allowing you to create various visual effects on your web page. |
| 58 | + |
| 59 | +<AdsComponent /> |
| 60 | + |
| 61 | +:::info Additional Information |
| 62 | + |
| 63 | +- The `background-attachment` property is often used in combination with the `background-image` property to set a background image and define its attachment behavior. |
| 64 | +- The `background-attachment` property can be used to create effects like parallax scrolling, where the background image moves at a different speed than the content, creating a sense of depth. |
| 65 | + |
| 66 | +::: |
| 67 | + |
| 68 | +## Example for background-attachment property |
| 69 | + |
| 70 | +In this example, we have a `<div>` element with a background image that has the `background-attachment` property set to `fixed`. This makes the background image remain fixed within the viewport as the content is scrolled: |
| 71 | + |
| 72 | +<Tabs> |
| 73 | + <TabItem value="HTML" label="index.html"> |
| 74 | + |
| 75 | +```html showLineNumbers |
| 76 | +<!DOCTYPE html> |
| 77 | +<html lang="en"> |
| 78 | +<head> |
| 79 | + <meta charset="UTF-8"> |
| 80 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 81 | + <title>Background Attachment Example</title> |
| 82 | + <link rel="stylesheet" href="styles.css"> |
| 83 | +</head> |
| 84 | +<body> |
| 85 | + <div> |
| 86 | + <h1>Fixed Background Image</h1> |
| 87 | + <p>This is a div with a fixed background image.</p> |
| 88 | + </div> |
| 89 | +</body> |
| 90 | +</html> |
| 91 | +``` |
| 92 | + |
| 93 | +</TabItem> |
| 94 | +<TabItem value="CSS" label="styles.css"> |
| 95 | + |
| 96 | +```css showLineNumbers {3} |
| 97 | +div { |
| 98 | + background-image: url('/assets/jpeg-image.jpg'); |
| 99 | + background-attachment: fixed; |
| 100 | + color: white; |
| 101 | + text-align: center; |
| 102 | + padding: 20px; |
| 103 | +} |
| 104 | + |
| 105 | +h1 { |
| 106 | + font-size: 2em; |
| 107 | + margin-bottom: 0; |
| 108 | +} |
| 109 | + |
| 110 | +p { |
| 111 | + font-size: 1.2em; |
| 112 | + margin-top: 0; |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +</TabItem> |
| 117 | +</Tabs> |
| 118 | + |
| 119 | +Now, you can see the output of the above code in the Browser Window like this: |
| 120 | + |
| 121 | +<BrowserWindow url="http://127.0.0.1:5500/index.html" bodyStyle={{ backgroundImage: "url('/assets/jpeg-image.jpg')", color: "#fff", padding: "20px", minHeight: "200px", textAlign:"center", backgroundAttachment:"fixed" }}> |
| 122 | +<> |
| 123 | + <h1 style={{ fontSize: "2em", marginBottom: "0" }}>Fixed Background Image</h1> |
| 124 | + <p style={{ fontSize: "1.2em", marginTop: "0" }}>This is a div with a fixed background image.</p> |
| 125 | +</> |
| 126 | +</BrowserWindow> |
| 127 | + |
| 128 | +In this example, the background image remains fixed within the viewport as the content is scrolled, creating a visually appealing effect on the web page. |
| 129 | + |
| 130 | +<AdsComponent /> |
| 131 | + |
| 132 | +## Conclusion |
| 133 | + |
| 134 | +The `background-attachment` property in CSS allows you to control the attachment behavior of the background image of an element. By setting the `background-attachment` property to `fixed`, `scroll`, `local`, or `initial`, you can create various visual effects on your web page. Experiment with different values of the `background-attachment` property to achieve the desired background image behavior for your elements. |
0 commit comments