background-attachment

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

background-attachment CSS 屬性設定背景影像的位置是固定在視口內,還是隨其包含塊滾動。

試一試

background-attachment: scroll;
background-attachment: fixed;
background-attachment: local;
background-attachment: local, scroll;
background-attachment: scroll, local;
<section id="default-example">
  <div id="example-element">
    London. Michaelmas term lately over, and the Lord Chancellor sitting in
    Lincoln's Inn Hall. Implacable November weather. As much mud in the streets
    as if the waters had but newly retired from the face of the earth, and it
    would not be wonderful to meet a Megalosaurus, forty feet long or so,
    waddling like an elephantine lizard up Holborn Hill. London. Michaelmas term
    lately over, and the Lord Chancellor sitting in Lincoln's Inn Hall.
    Implacable November weather. As much mud in the streets as if the waters had
    but newly retired from the face of the earth, and it would not be wonderful
    to meet a Megalosaurus, forty feet long or so, waddling like an elephantine
    lizard up Holborn Hill.
  </div>
</section>
body {
  overflow: scroll;
}

#default-example {
  height: 600px;
}

#example-element {
  max-width: 20rem;
  height: 100%;
  background:
    url("/shared-assets/images/examples/lizard.png") right 3rem top 1rem / 15rem
      no-repeat,
    url("/shared-assets/images/examples/moon.jpg") center / 10rem;
  color: #ff5454;
  font-size: 1.5em;
  font-weight: bold;
  overflow: auto;
  padding: 20px;
  text-shadow:
    0 0 0.6rem black,
    0 0 0.6rem black;
}

語法

css
/* Keyword values */
background-attachment: scroll;
background-attachment: fixed;
background-attachment: local;

/* Global values */
background-attachment: inherit;
background-attachment: initial;
background-attachment: revert;
background-attachment: revert-layer;
background-attachment: unset;

background-attachment 屬性被指定為下面列表中的關鍵字值之一。

fixed

背景相對於視口固定。即使元素有滾動機制,背景也不會隨元素移動。

local

背景相對於元素的內容固定。如果元素有滾動機制,背景會隨元素內容滾動,背景繪製區域和背景定位區域相對於元素的滾動區域,而不是邊框。

scroll

背景相對於元素本身固定,不隨其內容滾動。(它實際上附著在元素的邊框上。)

正式定義

初始值scroll
應用於所有元素。也適用於 ::first-letter::first-line
繼承性
計算值同指定值
動畫型別離散

正式語法

background-attachment = 
<attachment>#

<attachment> =
scroll |
fixed |
local

示例

基本示例

HTML

html
<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>

CSS

css
p {
  background-image: url("star-solid.gif");
  background-attachment: fixed;
}

結果

多重背景影像

此屬性支援多重背景影像。您可以為每個背景指定不同的 <attachment>,用逗號分隔。每個影像都與相應的 <attachment> 型別匹配,從第一個指定到最後一個。

HTML

html
<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>

CSS

css
p {
  background-image: url("star-solid.gif"), url("star-transparent.gif");
  background-attachment: fixed, scroll;
  background-repeat: no-repeat, repeat-y;
}

結果

規範

規範
CSS Backgrounds and Borders Module Level 3
# background-attachment

瀏覽器相容性

另見