CSS Sprite 未设置样式
我的 CSS 精灵有问题。看起来它正在工作(嗯,对我来说它看起来应该工作)。所有精灵只显示第一个图标,而不显示其他图标。
查了很多地方都没有找到答案。
谢谢你帮助我。
为了节省时间,我把它放在一个单独的页面上。
[死链接]
对于那些因任何原因无法访问该页面的人,这里是代码:
<div class="iconDiv">
<a href="http://facebook.com/" title="Facebook Page">
<img src="./1px.png" class="iconFB linkIcon" alt=""/><span>Facebook</span>
</a>
</div>
<div class="iconDiv">
<a href="http://www.flickr.com/" title="Flickr Page">
<img src="./1px.png" class="iconFL linkIcon" alt=""/><span>Flickr</span>
</a>
</div>
CSS 在这里(缩短):
.iconFB {background-position:0 -40px;}
.iconFL {background-position:0 -82px;}
.iconRSS {background-position:0 -164px;}
.iconY {background-position:0 -246px;}
.linkIcon {
width: 36px;
height: 36px;
background: url(iconSprite.png) no-repeat top left;
}
非常感谢您的帮助!
I am having a problem with my css sprites. It looks like it is working (well, to me it looks like it should work). All the sprites just show the first icon but not the other ones.
Looked at many places and could not find an answer.
Thanks for helping me.
I put it up on a seperate page to save time.
[dead link]
And for those who aren't able to go to that page for any reason, here is the code:
<div class="iconDiv">
<a href="http://facebook.com/" title="Facebook Page">
<img src="./1px.png" class="iconFB linkIcon" alt=""/><span>Facebook</span>
</a>
</div>
<div class="iconDiv">
<a href="http://www.flickr.com/" title="Flickr Page">
<img src="./1px.png" class="iconFL linkIcon" alt=""/><span>Flickr</span>
</a>
</div>
And the css is here (shortened):
.iconFB {background-position:0 -40px;}
.iconFL {background-position:0 -82px;}
.iconRSS {background-position:0 -164px;}
.iconY {background-position:0 -246px;}
.linkIcon {
width: 36px;
height: 36px;
background: url(iconSprite.png) no-repeat top left;
}
Thank you very much for helping!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您遇到CSS 特异性问题。
像这样的CSS:
被这样覆盖:
因为
background
是(除其他外)background-position
的简写。解决此问题的最简单方法是交换两个 CSS 块,如下所示:
(我修复了 Facebook 图标的位置,从
0 -40px
到0 0< /代码>)
You have a CSS specificity problem.
The CSS like this:
is being overridden by this:
because
background
is shorthand for (amongst other things)background-position
.The easiest way to fix this is to swap around the two blocks of CSS, like this:
(I fixed the position of the Facebook icon, from
0 -40px
to0 0
).linkIcon.iconFB 和 .linkIcon.iconFL 等将解决这个问题。目前,您的 linkIcon 样式会覆盖您在 .iconFB 中设置的位置,因为它位于 CSS 文件的更下方。
编辑:(或者只是将 iconFB 等样式移到 .linkIcon 样式下方)
.linkIcon.iconFB and .linkIcon.iconFL and so on will solve this. Currently your linkIcon style overwrites the positions you set in .iconFB because it's further down in the CSS file.
Edit: (or just move the iconFB etc. styles below the .linkIcon styles)
背景图像的位置被 .linkIcon 中的简写背景覆盖。
重新排序 CSS,或者在 .linkIcon 中使用单独的背景图像和背景重复声明(并且没有背景位置)。我认为后者更可取。
The position of the background image is being overridden by the shorthand background in .linkIcon.
Either re-order the CSS, or use separate background-image and background-repeat declarations (and no background-position) in .linkIcon. I'd argue the latter is preferable.
另一种方法是使用 id,因为这些图标可能是唯一的并且不会在同一页面上重复使用。由于它用于“识别”某个图标,因此使用 ID 而不是类是有意义的。
Another way would be to use id's as these icons are probably unique and not reused on the same page. As it is used to "identify" a certain icon it makes sense to use an ID instead of a class.