使用 CSS 雪碧图的优势

cssweb developmentfront end technology更新于 2025/11/27 16:44:17

使用雪碧图的主要优势在于减少 HTTP 请求数量,从而加快网站加载速度。图片加载速度也更快,使得在某些事件中图片切换更加流畅。雪碧图是将多张图片组合成一张单张图片。

以下代码展示了使用 CSS 雪碧图的优势 −

示例

<!DOCTYPE html>
<html>
<head>
<style>
body {
   font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.twitter,.facebook {
   background: url(sprites_64.png) no-repeat;
   display:inline-block;
   width: 64px;
   height: 64px;
   margin:10px 4px;
}
.facebook {
   background-position: 0 -148px;
}
</style>
</head>
<body>
<h1>Image Sprite example</h1>
<a class="twitter"></a>
<a class="facebook"></a>
</body>
</html>

输出

上述代码将产生以下输出 −

以上,我们使用以下命令设置了图像精灵−

.twitter,.facebook {
   background: url(sprites_64.png) no-repeat;
   display:inline-block;
   width: 64px;
   height: 64px;
   margin:10px 4px;
}
.facebook {
   background-position: 0 -148px;
}

相关文章


有用资源