views:

13425

answers:

13
A: 

Do you need the image to take up space or do you want to write over it?

geowa4
+37  A: 

It's a black and white decision to me. If the image is part of the content such as a logo or diagram or person (real person, not stock photo people) then use the <img /> tag plus alt attribute. For everything else there's CSS background images.

The other time to use CSS background images is when doing image-replacement of text eg. paragraphs/headers.

sanchothefat
Exactly, if it's not part of the content there is no reason to use an <img> tag
Birk
Excellent case! CON--Use background-image when doing image-replacement of text.
system PAUSE
Yeah, it's never ideal to do image replacement but some designs just won't be right until you do.
sanchothefat
+1 agreed. Note that image replacement of text falls in the same "not part of the content", since the content is the actual text.
eglasius
Doesn't a background-image have to be attached to something? You have to add some content to add a background to it and at the moment you can only have 1 background per content element...
Adrian
+8  A: 

Browsers aren't always set to print background images by default; if you intend to have people print your page :)

JayTee
Sounds like: PRO--Use IMG if you want the image to print by default. CON--Use background-image if you don't want the image to print by default. Nice one!
system PAUSE
A: 

Here's a technical consideration: will the image be generated dynamically? It tends to be a lot easier to generate the <img> tag in HTML than to try to dynamically edit a CSS property.

Bryan M.
And what about inline styles? This question really must not decided by this idea.
Török Gábor
maybe I should phrase is this way: would you rather work with a DOM element or an element attribute?
Bryan M.
+2  A: 

Use CSS background-image in a case of multiple skins or versions of design. Javascript can be used to dynamically change a class of an element, which will force it to render a different image. With an IMG tag, it may be more tricky.

MK_Dev
you can dynamically change the src attribute of an image tag too, just as easy as changing a class
sanchothefat
@sanchothefat, true, however, in this case image source would need to be kept in JS instead of CSS. IMO css file would be more appropriate to keep file name.
MK_Dev
+5  A: 

If you have your CSS in an external file, then it's often convenient to display an image that's used frequently across the site (such as a header image) as a background image, because then you have the flexibility to change the image later.

For example, say you have the following HTML:

<div id="headerImage"></div>

...and CSS:

#headerImage {
    width: 200px;
    height: 100px;
    background: url(Images/headerImage.png) no-repeat;
}

A few days later, you change the location of the image. All you have to do is update the CSS:

#headerImage {
    width: 200px;
    height: 100px;
    background: url(../resources/images/headerImage.png) no-repeat;
}

Otherwise, you'd have to update the src attribute of the appropriate <img> tag in every HTML file (assuming you're not using a server-side scripting language or CMS to automate the process).

Also background images are useful if you don't want the user to be able to save the image (although I haven't ever needed to do this).

Steve

Steve Harrison
+1  A: 

About the same as sanchothefat's anwser, but from a different aspect. I always ask myself: if I may remove completely the stylesheets from the website, the remaining elements do only belong to the content? If so, I did my job well.

Török Gábor
A: 

What about the size of the image? If I use the img tag, the browser scales the image. If I use css background, the browser just cuts a chunk from the larger image.

+2  A: 

Foreground = img.

Background = CSS background.

ck
+12  A: 
system PAUSE
A: 

Just to throw a spanner in the works - i'm of the opinion that you should never use the img tag. HTML was meant for content, not visual style. all the images on your page should come from the CSS, leaving your HTML code pure. (even if it does take a bit longer to build)

Big Coops
I agree with you with regard to images that serve as page decorations, but surely some images are content: photographs in a news article, or diagrams in an academic article, etc. Those are part of the content, and probably warrant an IMG tag.
benzado
A: 

well....

Using a background image, you need to absolutely specify the dimensions. This can be a significant problem if you don't actually know them in advance or cannot determine them.

A big problem with <img> is overlays. What if I want an css inner shadow on my image (box-shadow:inset 0 0 5px rbg(0,0,0,.5))? In this case, since can't have child elements, you need to use positioning and add empty elements which equates to useless markup.

In conclusion, it's quite situational.

Mat
+1  A: 

Use background images only when necessary e.g. containers with image that tiles.

One of the major PROS by using IMAGES is that it is better for SEO.

Marc Uberstein