62

I understand how to use sprites, however, isn't a "src" attribute required for IMG tags? I could always use a SPAN or other tag and set the background/width/etc but it won't be semantically correct.

Basically, I'd like to omit the SRC for an IMG tag and use just sprites, but am concerned about the HTML not being technically valid because of it. Thanks.

kapa
  • 77,694
  • 21
  • 158
  • 175
Ryan Peters
  • 7,608
  • 8
  • 41
  • 57

6 Answers6

62

Using sprites doesn't necessarily mean you need to define them in css backgrounds. You can also use IMG tag sprites, to do so you need basically trim your image. There are two good articles explaining that technique:

http://tjkdesign.com/articles/how-to_use_sprites_with_my_Image_Replacement_technique.asp

http://www.artzstudio.com/2010/04/img-sprites-high-contrast/

Both CSS and IMG methods sure have their own benefits, so you need to figure out which one suits you better.

wdev
  • 2,190
  • 20
  • 26
  • 1
    I know this is an old question, but i just really want to thank you for these two articles, both are great, and really gave me what i was looking for. Thank you and i hope this question doesn't bump because of me. – Tio Felix Nov 28 '11 at 20:03
  • 3
    both links seem to be dead – Shanimal Jul 03 '19 at 02:59
  • 5
    Here are two archived snapshots of those: * https://web.archive.org/web/20170212063839/http://www.cssmojo.com/how-to_use_sprites_with_my_image_replacement_technique/ * https://web.archive.org/web/20130726060548/http://www.artzstudio.com/2010/04/img-sprites-high-contrast/ – Bret Jul 05 '19 at 11:53
58

About semantical correctness:

When an image has semantical meaning, so it is considered to be content, use an IMG tag, without sprites, and a correctly set up ALT.

When an image is just decoration, like the background of a box, background of a button, background of a menu option, etc., it has no semantical meaning, so you can just use it as a background of a SPAN, DIV, etc. from CSS. You can use sprites in this case.

kapa
  • 77,694
  • 21
  • 158
  • 175
  • 2
    Is it possible to use sprites inside img tags? That way we could have the speed of sprites and the `alt` attribute. I have a lot of small buttons, so they are not decorative (must use img?) but waste too much time being individually loaded (must use sprite?). What a dilemma! – dialex Aug 10 '11 at 14:33
  • 5
    @DiAlex If you have small buttons, use the `button` or `input` tag with a background image. – kapa Aug 12 '11 at 12:24
  • 3
    @DiAlex Refer to the [HTML5 spec](http://dev.w3.org/html5/spec/Overview.html), you may find some interesting tags to use. – kapa Aug 12 '11 at 17:56
7

I use a 1x1 transparent gif (so called spacer) for the src. then set the background image for that img tag with the corresponding bg position. this way you're utilizing the speed of sprites and keeping the semantic of your code (you can still use the alt attribute)

  • I'm just converting my own site from to
    image sprites for this reason - not having to use a transparent.png image for the image src mean fewer HTTP requests. Who says I'm pedantic...?
    – Richard Apr 25 '15 at 22:39
  • I do it that way as well, but that's an extra image that causes an HTML request; and the number of HTML requests is exactly what we're trying to minimize by using sprites. That's why I don't want to have a "spacer" in the image. – Davide Andrea Mar 13 '18 at 18:06
  • @DavideAndrea: To avoid HTTP requests, you can use a data URI for the image; see https://stackoverflow.com/a/13139830. – wchargin Aug 14 '19 at 19:10
5

I could always use a SPAN or other tag and set the background/width/etc but it won't be semantically correct

Actually there is nothing wrong about using CSS to set the background of a span or div. It would actually be incorrect syntactically to omit the src from an image, as that is the whole point of the tag. There is nothing in the standards saying you have to put text inside a span. Syntactically speaking, modifying the background on an element would be the most "correct" way to do it.

Here is the ref on img tags over at W3C: http://www.w3.org/TR/html401/struct/objects.html#h-13.2

And a little extra reading: http://www.w3.org/TR/html4/struct/global.html#h-7.5.3

These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes.

Richard Key
  • 103
  • 5
3

You can either use CSS backgrounds, or HTML Canvas elements to dynamically draw upon. With canvas's you have the ability to easily subset images and perform blend mode effects.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
1

You solve this by re-thinking your options.

You create a defined area with a <a> with display:block; or <div> and use overflow hidden; to hide overflow and position:relative;.

Then you place your <img> image sprite inside absolutely positioned, which is possible since you positioned the parent.

Then use :hover on the image to change position.

Now your sprite is based on an img tag, so you can use your alt text.

Following example is based on a Facebook sprite with two versions of the icon on top of each other, each 50px by 50px, total height of image being 100px:

<!DOCTYPE html>
<html>
<head>
<style>
.icon {
    display:block;
    position:relative;
    width:50px;
    height:50px;
    border:1px solid red;
    overflow:hidden;
}
#fb {
    position:absolute;
    top:0;
    left:0;
}
#fb:hover {
    position:absolute;
    top:-50px;
    left:0;
}
</style>
</head>

<body>
<a href="https://facebook.com" class="icon" title="Facebook">
<img src="sprite-facebook.png" id="fb" width="50" height="100" alt="Facebook">
</a>
</body>
</html>
Paul
  • 1,624
  • 5
  • 18
  • 24