Is it possible to use CSS sprites for "foreground" images -- i.e. images that users are supposed to click on and interact with and maybe even print?
Instead of using the CSS background-image
property. What would you use?
Is it possible to use CSS sprites for "foreground" images -- i.e. images that users are supposed to click on and interact with and maybe even print?
Instead of using the CSS background-image
property. What would you use?
One primary requirement that cannot be handled by background images is for ARIA. All ARIA requirements will reject the use of background images for meaningful, navigational, and other 'informative' uses that a screen reader must interpret on behalf of a user with a disability. Being able to swap out a background image css statement for an img tag and some ARIA tagging whenever necessary is a critical feature in the current regulated development environment.
The answer to the original question is yes! It is possible to use the image that is displayed in a css background statement. But you must open the sprite image in an image editor and select out the portion that represents the sprite you want and save it as a separate image and reference it in an img tag.
The challenge is that often, these situations arise in a pre-built control library. Finding and altering the code in the library that selects and displays the background image is a little difficult, changing out the code is hard!
You can do this with less CSS like this:
Whatever has the class
class="myClass"
will have just the image in it, nothing else. This can be a<a>
an<input>
or a normal<div>
, whatever you want.It's a background image...but it's the element you're looking at, nothing's in front of that background. Just because you're using
background-image
for the property doesn't mean it's not a foreground element...like a button you can click on. You can do that like this:I have solved this problem using img tags and using the object-fit and object-position properties in my css. Here's a sample of the html and css I used:-
HTML
CSS
Obviously, you need to change the position and the size parameters according to the sprite you are using. For a full working example, check out this fiddle
You can use a standard
<img />
tag and put it in a container (like a<div />
) with a limited height/width. Then use relative positioning or negative margins to control the position of the image.You can do this, but you have to use background images for sprites as you have to be able to set position. This can be used for links or whatever you want. Look at Googles sprite, they use it for there buttons on iGoogle: http://img0.gmodules.com/ig/images/v2/sprite0_classic.gif