Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding "alt" to image with css 1

Status
Not open for further replies.

technoknow

Technical User
Apr 19, 2002
121
US
How do you add the "alt" text to an image if you are using:

#MainText { background: url(images/MainText.gif) no-repeat; }

in your css, and:

<div id="MainTxtTop"></div>

in the HTML?

Thanks,
technoknow
 
What you're using is not an image. It is a background image for a box (div). Depends why you need the alt attribute. If you need it to let the browsers that cannot display pictures know what it is, then you cannot. If you need it for the tooltip (which is what many people believe it is for) you can use title attribute:
Code:
#MainText { background: url(images/MainText.gif) no-repeat; }

<div id="MainTxtTop" title="This be Main Text Top"></div>
 
Yes, I'm using the alt tag for the browsers that can't display images.
It seems like I remember that there was is way to use the image once in the html with the alt tag and then reuse it with css? No?
Thanks,
technoknow
 
Nope. Alt tag refers to image content (describes what image is about) and as such has nothing to do with presentation (which CSS is for).
 
You could add the "alt" text inside a tag within your div, and use css to hide it:

Code:
#MainText { background: url(images/MainText.gif) no-repeat; }
#MainText span { visibility: hidden; }

<div id="MainTxtTop"><span>My Alt Text</span></div>

It's not quite what you're looking for. It shows the alt text when the browser doesn't support css, not images. So, if a browser supported images but not css, you'd see text. If a browser supported css but not images, you'd see nothing. Probably.
 
Thanks blueark, I'll give that a try.
Tech


Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint and curious volume of forgotten code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top