设置背景图片默认显示左上角,如果我们想显示图片中某个部分,可以采取负值把需要的部分移动到左上角。
最终效果图:
鼠标滑过的效果:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="css/amazon.css">
</head>
<body>
<div class="a"></div>
<a class="b" href="#"></a>
</body>
</html>
css:
*{
margin: 0;
padding:0;
}
.a{
width: 130px;
height: 45px;
background-image: url(../img/amazon-sprite.png);
}
.b{
width: 42px;
height: 30px;
display: block;
margin: 10px;
background-image: url(../img/amazon-sprite.png);
background-position: -10px -340px;
}
.b:hover{
width: 42px;
height: 30px;
display: block;
margin: 10px;
background-image: url(../img/amazon-sprite.png);
background-position: -57px -337px;
}