Tuesday, March 19, 2024

How To Use Text Over Images with HTML

Now and again I wonder if people are reading all of the stuff I write. When I put up the last FAQ page, I answered a question from a reader about putting text over an image. She wanted to be able to put the name of the person in the picture on top of the picture, like a caption. I said that it couldn’t be done without the use of a graphics editor. Well — KaBoom! The e-mail poured in.

Yes, people are reading. I will take the weasel road right now and say that when I answered the question, it was correct. Now the variables have changed out right from under me and the Version 4.0 browsers have offered a couple of different ways to do it. Here I’ll outline three.

The Easiest Way I Know To Put Text Over an Image

Here you go, text over a stunning image of yours truly:

 

  Joe Burns at Work

Like the new haircut? Most of the top is gone as well as most of the beard. I thought about getting a new character for the home page, but I still like the old one.

Before we get into the how’s and why’s of this method, I wanted to let you know that the images on this page were taken using my new digital camera. It’s really an amazing item. It holds a 3.5 inch floppy disc right in the body of the camera itself. When you snap a picture, it writes it to the disc in JPEG format so you can go right to the Net. Not only that, but you can also view the images as soon as you take them. That way you can quickly tell if someone blinked her or his eyes or not. If it’s a bad shot, erase it and take another one. Smooth, but a little steep. Mine was just over $500. (My wife gasped as I handed over the credit card.) There are cheaper ones, but not with the disc right on board. I still think it was a good buy. Now…

The HTML Code That Did It

<TABLE BORDER=”0″ cellpadding=”0″ CELLSPACING=”0″>
<TR>

<TD WIDTH=”221″ HEIGHT=”300″ BACKGROUND=”newjoe01.jpg” VALIGN=”bottom”>

<FONT SIZE=”+1″ COLOR=”yellow”>Joe Burns at Work</FONT></TD>

</TR>
</TABLE>

Here’s What’s Happening Within the HTML
 

I got the effect using a single table cell, adding a background, and then some text. Like so:

  • <TABLE BORDER=”0″ cellpadding=”0″ CELLSPACING=”0″> This is the format for the cell. You need to set everything to zero so that the cell borders lay right against the image. That way you have better control over the text in relation to the image.

     

  • <TR> This starts the table row. It’s not really needed but it’s good table format.

     

  • <TD WIDTH=”221″ HEIGHT=”300″ BACKGROUND=”newjoe01.jpg” VALIGN=”bottom”> This is what does the trick. I set the image you see above as the background of the image cell. Please note that I added the height and width of the image. YOU NEED TO DO THAT. If you don’t, then the cell will only conform to the size of the text you put after the TD command. In other words, you won’t see the entire picture.

     

  • <FONT SIZE=”+1″ COLOR=”yellow”>Joe Burns at Work</FONT></TD> This is the text that will appear on the image. I used a FONT size and color command to get the text to show up a little better.

     

  • </TR> Just good table creation again.

     

  • </TABLE> That ends the whole deal.

Doing It Through Layers

Although that table example is good enough, we at Goodies Inc. go further and show you a couple of other methods. Here’s a bit of code that will perform pretty much the exact same thing. It’s done with layers so you have to be running Netscape 4.0 to see the effects.

 

<LAYER LEFT=250 TOP=500>
<IMG SRC=newjoe02.gif>
</LAYER>

<LAYER LEFT=250 TOP=500>
<IMG SRC=overtext.gif>
</LAYER>

 

See The Commands In Action

Pretty nifty. That’s my Paul McCartney, Sergeant Pepper’s pose, if you didn’t know.

As you can see, the effect is the same, but the parts are a little different. Look at the code. I set up two layers following the basic format (found here), but this time I did it with two images. One is of the back of my head (newjoe02.jpg) and the other is an image of the same size with the text “That’s not a Bald Spot” written on it. Both are set to start 250 pixels from the left and 100 pixels from the top of the browser’s window.

Remember that layers lay one over the top of the other in the order they are written. That’s why the text image is written second.

The trick is that I made the image with the text transparent, except for the text itself. That way the other image shows through and you get the effect.

It’s a bit of work, but if you are chopping up your pages using layers and DIV sections, this is the way to go about setting up the effect.

Why Not Position the Text?

Capital idea!

The idea is basically the same as with layers, except you don’t have to create the text image like you did above. Here’s the code:

 

<IMG SRC=”newjoe03.jpg”>

<DIV STYLE=”position:absolute; top:250px; left:20px; width:200px; height:25px”>
<CENTER><FONT SIZE=”+2″ COLOR=”00ff00″>Looking Into The Future</FONT></CENTER>
</DIV>

(That DIV section should really be all on one line)

 

See It In Action

Looking into my computer screen actually. Bad shot. It looks like I should have a county jail number card underneath my face. (Turn to the right! (Yes, I see I’ve turned to the left.))

The concept of positioning is explained in mind-numbing detail here, but you can just about figure it out from the code above. The image is just sitting where it would normally sit. The text, formatted to +2 font size and green color, has been placed in a division. Style Sheet commands then place it 250 pixels from the top of the browser window and 20 pixels from the left side. The height and width command set the height and width of the division the text will sit inside. You shouldn’t set that any wider than the image. That way you’ll lessen your chance of rolling the text over the sides.

That’s That…

Most of you will probably end up using the TABLE background method up top, but maybe not. So now you can set your images with as many words of wisdom as you can muster. I haven’t any at the moment.

 

Enjoy!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured