代码
提交代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>CSS Sprites</title> <style> /* 清除默认样式 */ * { padding: 0; margin: 0; } /* 这段代码是为了居中显示,不是重点,看不懂的话可以无视 */ body { height: 100vh; display: flex; align-items: center; justify-content: center; } .sprite { /* 宽高要设置的和图标一样大或者稍微大一点点 */ width: 20px; height: 18px; /* 背景图地址设置为雪碧图的位置 */ background-image: url(http://img.mukewang.com/wiki/5ed9bc1809824eef00180038.jpg); /* 禁止背景图重复 */ background-repeat: no-repeat; /* 令背景图显示在正确的位置 */ background-position: top; } /* 当鼠标移入时的样式定义 */ .sprite:hover { /* 令背景图显示在正确的位置 */ background-position: bottom; } </style> </head> <body> <div class="sprite"></div> </body> </html>
运行结果