我想在div中使用精灵图像,我在同一个地方使用多个img
标签显示同一精灵的不同部分(假设精灵是透明的png图像)。我使用在线精灵生成器生成响应精灵与css文件。
以下是我所得到的:jsfiddle
以下是代码本身:
<div id="jersey">
<img style="position:absolute;width:131px;"class="custom-sprite custom-1">
<img style="position:absolute;width:131px" class="custom-sprite custom-2">
<img style="position:absolute;width:131px" class="custom-sprite custom-3">
</div>
.custom-sprite {
max-width: 100%; background-size: 100%;
background:url(https://s-media-cache-ak0.pinimg.com/originals/44/1f/71/441f7163417c68b66183a46d94451ba0.jpg) no-repeat;
}
.custom-sprite {
max-width: 100%; background-size: 100%;
background:url(https://s-media-cache-ak0.pinimg.com/originals/44/1f/71/441f7163417c68b66183a46d94451ba0.jpg) no-repeat;
}
.custom-1 {
background-position: 0 0%;
background-size: 100%;
}
.custom-2 {
background-position: 0 64.606181%;
background-size: 100%;
}
.custom-3 {
background-position: 0 1.595214%;
background-size: 100%;
}
#jersey {
position: absolute;
top: 0px;
left: 0px;
}
问题在于它没有显示任何内容,我希望精灵根据包含它的图像的宽度来改变它的大小。
编辑1:精灵图像用作示例
答案 0 :(得分:1)
您没有在代码中添加图片高度;
请找到更新小提琴link
<强> HTML 强>
<div id="jersey">
<img style="width:131px; height: 200px;"class="custom-sprite custom-1">
<img style="width:131px; height: 200px;" class="custom-sprite custom-2">
<img style="width:131px; height: 200px;" class="custom-sprite custom-3">
</div>
<强> CSS 强>
.custom-sprite {
max-width: 100%; background-size: 100%;
background:url(https://s-media-cache-ak0.pinimg.com/originals/44/1f/71/441f7163417c68b66183a46d94451ba0.jpg) no-repeat;
float: left;
}
.custom-1 { background-position: 0 0%; background-size: 100%; }
.custom-2 { background-position: 0 64.606181%; background-size: 100%; }
.custom-3 { background-position: 0 1.595214%; background-size: 100%; }
#jersey {
position: absolute;
top: 0px;
left: 0px;
width:400px;
height: 400px;
}
答案 1 :(得分:1)