--- title: background-attachment slug: Web/CSS/background-attachment tags: - CSS - CSS Background - CSS Property - Reference - 'recipe:css-property' translation_of: Web/CSS/background-attachment ---
background-attachment
は CSS のプロパティで、背景画像の位置を{{glossary("Viewport", "ビューポート")}}の中で固定するか、包含ブロックと一緒にスクロールするかを設定します。
/* キーワード値 */ background-attachment: scroll; background-attachment: fixed; background-attachment: local; /* グローバル値 */ background-attachment: inherit; background-attachment: initial; background-attachment: unset;
background-attachment
プロパティは、以下に挙げた値のうちの一つで指定します。
fixed
local
scroll
{{cssinfo}}
<p> There were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again. </p>
p { background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif"); background-attachment: fixed; }
{{EmbedLiveSample("Simple_example")}}
このプロパティは複数の背景画像に対応しており、それぞれの背景画像に異なる <attachment>
をカンマ区切りで指定できます。それぞれの画像には先頭から順番に <attachment>
の値が適用されます。
<p> There were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again. Suddenly she came upon a little three-legged table, all made of solid glass; there was nothing on it except a tiny golden key, and Alice's first thought was that it might belong to one of the doors of the hall; but, alas! either the locks were too large, or the key was too small, but at any rate it would not open any of them. However, on the second time round, she came upon a low curtain she had not noticed before, and behind it was a little door about fifteen inches high: she tried the little golden key in the lock, and to her great delight it fitted! </p>
p { background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif"), url("https://mdn.mozillademos.org/files/12059/startransparent.gif"); background-attachment: fixed, scroll; background-repeat: no-repeat, repeat-y; }
{{EmbedLiveSample("Multiple_background_images")}}
仕様書 | 状態 | 備考 |
---|---|---|
{{SpecName('CSS3 Backgrounds', '#the-background-attachment', 'background-attachment')}} | {{Spec2('CSS3 Backgrounds')}} | 複数の背景画像と local に対応するために一括指定プロパティを拡張。 |
{{SpecName('CSS2.1', 'colors.html#propdef-background-attachment', 'background-attachment')}} | {{Spec2('CSS2.1')}} | 重要な変更なし。 |
{{SpecName('CSS1', '#background-attachment', 'background-attachment')}} | {{Spec2('CSS1')}} | 重要な変更なし。 |
{{Compat("css.properties.background-attachment")}}