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

如下就是我要重点讲解的地方:
<!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;css

相关文章
相关标签/搜索