Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Placing Text Over Images

Now and again I wonder if people are reading all the stuff I write. When I put up the last FAQ page at HTML Goodies, 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 email poured in.

You can find thistutorial, and all of its examples, online at http://www.htmlgoodies.com/beyond/textonimages.html.
You can download just the examples at http://www.htmlgoodies.com/wpg/.

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 right out from under me, and the version 4.0 browsers have offered a few different ways to do it. In this section, I'll outline three.

The Easiest Way I Know

Here you go. Figure 3.1 shows text over a stunning image of yours truly.

Figure 3.01Figure 3.1 Hey, he's cute!

Like the 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. Besides, as of the writing of this book, I'm back to a full head of hair and full beard. I go for the "mountain man" look once every couple of years.

Here is the code that created the caption:

<TABLE BORDER="0" cellpadding="0" CELLSPACING="0">
<TD WIDTH="221" HEIGHT="300" BACKGROUND="newjoe01.jpg" VALIGN="bottom">
<FONT SIZE="+1" COLOR="yellow">Joe Burns at Work</FONT></TD>

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.

  • <TD WIDTH="221" HEIGHT="300" BACKGROUND="newjoe01.jpg" VALIGN="bottom">—This is what does the trick. I set the image in Figure 3.1 as the background of the image cell. Note that I added the height and width of the image. You need to do that. If you don't, 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 appears on the image. I used a FONT size and color command to get the text to show up a little better.

  • </TR>—This ends the table row.

  • </TABLE>—This 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 performs 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 LEFT=250 TOP=500>
<IMG SRC=overtext.gif>

See the commands in action in Figure 3.2.

Pretty nifty! That's my Paul McCartney, Sergeant Pepper pose—in case 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, 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 Is 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.

Figure 3.2Figure 3.2 Hey, he's... going bald.

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?

Positioning the text is a capital idea. We're going to use the positioning commands available to Internet Explorer browsers to position the text and image so that one lays over the top of the other.

The idea is basically the same as with layers, except that you don't have to create the text image like you did previously. Figure 3.3 shows the effect.

Figure 3.3Figure 3.3 Hey, he's ignoring me.

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>

That DIV section should really be all on one line.

I'm looking in to my computer screen actually. It's a bad shot. It looks like I should have a county jail number card underneath my face.

The image is just sitting where it would normally sit. The text, formatted to +2 font size and a 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.

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.

  • + Share This
  • 🔖 Save To Your Account