开发者

Should images be referenced from CSS or from Content folders with <img/>

I ask because my buddy posted a question earlier on How to resolve issue with image path when testing Html开发者_如何学GoHelper? 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?


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).


The tag is the best way.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜