CSS background-attachment:scroll、local 和 fixed 的区别
CSS 的 background-attachment 决定背景图片跟随什么移动。scroll 让背景跟随元素的盒子,local 让它跟随盒子内部可滚动的内容,fixed 则让背景以浏览器视口为参照。
三种取值分别以什么为参照
初始值为 scroll,该属性不继承。它作用于 background-image 绘制的图片和渐变。只有纯背景色时,看不出差别。
| 取值 | 参照 | 在盒子内滚动 | 滚动页面 |
|---|---|---|---|
scroll |
元素的盒子 | 背景在盒子中保持原位。 | 背景随盒子一起移动。 |
local |
可滚动内容 | 背景随内容一起移动。 | 背景随盒子一起移动。 |
fixed |
视口 | 只有内容移动。 | 盒子移动,图案在屏幕上的位置保持不变。 |
CSS 背景规范的 background-attachment 定义
对比容器内滚动和页面滚动
将代码用 UTF-8 保存为 background-attachment-example.html,然后用浏览器打开。三个盒子使用相同的条纹和高度,只有 background-attachment 不同。屏幕较窄时它们会纵向排列。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>对比 CSS 背景滚动</title>
<style>
body { margin: 16px; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
main { max-width: 56rem; margin-inline: auto; }
.samples { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr)); gap: 16px; }
.panel {
box-sizing: border-box; height: 180px; overflow: auto;
outline: 1px solid #496275; background-color: #064f6d;
background-image: repeating-linear-gradient(#064f6d 0 48px, #4c7489 48px 96px, #567665 96px 144px);
background-position: left top;
}
.scroll { background-attachment: scroll; }
.local { background-attachment: local; }
.fixed { background-attachment: fixed; }
.panel p { width: fit-content; margin: 0 0 52px; padding: 4px 8px; background: white; color: #172a3a; }
.panel:focus-visible { outline: 3px solid #005fcc; }
body::after { content: ""; display: block; height: 80vh; }
</style>
</head>
<body>
<main>
<h1>对比 CSS 背景滚动</h1>
<div class="samples">
<section aria-labelledby="title-scroll">
<h2 id="title-scroll">scroll</h2>
<div class="panel scroll" tabindex="0" role="region" aria-labelledby="title-scroll">
<p>第一行</p>
<p>第二行</p>
<p>第三行</p>
<p>第四行</p>
<p>第五行</p>
</div>
</section>
<section aria-labelledby="title-local">
<h2 id="title-local">local</h2>
<div class="panel local" tabindex="0" role="region" aria-labelledby="title-local">
<p>第一行</p>
<p>第二行</p>
<p>第三行</p>
<p>第四行</p>
<p>第五行</p>
</div>
</section>
<section aria-labelledby="title-fixed">
<h2 id="title-fixed">fixed</h2>
<div class="panel fixed" tabindex="0" role="region" aria-labelledby="title-fixed">
<p>第一行</p>
<p>第二行</p>
<p>第三行</p>
<p>第四行</p>
<p>第五行</p>
</div>
</section>
</div>
</main>
</body>
</html>
- 在各盒子内滚动鼠标滚轮,或按 Tab 将焦点移入,再用方向键滚动。
local的条纹随文字上移,另外两个盒子只有文字移动。 - 从盒子外稍微滚动页面。
scroll和local的条纹跟随盒子。fixed的条纹仍在屏幕上的同一高度,移动的盒子露出图案的另一部分。 - 只把
.local的值改为scroll。重新加载并滚动盒子内部,看到背景留在原位后恢复为local。
位置、尺寸与多层背景
使用 local 时,可滚动的内容区域会影响背景的定位和绘制范围。使用 fixed 时,定位区域是视口,background-origin 会被忽略。元素的背景绘制区域仍会裁切图片。
有多层背景时,按 background-image 的顺序列出取值。以下示例在同一文件夹的 photo.jpg 上叠加渐变。第一项用于渐变,第二项用于照片。
.banner {
background-image: linear-gradient(#0007, #0007), url('photo.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: scroll, fixed;
}
用内容或 min-height 为 .banner 提供高度,然后滚动页面。照片相对视口应用 center 和 cover。要改为以盒子为参照,把第二项也设为 scroll。
fixed 的表现何时会改变
对于根元素以外的元素,如果它自身或祖先的 transform 值不是 none,浏览器可能像 scroll 那样绘制 fixed。计算值仍可能显示为 fixed。可暂时关闭变换,比较实际滚动效果。
WebKit 有记录指出,iOS Safari 的固定背景在某些情况下会随页面滚动。如果设计依赖这一效果,请用目标设备实测。优先考虑可读性和可预测的裁切时,可以默认使用 scroll。
背景滚动异常时如何排查
- 检查盒子内部能否滚动。
overflow: auto需要内容高于盒子的限定高度。 - 背景不显示时,检查元素高度、图片请求,以及设为
no-repeat的图片是否落在绘制区域外。 - 如果计算值又变成
scroll,检查后面的background简写是否重置了它。
相关的背景属性和示例。 设置背景颜色与图片 · 用 background-size 调整图片尺寸



