CSS Sprite not working

﹥>﹥吖頭↗ 提交于 2019-12-24 17:03:53

问题


I'm trying to set up a sprite menu but it's always showing first slide of srpite, in all links. This is my CSS:

#menu-social{float: right; width: 175px; margin-top: 5px; list-style-type: none;}
#menu-social li{display: inline-block; margin-right: 8px;}
#menu-social li a{display: block; height: 18px; background: url(images/spr_sociales.png) transparent no-repeat;}

#link-google-plus{width: 30px; background-position: 0 0;}
#link-twitter{width: 21px; background-position-x: -30px; /*or background-position:  0 -30px;*/}
#link-facebook{width: 21px; background-position:  0 -51px;}
#link-tuenti{width: 21px; background-position:  0 -72px;}

But it always shows Google+ icon, which is the first one in the sprite.

What am I missing?

Thank you


回答1:


You're having what I call a selector score issue.. Since your first rule #menu-social li a uses an ID + element + element, it overload the following rules #link-facebook, with only the ID.

In other words:

  • For each ID value, apply 100 points
  • For each class value (or pseudo-class or attribute selector), apply 10 points
  • For each element reference, apply 1 point

Give a try on this snippet:

#menu-social { float: right; width: 175px; margin-top: 5px; list-style-type: none;}
#menu-social li { display: inline-block; margin-right: 8px;}
#menu-social li a { display: block; height: 18px; background: url(images/spr_sociales.png) transparent no-repeat;}

#menu-social li a#link-google-plus { width: 30px; background-position: 0 0; }
#menu-social li a#link-twitter { width: 21px; background-position-x: -30px; /*or background-position:  0 -30px;*/ }
#menu-social li a#link-facebook { width: 21px; background-position:  0 -51px; }
#menu-social li a#link-tuent { width: 21px; background-position:  0 -72px; }

A simple/elegant solution would be to change the IDs to classes (you don't need that much IDs), like this:

#menu-social li a { display: block; height: 18px; background: url(images/spr_sociales.png) transparent no-repeat;}

#menu-social li a.link-google-plus { width: 30px; background-position: 0 0; }
#menu-social li a.link-twitter { width: 21px; background-position-x: -30px; /*or background-position:  0 -30px;*/ }
#menu-social li a.link-facebook { width: 21px; background-position:  0 -51px; }
#menu-social li a.link-tuent { width: 21px; background-position:  0 -72px; }

More links on this issue here:

  1. http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
  2. http://css-tricks.com/specifics-on-css-specificity/
  3. http://www.htmldog.com/guides/cssadvanced/specificity/


来源:https://stackoverflow.com/questions/9224010/css-sprite-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!