We help IT Professionals succeed at work.

displaying html markup under html alt of img

jazzIIIlove asked

I want to display a simple html markup (a div with some content of <p>) on image hover and alt seems not working properly. Any way to achieve this?

Note that i need to write the alt content in backend of .NET

 string rawimage =
                                                    "<img data-index='{0}' src='images/category{0}.png' alt='{1}' style='{2}' />",
                                                    "1", myPopupText, "margin-right:4px");

Open in new window

Any way? Or a way with less pain?
Watch Question

Brandon LyonWeb Developer/Designer
That is not what an alt tag is for and it wouldn't work. If you need hover events on the tag you have two options. You can append HTML using CSS content or you can do it using javascript.

The alt tag is for using text only to convey what the image is about. If people can't see the image then they need to know what was there. This is valid for search engines, people with accessibility issues, and when browser/server issues prevent viewing the file.
"title" is the attribute you're looking for, that will add a tooltip to the image when a user hovers over it.
Note that you can't include markup, just simple text.

And FYI, the article above on appending html is actually wrong.  before and after pseudo elements don't actually append before or after the tag itself, they append before or after the content inside the tag.

So elements like img that can't have child elements, before and after will do nothing on most browsers.
Brandon LyonWeb Developer/Designer
Ah yes, you're correct, I forgot that certain tags such as IMG will not render CSS before and after content.
Would make life SO much easier if they would ;)