Chinaunix首页 | 论坛 | 博客
  • 博客访问: 847034
  • 博文数量: 366
  • 博客积分: 10267
  • 博客等级: 上将
  • 技术积分: 4290
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:04
文章分类

全部博文(366)

文章存档

2012年(366)

分类: 系统运维

2012-03-29 21:17:59

关于CSS sprite技术的详解,小弟在这里就不赘述了,因为伟大的英特网上有一篇伟大的技术文档介绍。请移步

在CSS sprite中最重要的就是关于background这个属性的理解,所以本文比较详细的介绍了background这个属性,如有错误,劳请指教。

background的集合有: 'background-color', 'background-image', 'background-repeat', 'background-attachment','background-position', 和'background'。

在介绍background特性之前首先要明确background的作用范围,background是在padding box之内有效,所以背景色和背景图片都在此范围内。

DOCTYPE html>
<html>
<head>
<title>title>
<style type="text/css" rel="stylesheet">
style>

head>
<body>
<div id="div1" style="background-color:grey; padding:50px; width:150px; height:150px; border:2px solid green; margin:50px;">
Test background scope
div>
<div id="div2" style="background-image:url(test.gif); padding:50px; width:150px; height:150px; border:2px solid green; margin:50px;">
Test background scope
div>
div>
body>

html>

background的核心属性background-position

'background-position'

Value: [ [ | | left | center | right ] [ | | top | center | bottom ]? ] | [ [ left | center | right ] || [ top | center | bottom ] ] |
Initial: 0% 0%
Applies to: all elements
Inherited: no
Percentages: refer to the size of the box itself
Media:
Computed value: for the absolute value, otherwise a percentage

(1)百分数

  将background-position设置为百分数,可以将其归结为跟踪原则。background-position:x% y%,是指以图片左上角为原点的A(图片长度的x%,图片高度的y%)点和以paddingbox的左上角为原点的B(paddingbox长度的x%,paddingbox高度的y%)点,相合重合。

(2)数值

将background-position设置为数值,可以将其归结为挂靠原则。background-position:Xpx Ypx,是指图片的左上角挂靠的以paddingbox的左上角为原点的倒直角坐标系,B(Xpx, Ypx)点。例如:background-position:50px 50px,是指将图片的左上角挂靠在以paddingbox的左上角为原点的倒直角坐标系,B(50px, 50px)点.background-position:-50px -50px,是指将图片的左上角挂靠在以paddingbox的左上角为原点的倒直角坐标系,B(-50px, -50px)点.

综合实例:

DOCTYPE html>
<html>
<head>
<title>title>
<style type="text/css" rel="stylesheet">
#div3
{
background-image
:url(test.gif);
padding
:50px;
width
:100px;
height
:100px;
border
:2px solid green;
margin
:50px;
background-position
:20% 20%;
}
#div4
{
background-image
:url(test.gif);
padding
:50px;
width
:150px;
height
:150px;
border
:2px solid green;
margin
:50px;
background-position
:50px 50px;
background-repeat
:no-repeat;
}
#div5
{
background-image
:url(test.gif);
padding
:50px;
width
:150px;
height
:150px;
border
:2px solid green;
margin
:50px;
background-position
:-50px -50px;
background-repeat
:no-repeat;
}
style>


head>
<body>
<div id="div1" style="background-color:grey; padding:50px; width:150px; height:150px; border:2px solid green; margin:50px;">
Test background scope
div>
<div id="div2" style="background-image:url(test.gif); padding:50px; width:150px; height:150px; border:2px solid green; margin:50px;">
Test background scope
div>
<div id="div3">
Test background scope
div>
<div id="div4">
Test background scope
div>
<div id="div5">
Test background scope
div>
div>
body>

html>





分类: web前端
绿色通道:与我联系
1
0
(请您对文章做出评价)
« 博主前一篇:由布局学习CSS——w3c文档复习
» 博主后一篇:从面试题学习Javascript——基础知识
posted @ 2012-03-29 16:55 肖雄 Views(378) Comments(0) Edit 收藏
阅读(684) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~