When to use IMG vs. CSS background-image?
Proper uses of IMG
- Use
IMG
if you intend to have people print your page and you want the image to be included by default. —JayTee - Use
IMG
(withalt
text) 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.
Pragmatic uses of IMG
- Use
IMG
plus alt attribute if the image is part of the content such as a logo or diagram or person (real person, not stock photo people). —sanchothefat - Use
IMG
if you rely on browser scaling to render an image in proportion to text size. - Use
IMG
for multiple overlay images in IE6. UseIMG
with az-index
in order to stretch a background image to fill its entire window.
Note, this is no longer true with CSS3 background-size; see #6 below.- Using
img
instead ofbackground-image
can dramatically improve performance of animations over a background.
When to use CSS background-image
- Use CSS background images if the image is not part of the content. —sanchothefat
- Use CSS background images when doing image-replacement of text eg. paragraphs/headers. —sanchothefat
- Use
background-image
if you intend to have people print your page and you do not want the image to be included by default. —JayTee - Use
background-image
if you need to improve download times, as with CSS sprites. - Use
background-image
if you need for only a portion of the image to be visible, as with CSS sprites. - Use
background-image
withbackground-size:cover
in order to stretch a background image to fill its entire window.
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.
I'm surprised no one's mentioned this yet: CSS transitions.
You can natively transition a div
's background image:
#some_div {
background-image:url(image_1.jpg);
-webkit-transition:background-image 0.5s;
/* Other vendor-prefixed transition properties */
transition:background-image 0.5s;
}
#some_div:hover {
background-image:url(image_2.jpg);
}
This saves any kind of JavaScript or jQuery animation to fade an <img/>
's src
.
More information about transitions on MDN.
Above answers considers only Design aspect . I am listing it in SEO aspects.
When to use <img />
- When Your Image need to be indexed by search engine
- If it has relation to content, including cards (click areas), but not related to design. Design is probably the most difficult thing to parse here because so it's all design right. I would say perhaps functional design (Cards, thumbnails, profile images, things you can click) vs Aesthetic design which is mostly used for sites appeal.
- List item
- If your image is not too small ( not iconic images ).
- Images where you can add
alt
andtitle
attribute. - Images from a webpage which you want to print using print media css
When to use CSS background-image
- Images Purely Used to Design.
- No Relation With Content.
- Small Images which we can play with CSS3.
- Repeating Images ( In blog author icon , date icon will be repeated for each article etc.,).
As i will use them based on these reasons. These are Good practices of Search Engine Optimization of Images.