Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sprites with IMG tag?

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.

like image 336
Ryan Peters Avatar asked Dec 02 '10 14:12

Ryan Peters


People also ask

How do I use an image as a sprite tag?

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.

Should I use image sprites?

A web page with many images can take a long time to load and generates multiple server requests. Using image sprites will reduce the number of server requests and save bandwidth.

Does HTML IMG work with PNG?

The HTML <img> element is found within the <body> tag. The <img> tag has 2 required attributes - src and alt. The <img> tag may support (depending on the browser) the following image formats: jpeg, gif, png, apng, svg, bmp, bmp ico, png ico.

How do I make an image a sprite?

CSS Image Sprite is a combined image file of all pictures in a document page. Image sprites come is useful as image resources will have to be loaded only once. Using the CSS background-position different parts of the combined image can be shown.


1 Answers

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.

like image 128
wdev Avatar answered Nov 03 '22 00:11

wdev