CSS background-position:百分比、居中和边缘偏移
CSS 的 background-position 控制背景图的位置。center 会将图像中心与定位区域的中心对齐。百分比乘以区域尺寸和图像实际显示尺寸的差。
用关键字和边缘距离定位
CSS background-position 规范 的初始值是 0% 0%。只写 center 时,两个方向都居中。只写 left 时,横向靠左、纵向居中。12px 8px 这样的两个长度分别表示距左侧和顶部的距离。
| 值 | 放置方式 | 预览 |
|---|---|---|
left top |
定位区域的左上角 | |
center |
图像与定位区域的中心对齐 | |
right bottom |
定位区域的右下角 | |
right 12px bottom 8px |
距右侧12px、底部8px |
想让图像与右侧或底部保持固定间距,可在边缘关键字后写长度。right 12px bottom 8px 在图像右侧留12px、底部留8px。正值表示从指定边缘向内移动。
计算百分比位置
横向偏移量等于(定位区域宽度 − 图像显示宽度)× 百分比,纵向则用高度。如果区域宽240px、图像宽80px,50% 的偏移就是80px。background-origin 选择定位框,background-size 决定用于计算的图像尺寸。
图像与区域等宽时,横向剩余空间为零,改变百分比也不会移动图像。如果 cover 使图像宽于区域,差值就变成负数。把对齐位置朝右调整,可以看到更多图像右侧内容。
运行示例验证偏移量
把代码保存成 UTF-8 编码的 background-position-example.html 并用浏览器打开。示例使用固定尺寸的渐变作背景图,不需要外部文件。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>背景图位置对比</title>
<style>
body { font: 1rem/1.5 system-ui, sans-serif; margin: 1rem; }
h1 { overflow-wrap: anywhere; }
.samples {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 244px), 1fr));
gap: 1rem;
max-width: 58rem;
}
.tile {
box-sizing: border-box;
width: min(100%, 244px);
height: 104px;
border: 2px dashed #607d8b;
background-color: #e6f2ff;
background-origin: content-box;
background-clip: content-box;
background-repeat: no-repeat;
background-image: linear-gradient(#006c9c, #006c9c);
background-size: 80px 40px;
}
.start { background-position: 0% 0%; }
.half { background-position: 50% 0%; }
.end { background-position: 100% 0%; }
.edge { background-position: right 12px bottom 8px; }
.layers {
background-image: linear-gradient(#006c9c, #006c9c),
linear-gradient(#b95820, #b95820);
background-size: 48px 24px, 36px 24px;
background-position: left 8px top 8px, right 12px bottom 8px;
}
</style>
</head>
<body>
<main>
<h1>背景图位置对比</h1>
<div class="samples">
<div>
<p><code>0% 0%</code></p>
<div class="tile start" aria-hidden="true"></div>
</div>
<div>
<p><code>50% 0%</code></p>
<div class="tile half" aria-hidden="true"></div>
</div>
<div>
<p><code>100% 0%</code></p>
<div class="tile end" aria-hidden="true"></div>
</div>
<div>
<p><code>right 12px bottom 8px</code></p>
<div class="tile edge" aria-hidden="true"></div>
</div>
<div>
<p>两层背景图</p>
<div class="tile layers" aria-hidden="true"></div>
</div>
</div>
</main>
</body>
</html>
内容区宽240px时,前三个框的横向偏移分别是0px、80px和160px。第四个框距右侧12px、底部8px。把 50% 0% 改成 center,还会在纵向居中。
.start { background-position: 0% 0%; }
.half { background-position: 50% 0%; }
.end { background-position: 100% 0%; }
.edge { background-position: right 12px bottom 8px; }
把 .tile 的外部宽度从244px改为204px,内容区宽度变为200px。查看前三个偏移是否变为0px、60px和120px,然后恢复原值。在窄屏上应使用实际内容宽度计算。
分别定位多张背景图
background-image 包含多张图时,位置值按照图像列表的顺序对应。下方第一个值把蓝色图像从左上方移入,第二个值让橙色图像与右侧和底部保持距离。
.layers {
background-image: linear-gradient(#006c9c, #006c9c),
linear-gradient(#b95820, #b95820);
background-size: 48px 24px, 36px 24px;
background-position: left 8px top 8px, right 12px bottom 8px;
}
只有一个位置值时,它会在所有背景层上重复使用。更改图像顺序时,也要调整对应的尺寸和位置。
排查位置看起来没有变化的情况
如果调整百分比后看不出变化,先比较图像的渲染尺寸和定位框的尺寸。可暂时用 background-repeat 的 no-repeat 确认单个图案的起点。
在开发者工具中查看 background-position 和 background-size 的计算值。空元素还需检查高度。后写的 background 简写可能重置之前的位置。



