Let us see by comparison.
It isn't enough to simply supply a background-image
property, because the img
element has no inherent height
or width
and the background-image
property can't provide that automatically in the same way that setting the src
attribute does. So, without declared dimensions, the CSS-applied image doesn't show.
#my-pic {
background-image: url(//placehold.it/150);
}
<img src="//placehold.it/150" />
<img id="my-pic" src="" />
You can force the img
tag to have width
and height
equal to that of the background-image
, if you want it to behave similarly to a src
-applied image. Note: In some browsers, a broken image placeholder icon will be displayed on top of the background-image
.
#my-pic {
display: inline-block;
width: 150px;
height: 150px;
background-image: url(//placehold.it/150);
}
<img src="//placehold.it/150" />
<img id="my-pic" src="" />
But this has limitations that the native img
and src
attribute combination don't have, such as the ability to define just the height
or width
and retain natural constraints.
#my-pic {
display: inline-block;
width: 150px;
background-image: url(//placehold.it/300);
}
<img src="//placehold.it/300" width="150" />
<img id="my-pic" src="" />
Also, setting the width
and height
to anything other than natural dimensions, will just crop the background-image
, instead of force-constraining it the way img
behaves natively.
#my-pic {
display: inline-block;
width: 300px;
height: 150px;
background-image: url(//placehold.it/300);
}
<img src="//placehold.it/300" width="300" height="150" />
<img id="my-pic" src="" />
There are other properties you can use to achieve various results, such as background-attachment
, background-clip
, background-origin
, background-position
, background-repeat
, and background-size
.
But if you're employing these styles, you probably shouldn't use an img
tag as your container; a div
or some other type of containing element with semantic meaning would be better suited for the job.
Regarding alt
text, if an image that is defined in the src
attribute fails to load, the value of alt
attribute is shown as expected; whereas an alt
attribute given to the CSS-applied image is displayed at all times and adjusts the alignment of the inline-block element based on the text baseline.
#my-pic {
display: inline-block;
width: 150px;
height: 150px;
background-image: url(http://placehold.it/150);
}
#my-pic-failed {
display: inline-block;
width: 150px;
height: 150px;
background-image: url(//failedtoload.png);
}
<img src="http://placehold.it/150" alt="Alternate text"/>
<img src="//failedtoload.png" alt="Alternate text"/>
<img id="my-pic" src="" alt="Alternate text" />
<img id="my-pic-failed" src="" alt="Alternate text" />