如何创建和使用 CSS 图像雪碧图


CSS 图像雪碧图是在文档页面中所有图片的合并图像文件。图像雪碧图非常有用,因为图像资源只需要加载一次。通过使用 CSS background-position,可以显示合并图像的不同部分。

我们来看看一个 CSS 图像雪碧图示例 -

示例

 在线演示

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.sprite {
   background: url("Capture.png") no-repeat;
   width: 280px;
   height: 200px;
   display: inline-block;
}
.flag1 {
   background-position: 0px 0px;
}
.flag2 {
   background-position: -255px 0px;
}
.flag3 {
   background-position: -510px 0px;
}
.flag4 {
   background-position: -765px 0px;
}
body {
   text-align: center;
}
</style>
</head>
<body>
<div><h1>Flag Image Sprite</h1></div>
<div class="sprite flag1"></div>
<div class="sprite flag2"></div>
<div class="sprite flag3"></div>
<div class="sprite flag4"></div>
</body>
</html>

输出

更新于: 07-Jan-2020

904 次浏览

开启你的职业生涯

完成课程认证

开始
广告
© . All rights reserved.