开发者

Background-position not working for CSS sprites

I can't get the "background-position" CSS to target a specific part of the image, I've set the position and can get all the images to load but the area specified does not show. I have written this code so far to no avail:

<head>

<style type="text/css">

ul#links {
    list-style: none;
}

ul#links li a{
    float: left;
    padding: 40px;
    background: url('http://static.tumblr.com/wgijwsy/ixYlr91ax/sprite_colored.png') no-repeat; 
}

#customlink {
    width: 24px;
    height: 24px;
    background-position: -0px -0px ;
    background-repeat: no-repeat;
}

#rss {
    width: 24px;
    height: 24px;
    background-position: -24px -0px;
    background-repeat:no-repeat;
}

#facebook {
    width: 24px;
    height: 24px;
    background-position: -0px -24px;
    background-repeat:no-repeat;
}

#flickr {
    width: 24px;
    height: 24px;
    background-position: -24px -24px;
    background-repeat:no-repeat;
}

#twitter {
    width: 24px;
    height: 24px;
    background-position: -0px -48px;
    background-repeat:no-repeat;
}

#linkedin {
    width: 24px;
    height: 24px;
    background-position: -24px -48px;
    background-repe开发者_JAVA百科at:no-repeat;
}

#google {
    width: 24px;
    height: 24px;
    background-position: -0px -72px;
    background-repeat:no-repeat;
}

#foursquare {
    width: 24px;
    height: 24px;
    background-position: -72px -0px;
    background-repeat:no-repeat;
}

</style>

</head>

<body>
    <ul id="links">
    <h2>Social Networks</h2>
    <li><a href="" id="customlink"></a></li>
    <li><a href="" id="rss"></a></li>
    <li><a href=""id="facebook"></a></li>
    <li><a href=""id="flickr"></a></li>
    <li><a href="" id="twitter"></a></li>
    <li><a href="" id="linkedin"></a></li>
    <li><a href=""id="google"></a></li>
    <li><a href=""id="foursquare"></li>
    </ul>

</body>
</html>


Due to ul#links li a being more specific than for example #facebook, the background rule inside ul#links li a is overriding the background-position on #facebook.

Splitting background into background-image and background-repeat is a simple fix:

ul#links li a{
    float: left;
    background-image: url('http://static.tumblr.com/wgijwsy/ixYlr91ax/sprite_colored.png');
    background-repeat: no-repeat;
    width: 24px;
    height: 24px;
}
#facebook {
    background-position: -0px -24px;
}


Think you were getting a bit confused between styling the LI's and the actual A's, things are overriding what you've set.

here is a working fiddle for you - hope this points you in the correct direction :)

http://jsfiddle.net/JAahh/1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜