每日一个css效果之css sprites

栏目: CSS · 发布时间: 6年前

内容简介:使用css sprites是减少图片资源请求次数的首选方法。将一些背景图片组合到一张图片中,并在css文件中使用以上是雅虎web性能优化最佳实践中的Minimize HTTP Requests(减少http请求次数)中的一个策略,使用css sprites。并不是所有的图片都需要组合到一起,需要组合的图片:ico、图标等小型图片。大型的图不需要应用css sprites。
CSS Sprites are the preferred method for reducing the number of image requests. Combine your background images into a single image and use the CSS background-image and background-position properties to display the desired image segment.

使用css sprites是减少图片资源请求次数的首选方法。将一些背景图片组合到一张图片中,并在css文件中使用 background-imagebackground-position 属性展示需要的图片部分。

以上是雅虎web性能优化最佳实践中的Minimize HTTP Requests(减少http请求次数)中的一个策略,使用css sprites。

并不是所有的图片都需要组合到一起,需要组合的图片:ico、图标等小型图片。大型的图不需要应用css sprites。

实现方式

1.首先把需要的图标利用ps等 工具 合成到一张图片中,例如

每日一个css效果之css sprites

2.编写css样式

在编写展示图片的时候要注意两点:

  1. 图片的定位
  2. 图片容器的宽和高

由于css sprites主要的应用是展示图标等小型图片,通常需要与其他元素展示在一行中,所以在编写css代码时有一个很方便的技巧是将容器的 display 属性设置为 inline-block ,既可以方便的设置容器的宽和高又可以与其他需要的元素共处一行,例如:

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<title>home</title>

	<style>
		.ico {
			height: 16px;
			width: 16px;
			display: inline-block;
			background-image: url("images/ico.png");
		}
		.ico-alert {
			background-position: 0 0;
		}
		.ico-word {
			background-position: -24px 0;
		}
		.ico-excel {
			background-position: -48px 0;
		}
		.ico-ppt {
			background-position: -72px 0;
		}
		.ico-pdf {
			background-position: -96px 0;
		}
		.ico-txt {
			background-position: -120px 0;
		}
		a {
			display: inline-block;
			width: 35px;
			text-align: center;
			margin: 5px;
			text-decoration: none;
		}
	</style>
</head>
<body>
	<div>
		<span class="ico ico-alert"></span><a href="#">alert</a>
		<span class="ico ico-word"></span><a href="#">word</a> <br>
		<span class="ico ico-excel"></span><a href="#">excel</a>
		<span class="ico ico-ppt"></span><a href="#">ppt</a> <br>
		<span class="ico ico-pdf"></span><a href="#">pdf</a>
		<span class="ico ico-txt"></span><a href="#">txt</a>
	</div>
</body>
</html>
复制代码

效果如下

每日一个css效果之css sprites

其核心代码为

/* 设置容器的高度,宽度和图片 */
.ico {
	height: 16px;
	width: 16px;
	display: inline-block;
	background-image: url("images/ico.png");
}
/* 定位显示的背景 */
.ico-alert {
	background-position: 0 0;
}
复制代码

background-position 有三种定位方式

  • 关键词定位 top , right , bottom , left , center 选择其中的两个作为其参数,若只有一个参数则认为第二个为center
  • 百分比定位
  • 像素定位

百分比定位和像素定位可以混用

百分比定位和像素定位:其参数可正可负。当为正数时,代表背景图片作为对象盒子背景图片时靠左和靠上多少距离多少开始显示背景图片;当为负数时代表背景图片作为盒子对象背景图片,将背景图片拖动超出盒子对象左边多远,拖动超出盒子对象上边多远开始显示此背景图片。一般都会使用负数,比较符合人的使用习惯


以上所述就是小编给大家介绍的《每日一个css效果之css sprites》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First Rails

Head First Rails

David Griffiths / O'Reilly Media / 2008-12-30 / USD 49.99

Figure its about time that you hop on the Ruby on Rails bandwagon? You've heard that it'll increase your productivity exponentially, and allow you to created full fledged web applications with minimal......一起来看看 《Head First Rails》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具