- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 引入
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 平移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - Justify Items
- CSS - Justify Self
- CSS - 制表符大小
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块级尺寸
- CSS - 最小块级尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移
- CSS - 重音颜色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - Important
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2D 变换
- CSS - 3D 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - background-position 属性
CSS background-position 属性设置元素背景图像的初始位置。图像的位置相对于由background-origin 属性设置的值。
语法
background-position: value;
属性值
| 值 | 描述 |
|---|---|
|
它指定图像的位置。如果指定单个关键字,则另一个值变为中心。 |
| x% y% | 它以百分比指定图像的位置。第一个值用于水平方向,第二个值用于垂直方向。如果只指定一个值,则另一个值为 50%。默认值为 0% 0%。 |
| xpos ypos | 它以长度单位指定图像的位置。第一个值用于水平方向,第二个值用于垂直方向。如果只指定一个值,则另一个值为 50%。 |
| initial | 它将属性设置为其初始值。 |
| inherit | 它从父元素继承属性。 |
CSS 背景位置属性示例
以下示例使用不同的值解释了background-position 属性。
使用单个关键字值的背景位置属性
要设置图像的位置,我们可以使用关键字:left、right、top、bottom、center 与background-position 属性一起使用。如果给出两个值,第一个值设置水平方向,第二个值设置垂直方向。如果给出一个值,则另一个值取为中心。在以下示例中,使用了单个值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.background {
background-image: url('/css/images/tutimg.png');
background-size: 100px 100px;
border: 4px solid gray;
width: 300px;
height: 300px;
background-repeat: no-repeat;
position: relative;
}
.position-left {
background-position: left;
}
.position-right {
background-position: right;
}
.position-top {
background-position: top;
}
.position-bottom {
background-position: bottom;
}
.position-center {
background-position: center;
}
</style>
</head>
<body>
<h2>
CSS background-position Property
</h2>
<h4>
Background Position with Single Values
</h4>
<p>
<strong>
background-position:
</strong>
left (the second value is default "center")
</p>
<div class=" background position-left">
</div>
<p>
<strong>
background-position:
</strong>
right (the second value is default "center")
</p>
<div class="background position-right">
</div>
<p>
<strong>
background-position:
</strong>
top (the second value is default "center")
</p>
<div class="background position-top">
</div>
<p>
<strong>
background-position:
</strong>
bottom (the second value is default "center")
</p>
<div class="background position-bottom">
</div>
<p>
<strong>
background-position:
</strong>
center (the second value is default "center")
</p>
<div class="background position-center">
</div>
</body>
</html>
使用双关键字值的背景位置属性
要设置图像的位置,我们可以使用关键字:left、right、top、bottom、center 与background-position 属性一起使用。如果给出两个值,第一个值设置水平方向,第二个值设置垂直方向。在以下示例中,使用了双值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.background {
background-image: url('/css/images/tutimg.png');
background-size: 100px 100px;
border: 4px solid gray;
width: 300px;
height: 300px;
background-repeat: no-repeat;
position: relative;
}
.position-left {
background-position: left top;
}
.position-right {
background-position: right bottom;
}
.position-top {
background-position: top right;
}
.position-bottom {
background-position: bottom left;
}
</style>
</head>
<body>
<h2>
CSS background-position Property
</h2>
<h4>
Background Position with Double Values
</h4>
<p>
<strong>
background-position:
</strong>
left top
</p>
<div class=" background position-left">
</div>
<p>
<strong>
background-position:
</strong>
right bottom
</p>
<div class="background position-right">
</div>
<p>
<strong>
background-position:
</strong>
top right
</p>
<div class="background position-top">
</div>
<p>
<strong>
background-position:
</strong>
bottom left
</p>
<div class="background position-bottom">
</div>
</body>
</html>
使用单个百分比值的背景位置属性
要设置图像的位置,我们可以使用百分比值与background-position 属性一起使用。如果给出两个值,第一个值设置水平方向,第二个值设置垂直方向。如果给出一个值,则另一个值取为 50%。在以下示例中,使用了单个值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.background {
background-image: url('/css/images/tutimg.png');
background-size: 100px 100px;
border: 4px solid gray;
width: 300px;
height: 300px;
background-repeat: no-repeat;
position: relative;
}
.position-left {
background-position: 20%;
}
.position-right {
background-position: 30%;
}
.position-top {
background-position: 50%;
}
.position-bottom {
background-position: 90%;
}
</style>
</head>
<body>
<h2>
CSS background-position Property
</h2>
<h4>
Background Position with Single Values
</h4>
<p>
<strong>
Note:
</strong>
0%0% - left top, 100% 100% - right bottom
</p>
<p>
<strong>
background-position:
</strong>
20% (the second value is "50%")
</p>
<div class=" background position-left">
</div>
<p>
<strong>
background-position:
</strong>
30% (the second value is "50%")
</p>
<div class="background position-right">
</div>
<p>
<strong>
background-position:
</strong>
50% (the second value is "50%")
</p>
<div class="background position-top">
</div>
<p>
<strong>
background-position:
</strong>
90% (the second value is "50%")
</p>
<div class="background position-bottom">
</div>
</body>
</html>
使用双百分比值的背景位置属性
要设置图像的位置,我们可以使用百分比值与background-position 属性一起使用。如果给出两个值,第一个值设置水平方向,第二个值设置垂直方向。在以下示例中,使用了双值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.background {
background-image: url('/css/images/tutimg.png');
background-size: 100px 100px;
border: 4px solid gray;
width: 300px;
height: 300px;
background-repeat: no-repeat;
position: relative;
}
.position-left {
background-position: 20% 40%;
}
.position-right {
background-position: 30% 70%;
}
.position-top {
background-position: 50% 20%;
}
.position-bottom {
background-position: 90% 45%;
}
</style>
</head>
<body>
<h2>
CSS background-position Property
</h2>
<h4>
Background Position with Double Values
</h4>
<p>
<strong>
Note:
</strong>
0%0% - left top, 100% 100% - right bottom
</p>
<p>
<strong>
background-position:
</strong>
20% 40%
</p>
<div class=" background position-left">
</div>
<p>
<strong>
background-position:
</strong>
30% 70%
</p>
<div class="background position-right">
</div>
<p>
<strong>
background-position:
</strong>
50% 20%
</p>
<div class="background position-top">
</div>
<p>
<strong>
background-position:
</strong>
90% 45%
</p>
<div class="background position-bottom">
</div>
</body>
</html>
使用单个长度值的背景位置属性
要设置图像的位置,我们可以使用长度值与background-position 属性一起使用。如果给出两个值,第一个值设置水平方向,第二个值设置垂直方向。如果给出一个值,则另一个值取为 50%。在以下示例中,使用了单个值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.background {
background-image: url('/css/images/tutimg.png');
background-size: 100px 100px;
border: 4px solid gray;
width: 300px;
height: 300px;
background-repeat: no-repeat;
position: relative;
}
.position-left {
background-position: 20px
}
.position-right {
background-position: 30px;
}
.position-top {
background-position: 50px;
}
.position-bottom {
background-position: 90px;
}
</style>
</head>
<body>
<h2>
CSS background-position Property
</h2>
<h4>
Background Position with Single Values
</h4>
<p>
<strong>
background-position:
</strong>
20px (the second value is center)
</p>
<div class=" background position-left">
</div>
<p>
<strong>
background-position:
</strong>
30px (the second value is center)
</p>
<div class="background position-right">
</div>
<p>
<strong>
background-position:
</strong>
50px (the second value is center)
</p>
<div class="background position-top">
</div>
<p>
<strong>
background-position:
</strong>
90px (the second value is center)
</p>
<div class="background position-bottom">
</div>
</body>
</html>
使用双长度值的背景位置属性
要设置图像的位置,我们可以使用长度值与background-position 属性一起使用。如果给出两个值,第一个值设置水平方向,第二个值设置垂直方向。在以下示例中,使用了双值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.background {
background-image: url('/css/images/tutimg.png');
background-size: 100px 100px;
border: 4px solid gray;
width: 300px;
height: 300px;
background-repeat: no-repeat;
position: relative;
}
.position-left {
background-position: 20px 40px;
}
.position-right {
background-position: 30px 70px;
}
.position-top {
background-position: 50px 150px;
}
.position-bottom {
background-position: 120px 140px;
}
</style>
</head>
<body>
<h2>
CSS background-position Property
</h2>
<h4>
Background Position with Double Values
</h4>
<p>
<strong>
background-position:
</strong>
20px 40px
</p>
<div class=" background position-left">
</div>
<p>
<strong>
background-position:
</strong>
30px 70px
</p>
<div class="background position-right">
</div>
<p>
<strong>
background-position:
</strong>
50px 150px
</p>
<div class="background position-top">
</div>
<p>
<strong>
background-position:
</strong>
120px 140px
</p>
<div class="background position-bottom">
</div>
</body>
</html>
使用不同值的背景位置属性
要设置图像的位置,我们可以组合使用不同的值(例如 % pos)与background-position 属性一起使用。这在以下示例中显示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.background {
background-image: url('/css/images/tutimg.png');
background-size: 100px 100px;
border: 4px solid gray;
width: 300px;
height: 300px;
background-repeat: no-repeat;
position: relative;
}
.position-left {
background-position: 20% 60px;
}
.position-right {
background-position: 60% 120px;
}
</style>
</head>
<body>
<h2>
CSS background-position Property
</h2>
<h4>
Background Position with Different Values
</h4>
<p>
<strong>
background-position:
</strong>
20% 60px
</p>
<div class=" background position-left">
</div>
<p>
<strong>
background-position:
</strong>
60% 120px
</p>
<div class="background position-right">
</div>
</body>
</html>
支持的浏览器
| 属性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |




