关于背景图片的定位(background-position)

最近仔细研究了一下关于css 背景图片的定位问题,也许有的朋友会认为这是个简单的问题,以至于在网上很难搜到关于background-position的详细解释,能搜到的帖子基本上都是千篇一律的把概念复制一下贴出来的。
    不过在这里我还是先要把概念再罗嗦一遍(不想看的朋友可以直接跳过这个部分)

Html代码 复制代码
  1. 语法:   
  2.     background-position : length || length   
  3.     background-position : position || position   
  4.   
  5. 取值:   
  6.     length : 百分数 | 由浮点数字和单位标识符组成的长度值。   
  7.     position : top | center | bottom | left | center | right   
  8.   
  9. 说明:   
  10.     设置或检索对象的背景图像位置。必须先指定 background-image 属性。该属性定位不受对象的补丁属性( padding )设置影响。   
  11.     默认值为: 0% 0% 。此时背景图片将被定位于对象不包括补丁( padding )的内容区域的左上角。   
  12.     如果只指定了一个值,该值将用于横坐标。纵坐标将默认为 50% 。如果指定了两个值,第二个值将用于纵坐标。   
  13.     如果设置值为 right center,因为 right 作为横坐标值将会覆盖 center 值,所以背景图片将被居右定位。   
  14. 对应的脚本特性为 backgroundPosition 。   
  15.   
  16. background-position -- 定义背景图片的位置   
  17.   
  18. 取值:   
  19. <percentage> | <length> | left | center | right ] [ <percentage> | <length> | top | center | bottom ] ]    
  20.  * 水平   
  21.  left: 左   
  22.  center: 中   
  23.  right: 右   
  24.  * 垂直   
  25.  top: 上   
  26.  center: 中   
  27.  bottom: 下   
  28. * 垂直与水平的组合   
  29.  x-% y-%   
  30.  x-pos y-pos   
  31.   
  32.  初始值: 0% 0%   
  33.  继承性: 否   
  34.  适用于: 所有元素   
  35.  background:背景.position:位置.   
语法:
    background-position : length || length
    background-position : position || position

取值:
    length : 百分数 | 由浮点数字和单位标识符组成的长度值。
    position : top | center | bottom | left | center | right

说明:
    设置或检索对象的背景图像位置。必须先指定 background-image 属性。该属性定位不受对象的补丁属性( padding )设置影响。
    默认值为: 0% 0% 。此时背景图片将被定位于对象不包括补丁( padding )的内容区域的左上角。
    如果只指定了一个值,该值将用于横坐标。纵坐标将默认为 50% 。如果指定了两个值,第二个值将用于纵坐标。
    如果设置值为 right center,因为 right 作为横坐标值将会覆盖 center 值,所以背景图片将被居右定位。
对应的脚本特性为 backgroundPosition 。

background-position -- 定义背景图片的位置

取值:
[ <percentage> | <length> | left | center | right ] [ <percentage> | <length> | top | center | bottom ] ] 
 * 水平
 left: 左
 center: 中
 right: 右
 * 垂直
 top: 上
 center: 中
 bottom: 下
* 垂直与水平的组合
 x-% y-%
 x-pos y-pos

 初始值: 0% 0%
 继承性: 否
 适用于: 所有元素
 background:背景.position:位置. 

以下就是我要重点讲解的地方:

Html代码 复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="UTF-8">  
  3. <head>  
  4. <title>background-position定位问题</title>  
  5. <style type="text/css">  
  6. <!--   
  7. *{   
  8. margin:0;   
  9. padding:0;   
  10. }    
  11.   
  12. body {   
  13. text-align:center;   
  14. background:#000;   
  15. }   
  16.   
  17. #container{   
  18. width:1000px;   
  19. margin:0 auto;   
  20. background:#fff url(images/bg.jpg) no-repeat left top;   
  21. height:500px;   
  22. }   
  23. -->  
  24. </style>  
  25. </head>  
  26. <body>  
  27. <div id="container"> </div>  
  28. </body>  
  29. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="UTF-8">
<head>
<title>background-position定位问题</title>
<style type="text/css">
<!--
*{
margin:0;
padding:0;
} 

body {
text-align:center;
background:#000;
}

#container{
width:1000px;
margin:0 auto;
background:#fff url(images/bg.jpg) no-repeat left top;
height:500px;
}
-->
</style>
</head>
<body>
<div id="container"> </div>
</body>
</html>

 

1.

background-position:left top;(见图2).

背景图片的左上角和容器(container)的左上角对齐,超出的部分隐藏。

等同于 background-position:0,0;

也等同于background-position:0%,0%;

2.

background-position:right bottom;(见图1)。

背景图片的右下角和容器(container)的右下角对齐,超出的部分隐藏。

等同于background-positon:100%,100%;

也等同于background-positon:容器(container)的宽度-背景图片的宽度,容器(container)的高度-背景图片的高度

3.

background-position:500px 15px;(见图3)。

背景图片从容器(container)左上角的地方向右移500px,向下移15px,超出的部分隐藏。

4.

background-position:-500px -15px;(见图4)。

背景图片从容器(container)左上角的地方向左移500px,向上移15px,超出的部分隐藏。

5.

background-position:50% 50%;(见图5)。

等同于left:{容器(container)的宽度—背景图片的宽度}*left百分比,超出的部分隐藏。

等同于right:{容器(container)的高度—背景图片的高度}*right百分比,超出的部分隐藏。

例如:background-position:50% 50%;就是background-position:(1000-2000)*50%px,(500-30)*50%px;即background-position:-500px,235px;也就是背景图片从容器(container)的左上角向左移500px,向下移235px;

6.(这种情况背景图片应该用bg2.jpg才能看出效果,bg.jpg的高度太小效果不明显)

background-position:-50% -50%;(见图6)。

等同于left:-{{容器(container)的宽度—背景图片的宽度}*left百分比(百分比都取正值)},超出的部分隐藏。

等同于right:-{{容器(container)的高度—背景图片的高度}*right百分比(百分比都取正值)},超出的部分隐藏。

例如:background-position:-50% -50%;就是background-position:-{(1000-500)*50%}px,-{(500-360)*50%}px;即background- position:-250px,-70px;也就是背景图片从容器(container)的左上角向左移250px,向上移70px;

  • 大小: 164.1 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值