Code Snippet
Basic Link Rollover as CSS Sprite
a {
display: block;
background: url(sprite.png) no-repeat;
height: 30px;
width: 250px;
}
a:hover {
background-position: 0 -30px;
}The set height and width ensure only a portion of the sprite.png graphic is shown. The rollover shifts the position of the background image, revealing a different area of the graphic.
Might be worth it to add
a {text-indent: -9000px;}Yep, that’s necessary if you are intending to hide the text and just show image. Just because it’s a sprite doesn’t automatically mean that’s the intention though.
a {
display: block;
text-indent: -10000px;
background: url(sprite.png) no-repeat;
background-position: center top;
}
a:hover {
background-position: center bottom;
}
Vlad, the [background-position] attribute is not needed, you can just append the position “center top” to the already declared background attribute.
Aint this just a plethora of nifty resources!!!
//* this is code of vlad*//
a {
display: block;
text-indent: -10000px;
background: url(sprite.png) no-repeat;
background-position: center top;
}
a:hover {
background-position: center bottom;
}
//*i add some:*//
a:active {
background-position: center center
}
soo: 3 status of bg image
When designing dynamic text links for many CMS Software packages (like WordPress or Joomla), you can use this if the background image of the link changes on hover, but you would not use the “text-indent” attribute.
What about using
spantags and setting them to havedisplay: none;in the CSS instead of setting an indent?This would work Benjamin, but would thwart screen readers I believe. Something to that effect I know. ;)
This might be a stupid question, but what if you have a horizontal list of nav buttons? Would you make a different image for each button? If so, would you have to give each button a class name?
I’m pretty sure I don’t know what I’m talking about. :-)
Thanks,
M
I have used the same trick in my client website for icon links in http://www.toolshandles.com
I need more explanations ? :(
????
Not sure if I can explain it properly, as English is not my native language, but I will try to be clear:
1- In an image editor program(photoshop) you line up the images, icons, etc.. It is better to calculate to have same distance between images.. Icons should be the same dimensions.
2- You load the image in an anchor -> you define it as a block and define the size (width/height) of the image(icon) -> you can also add here the background-position (for mouse out state)
3- on mouse over you define a new background-position, so the image will change state. In our example, on hover, we have defined the position(coordinate) of the image in the sprite.png.
Why we use this?
- Like this, we load one picture(that contain our icons with all states) only one time per page and we use it several times. The image will be cached and reused.
- We don’t have to save and use a lot of images, with 2-3 states.
I hope I was clear enough.
Cheers.
Its really nice explanation brother …
thankx a lot
I’ve been trying to figure sprites out for a while. It would be helpful to see what the tip does.