
- Image via Wikipedia
Including an image on an HTML page can be done using either the HTML img tag or using the CSS background-image property. Having understood that, the next question is when to use which of these two methods.
From a semantics point of view we can say that the img tag should be used whenever the image is directly related to the content being explained on the page. Thus if I am showing a cooking recipe and want to display a photo of the finished plate, that would be directly related to the content and thus an tag would be in order. If on the other hand I want to display an image as part of the site’s design or for decoration purposes, it would be ideal to use the CSS background-image property.
Some other considerations to keep in mind are outlined in the following list of recommended uses for each method.
HTML img tag:
- Use
IMGif you intend to have people print your page and you want the image to be included by default. - Use
IMG(withalttext) when the image has an important semantic meaning, such as a warning icon. This ensures that the meaning of the image can be communicated in all user-agents, including screen readers. - Use
IMGif you rely on browser scaling to render an image in proportion to text size. - Use
IMGfor multiple overlay images in IE6. - Use
IMGwith az-indexin order to stretch a background image to fill its entire window. - Using
imginstead ofbackground-imagecan dramatically improve performance of animations over a background.
CSS background-image property:
- Use CSS background images if the image is not part of the content.
- Use CSS background images when doing image-replacement of text eg. paragraphs/headers.
- Use
background-imageif you intend to have people print your page and you do not want the image to be included by default. - Use
background-imageif you need to improve download times, as with CSS sprites. - Use
background-imageif you need for only a portion of the image to be visible, as with CSS sprites.

Related posts:

Web professional in Malta, Europe. Focusing on building visually stunning websites that are easy to maintain, usually using WordPress as the CMS. Web developing since 1995, loving WordPress for more than 5 years.
One Comment
Got something to say? Feel free, I want to hear from you! Leave a Comment
Trackbacks for this post