Click here to Skip to main content
15,891,136 members
Articles / Web Development / HTML

How to create an ALT text with multiple lines and line breaks

Rate me:
Please Sign up or sign in to vote.
2.50/5 (4 votes)
18 Jan 2007CPOL2 min read 58.2K   7   7
Ever wondered if you can use the ALT tag in HTML for more that just a vague description?

Introduction

This is just a quick tutorial on how to create multiple lines in an ALT text field. I had the issue of a client wanting to show a biography page with loads of images, and when a user hovers over an image, the ALT tag must show the name, occupation, and company name.

Of course, the nice way of doing this is with a DHTML popup or something similar, but due to time and budget restrictions, a differrnt method had to be found.....step forward the alt text.

Remember!!!!

The alternative text field is not there for this use actually!! The two main uses are to first show some text if the image couldn't load so the visitor can at least know what the image was about, and secondly for Google search engines, of course!!

Don't over estimate the value of the ALT text field as it is not a really big thing, and try to avoid it than use it for the use I am showing here.

Downside

As always, there is a downside. I haven't had too much time to test everything about this, and couldn't find much information on the internet about this (although a thorough search might prove me wrong), but from my testing, this only works in IE version 5 and higher and some versions of Mozilla. Couldn't get it to work on Netscape and didn't have time to test in Opera, but if I had to take a guess, it doesn't work there!! So be careful.

Code

As always, you can do it in a programming language like C#, I am only going to explain the HTML version. Adjust the code to fit your style:

We want an image to have multiple ALT text. The ALT text should contain the name, company, and date for illustration purposes. Remember that the ALT text field doesn't take any HTML code!! So no use trying to input <br> tags in there. Trust me in that /n will not work either!! The only thing that I can find that works in this case is HTML character codes. I know, I know, I can hear you say duh... well, I wasn't sure about it, so I thought I'll just say it!!

The HTML character code for line feed is &#10 but carriage return &#13 will also work, so we input that in the text we want in the ALT.

And there we go, a quick simple example of how to increase the use of the ALT tag!!

Conclusion

I can't overstate that this is not the ideal way, but it can serve as an extra way to save some time or even cut down on budget prices!! Also, all other HTML character codes should work, but again, I haven't tested this, so go ahead and play around!!

Let me know what you think.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
South Africa South Africa
I have been programming for 5 years, and doing web and software design for 7 years. I worked for a blue chip company in the UK working with the web interface of Software Security.

Comments and Discussions

 
General'Page Breaks' Pin
Ashaman18-Jan-07 7:18
Ashaman18-Jan-07 7:18 
GeneralRe: 'Page Breaks' Pin
Miela_SA18-Jan-07 20:35
Miela_SA18-Jan-07 20:35 
GeneralDoes not work in FireFox Pin
Shawn Poulson18-Jan-07 6:03
Shawn Poulson18-Jan-07 6:03 
For me, FireFox 2.0 does not show ALT text as tooltips on image mouseover. There doesn't appear to be any config settings in Tools->Options or about:config to enable this.

---
Shawn Poulson
spoulson@explodingcoder.com

GeneralRe: Does not work in FireFox Pin
Gabriel Topala18-Jan-07 6:32
Gabriel Topala18-Jan-07 6:32 
GeneralRe: Does not work in FireFox Pin
Miela_SA18-Jan-07 20:34
Miela_SA18-Jan-07 20:34 
GeneralRe: Does not work in FireFox Pin
Shawn Poulson22-Jan-07 6:22
Shawn Poulson22-Jan-07 6:22 
QuestionRe: Does not work in FireFox Pin
Matt Keeble1-Mar-07 2:39
Matt Keeble1-Mar-07 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.