CSS分割背景图片的技巧
2011-11-22 22:31
363 查看
核心的属性是这个:
background-position参数值从w3schools找出来的
Property Values
VALUE | DESCRIPTION |
---|---|
top left top center top right center left center center center right bottom left bottom center bottom right | If you only specify one keyword, the second value will be “center”. Default value is: 0% 0% |
x% y% | The first value is the horizontal position and the second value is the vertical. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%. |
xpos ypos | The first value is the horizontal position and the second value is the vertical. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and positions |
inherit | Specifies that the setting of the background-position property should be inherited from the parent element |
CSS:
Using Percentages in Background-Image
.box1
.box2
.box3
.box4
下面这个例子也是常用到的:
鼠标移到按钮上,用稍微亮一点点的图片显示的悬停效果。
用到的green.png原图:
按钮的代码:
帮助
帮助
效果如下:
Green
Orange
Blue
Red
from
sprite.png
最后一个按钮样式和第一个其实是一样的,只不过最后是从sprite.png加载过来的,我使用了CSS
图片拼合生成器将四张图拼接起来了,然后再用CSS去定位。
拼接完成之后的图:
这是另外一篇不错的参考
为了加快页面访问速度,我们可能会想到多种可行的办法:如提升硬性条件方面的性能、减少HTTP请求次数,使用Cache,合并文件(aoao总结的把js和css合并成一个文件非常好玩)等等。
这里主要讲一下如何节省图片的请求次数,至于如何能减少图片请求,不外乎就是减少实际图片的数量和代码的编写质量。
那么,如何才能减少图片的数量呢?我们可以把一些不经常变动的图片由原来分割成的多张小图合并成一张,然后通过CSS背景切割来加快完成渲染,这样就减少了实际图片的数量,也就减少了部分图片请求。至于这种技术,我们暂时叫它css sprites。
如下面这个圆角框架的处理:
[效果演示:http://www.doyoe.com/model/xhtmlcss/style/cssimg/1.htm]
CSS部分:
#list{
width:198px;
}
.hd,.ft{
background:url(skin/bg_01.gif) no-repeat;
}
.hd h3{
height:30px;
line-height:30px;
text-indent:5px;
}
.bd{
border-left:1px solid #aaa;
border-right:1px solid #aaa;
padding:5px;
}
.ft{
overflow:hidden;
height:6px;
background-position:left bottom;
}
XHTML部分:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn" lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>DY CSS图片切割</title>
<meta name="description" content="使用css sprites技术切割合并背景图片以减少HTTP请求" />
<meta name="keywords" content="css sprites, 图片切割, 图片合并, HTTP请求" />
<meta name="Author" content="Doyoe(飘零雾雨), dooyoe@gmail.com" />
</head>
<body>
<div id="list">
<div class="hd"><h3>顶部边框(标题)</h3></div>
<div class="bd">正文<br />大段正文内容<br />还可以写更多的内容</div>
<div class="ft"><!--底部边框--></div>
</div>
</body>
</html>
以往做圆角的东东,一般可能会做成2,3张图片。而可以看到,这个例子仅用了一张图片,所以也会减少不少图片请求。
先看一下CSS是如何写的:首先在.top和.bot中定义了背景图片,这是自然的,要用背景肯定得定义背景图片,这是天经地义的,这里肯定没有什么出采的地方。接着设置background-position样式来达到只显示背景图的某个区域。
因为.top和.bot调用的是同一张背景图片,所以就同时给它们定义,省得单独需要定义2次,这样写也减少了不少字节哦:)
再一个疑问就是中间部分是如何解决边框问题的。可以看到,只需要定义左右两边的border就可以搞定。
至此,关于合并图片,并利用CSS进行背景切割以减少请求的简单例子就搞定了。
相关文章推荐
- css 背景图片平铺技巧
- HTML基础(四):CSS中关于背景图的应用技巧,把很多图片聚合的一张图片中
- css 背景图片平铺技巧
- CSS网页制作技巧:背景图片定位 显示切图的一部分作为背景显示
- css background之设置图片为背景技巧
- CSS图片分割 CSS Sprites 将多个背景集成到一个png图片上CSS定位
- CSS网页制作技巧:背景图片定位
- css background之设置图片为背景技巧
- 网页设计中利用CSS实现背景图片平铺的技巧
- css 背景图片平铺技巧
- css背景设置,让套图中某张图片居中显示的例子
- 纯css实现背景图片半透明,内容不透明
- css 小经验: css中背景图片定位
- CSS Sprites - CSS背景图片的绝对定位应用
- CSS中背景图片位置属性background-position
- 礼拜四log~CSS背景设置技巧和java框架某些问题的解决方法
- 如何让图片按比例响应式缩放、并自动裁剪的css技巧
- 【IE6的疯狂之八】链接伪类(:hover)CSS背景图片有闪动BUG
- 用CSS把背景图片拉伸
- css控制背景图片位置-background-position