cssimage-replacement

Link with background image


I have some 10 buttons with image-text and hover states for each. What I want to do is use background-position, width and height to only show the part of the background image sprite and hover background-position to show the hover style. I'll also use an image replacement class on the element so that it remains accessible and indexable.

So (measurements are random):

[CSS]

.menu{background-image:url(path/to/sprite.png);}
.button-1{width:200px;height:30px;background-position:0 0;}
.button-1:hover{background-position:0 -30px;}
.button-2{width:250px;height:30px;background-position:100px 0;}
.button-2:hover{background-position:100px -30px;}
/* Image Replacement Class (H5BP, @necolas && BEM) */
.ir{border:0;font:0/0 a;text-shadow:none;color:transparent;background-color:transparent;}

[HTML]

<a href="someLink.html" class="menu button-1 ir">Button 1</a>
<a href="someOtherLink.html" class="menu button-2 ir">Button 2</a>

What I want to know is if that is a good way of doing this or should it be done differently, like:

<a href="someLink.html"><img src="image.png" width="200" height="30" alt="Button 1"/></a>

Then with JavaScript swap the image on hover.

Is there any difference between the two in terms of accessibility and robots?

Thank you.


Solution

  • What you are doing is correct. Do not forget to add a display: block; to that link. Something else you can do is putting the actual link text in a span and then positioning that span absolute out of the screen. Like so:

    <a href="" title="Test link"><span>Home page</span></a>
    

    And in your css file:

    a > span {position: absolute; top: -10000px;}