Before I begin, I read this very useful question which lays out what's the pros and cons of using <img>
's and background-images
. When to use IMG vs CSS
After reading that, however I've been thinking about what's the most efficient & semantic way of displaying an icon. The other question contended that if there's semantic meaning to it such as a warning icon, then it should be displayed as an <img>
which I think makes sense in most cases. However, what about the other times?
Here's the only other ways I've seen icons presented:
<i>
tag, Google uses the <span>
tag)Using the extra padding method seems to work alright in some cases, but you might run into the problem of exposing other icons in your sprite map if the dimensions of the element don't match up with your icon.
I'm guessing the separate element icon method is probably the most fail-safe way of doing it, but I'm not 100% certain if it's always the best way to do it and what element to use (i, span, other?)
What's your thoughts?
It depends on the circumstance, if the icon is actually designed to give emphasis to some text, then you'd include it in the tag, i.e.
<em>Warning!</em>
And you would add CSS styles to display the icon as the background-image
. Technically speaking, you should only be using the <img>
tag for items which are semantically images, such as a photograph or diagram. Otherwise for things like headings or icons, stick with changing the background-image
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With