tags:

views:

69

answers:

3

I ask because my buddy posted a question earlier on How to resolve issue with image path when testing HtmlHelper? and a few of us in the office got talking about how to resolve this.

One of the guys suggested that he wouldn't have this issue (Test crashing because it has a dependency on functionality in IIS) if he was referencing the image from CSS.

His point made perfect sense, but it kind of threw us back because we'd always been referencing images with <img/>. Were we doing it wrong all along? None of us are experts in this area so I thought it was worth putting to the community. What's the best way to reference images?

A: 

The tag is the best way.

It's standards compliant, accessible, and SEO friendly.

TALLBOY
...but in many situations, decorative images don't belong in `img` elements. See e.g. headers and such.
You
+7  A: 

There are a variety of benefits to using CSS background-image and inline <img /> tags. CSS background images are great for decorative elements (content borders, background patterns, etc.) as they can be repeated with background-repeat. They make a poor choice for content-centric images, though, like product photographs, bio avatars, etc. Here, inline images work best because you can use an ALT attribute, and images are displayed regardless of browser (some older mobile browsers don't support backgrounds in CSS).

David Kaneda
good points - thanks. What about an image that would depict sortability of a table column? That's the kind of image my buddy was having difficulty with. Should that be css or `<img />`?
DaveDev
If the control is AJAX/Javascript-based, I would say do whatever's easier (as accessibility and SEO are not a concern). If hitting the sort button performs a traditional GET request on the page, I would try to get it in as a proper image. Let me know if that distinction makes sense-
David Kaneda
+1 Very well put.
Pekka
A: 

Indiscriminately putting all images into CSS background-image properties is a terrible idea in my opinion. Just a few things you deprive yourself of:

  • You can't stretch and resize images (may sometimes be necessary)
  • You can't use the ALT attribute (as David already says)
  • Background images usually won't get printed
  • they won't get indexed by search engines
  • they are difficult for the user to store
  • missing images won't show up with the "broken image" icon in IE
  • bad browser layout engines may have problems zooming, because the surrounding container could get zoomed apart from the image content (Shouldn't be a problem in FF though)
  • you always need an additional container to put the background-image in so you can't e.g. do DOM operations with image elements

the <img> tag exists for a reason, and should absolutely be used where appropriate (i.e. where the image is part of the content rather than decoration or background). For the "sort icon" thing I second what David says, it's fine both ways, but if it's a clickable element it should be an img.

Pekka