Grow your CSS skills. Land your dream job.

Last updated on:

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.

Comments

  1. Permalink to comment#

    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.

    • Vlad
      Permalink to comment#

      a {
      display: block;
      text-indent: -10000px;
      background: url(sprite.png) no-repeat;
      background-position: center top;
      }

      a:hover {
      background-position: center bottom;
      }

    • Dyllon
      Permalink to comment#

      Vlad, the [background-position] attribute is not needed, you can just append the position “center top” to the already declared background attribute.

  2. timani
    Permalink to comment#

    Aint this just a plethora of nifty resources!!!

  3. drudube
    Permalink to comment#

    //* 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.

  4. What about using span tags and setting them to have display: 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. ;)

  5. Michael
    Permalink to comment#

    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

  6. I have used the same trick in my client website for icon links in http://www.toolshandles.com

  7. Ahmad Awais
    Permalink to comment#

    I need more explanations ? :(

  8. Ahmad Awais
    Permalink to comment#

    ????

    • Permalink to comment#

      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

  9. Gigi
    Permalink to comment#

    I’ve been trying to figure sprites out for a while. It would be helpful to see what the tip does.

  10. Ian
    Permalink to comment#

    I’m confused, with all of these examples, I only see one image in each of them.

    I thought a sprite always had two images? What would be the complete code for a sprite with both images (hover and static– or hover, static and used link)?

    Thanks in advance

    Ian

    • Der
      Permalink to comment#

      u r not seen the big picture here, the idea in css sprite is that u r using 1 image file havin 2 pics side to side or top to bottom doesn’t matter is up to u, usin css to add bkgrd to ur container then usin pseudo ‘hover’ to reposition that graphic to have an illusion of two graphics inside that container

  11. Greg
    Permalink to comment#

    How can this work in line? I have a line of text that needs to be followed with a button that when clicked downloads a pdf.

    Perhaps it is easier to use two images rather than a sprite?

  12. Splungeman
    Permalink to comment#

    If the visible portion of your sprite has a dimension that would fit in a sidebar for example, would the entire sprite work if it is actually wider than the displayed image?

  13. Thanks, this worked really well once I understood the background-position values.

  14. Hi will this work on responsive websites

    a {
    display: block;
    background: url(sprite.png) no-repeat;
    height: 30px;
    width: 250px;
    }

    because of the fixed width and height, will it be possible to make it responsive..?

    Thank you,
    chadtulio

  15. muns88
    Permalink to comment#

    There has to be some way of using “%” vs “px” to make it work. As long as your background image scaled well.

  16. Permalink to comment#

    Would be better if i could see an example right away!

  17. Barry
    Permalink to comment#

    Sprite images can be as large as you want….So if the image is 50px x 50px your image canvas can be 1000px x 1000px if you like…So if you want loads of white space around the image area that is OK…I often separate each image by 100px blocks purely for maths purposes and to avoid any unforeseen chances that the other images on that png file may show…

    For example if I have a menu and each item is 250px wide x 40px high, then i make sure that each image in the sprite are not within 300px of each other if they are side by side…

  18. Jason
    Permalink to comment#

    You all can see an example of a sprite right away. It’s called “Google”. Just sayin.

  19. surender sharma
    Permalink to comment#

    its work like a masking just show the image according to ur tag width and height if u hav a image file with height 100px and width 100px and ur social icon size is 30px 30px then u can write code like this
    a{
    dispaly:block;
    background:url(images/sprite_file.jpg) left top no-repeat;
    height:30px; /*according to icon height size/
    width:30px; /*according to icon width size
    /
    text-indent:-9999999px;
    }
    a:hover{
    background:url(images/sprite_file.jpg) left bottom no-repeat;
    }

    in hover u can see ur file bottom part in 30px by 30px mask area so make sure in bottom of the prite.jpg ur hover image“

  20. Deborah
    Permalink to comment#

    Does this only work on hover or can I set it for visited?

  21. surender sharma (Dimple)
    Permalink to comment#

    Deborah Good question 90% i m sure that it’s only work only on hover and for 10% i will try

  22. Permalink to comment#

    CSS Sprites has been around a very long time. Read a far more interesting and educational article about on alistpart.com :

    http://alistapart.com/article/sprites

    // I hold no affiliation with AListApart; It is just a component of CSS I am most passionate about – I was writing code and creating graphics for this style of menus back in the early 00’s.

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".