Images

Most of the material about images is covered in the tutorial at http://catcode.com/imgguide/. Please read that tutorial before continuing.

Aligning Images

The ability to align images in HTML evolved in two stages. The first stage added the ability to align images with respect to the surrounding text. The second stage gave the ability to take an image out of the flow (the natural way that text comes onto the page, from left to right, top to bottom). Taking an image out of the flow lets you wrap text around an image.

Alignment with Text

As you see, book with W3C on the cover this image does not start a new line. That means that the <img> element puts pictures in line with the text flow. Notice that the bottom of the picture is aligned with the baseline of the text (the common horizontal line for all the letters). You can change this alignment with the vertical-align style property.

In the following example, I have placed the picture in a folder named screenshots with the name w3book.png. You may save the picture in the same directory as your HTML files (right-click the image to do this), or put it in any directory you like. When you try your commands, you just have to make sure you put in the proper relative URL for the src attribute.

Put the following lines into your HTML file. The baseline value for the vertical-align property is the default value.

This <img src="w3book.png" width="49" height="61"
   style="vertical-align: baseline"
   alt="book with W3C on cover" /> is my book.

If you substitute the following values instead of baseline, you will see that the alignment of the image relative to its surrounding text changes accordingly.

Value of vertical-alignResult
baseline
This book with W3C on cover is my book.
bottom
This book with W3C on cover is my book.
middle
This book with W3C on cover is my book.
top
This book with W3C on cover is my book.

Yes, there is a difference between baseline and bottom. Notice that, when vertical alignment is at the bottom, the bottom of the book is below the tail of the letter “y”; with baseline, the bottom of the book is not below the tail of the “y.”

You can see a complete list of possible values for vertical-align in the CSS specification.

Wrapping Text Around an Image

By adding the style style="float: left" to an image, you tell the browser to take the image out of the flow and put it at the left side of the browser window. For the following example, use this image: sample image. In the following examples, I have placed the image in the screenshots subdirectory with the name mini_img.png. Change the URL to reference the place where you have saved the image. Try this and see what it looks like.

<p>
This is text before an image. The image, which is left aligned, is a
small yellow rectangle,
<img src="screenshots/mini_img.png" style="float: left"
    alt="sample image" width="100" height="44" />
which contains a red circle and a deep turquoise square. Because the
image has been left-aligned, you won&#8217;t see it in the middle of the
text. Instead, it will go to the left side of the window, and the text
will &#8220;close up&#8221; You won&#8217;t see the picture between
the words <em>rectangle</em> and <em>which</em>.
The text will appear to wrap around the image.</p>

Now, replace the word left with right everywhere in the example and try it again.

Stopping Text Wrap

Now try this example:

<p>Here is a left-aligned image.
<img src="screenshots/mini_img.png" style="float: left"
    alt="sample image" width="100" height="44" />
Text wraps around it.</p>
<p>Here is another image at the right.
<img src="screenshots/w3book.png" style="float: right"
    alt="book with W3C on cover" width="49" height="61" />
Text wraps around it as well.</p>
<p>The end.</p>

Here is the result:

Here is a left-aligned image. sample image Text wraps around it.

Here is another image at the right. book with W3C on cover Text wraps around it as well.

The end.

It looks like this because all the text is collapsed into the flow. What you really want is three separate sections. You want the text for the “image at right” separately from the area with the left-aligned image, and you want “The end” to display separately from the area with the right-aligned image. You do this by adding a line break with a style="clear: left" attribute. The first <br /> in the following code says, “begin a new line for text when the left side of the screen has no graphics on it (when it’s clear of graphics).” The second <br /> says, “begin a new line for text when the right side of the screen is clear of any graphics.” Try it and see.

<p>Here is a left-aligned image.
<img src="screenshots/mini_img.png" align="left"
    alt="sample image" width="100" height="44" />
Text wraps around it.</p>
<br style="clear: left" />
<p>Here is another image at the right.
<img src="screenshots/w3book.png" align="right"
    alt="book with W3C on cover" width="49" height="61" />
Text wraps around it as well.</p>
<br style="clear: right" />
<p>The end.</p>

Spacing Around an Image

As you have seen when you tried the examples (you did try them, didn’t you?) the text comes right up to the borders of the image. If you want to add horizontal and vertical space around the image, add an extra property specifier for margin-left, margin-right, margin-top, or margin-bottom. For example, in the example with lots of text, change style="float:left" to style="float:left; margin-right:5px" to add 5 pixels of space between the image and the text. You must use the px to specify pixels. You will learn more about margins and other spacing specifications later in the course.

Placing a Border Around an Image

You may place a border around an image by using markup like this:

<img src="screenshots/mini_img.png"
    style="border: 1px solid black;"
    alt="sample image" width="100" height="44" />

The 1px tells the width of the border; the solid gives the border style (you will find out later that there are other kinds of borders), and, of course, black gives the color.

Centering an Image

Everyone tries something like this, and then complains that it doesn’t center the image in the middle of the window:

<img src="screenshots/mini_img.png"
    style="text-align: center"
    alt="sample image" width="100" height="44" />

The <img> element does not use style="text-align: center". Period. And style="vertical-align: middle" does not do what you want either. Instead, you must place the image inside a <div> and center its content:

<div style="text-align: center">
<img src="screenshots/mini_img.png"
	alt="sample image" width="100" height="44" />
</div>

ImageMaps

Sometimes you to use an image for navigation and have different parts of the image go to different locations. Move your mouse around the image below and look in the status bar at the bottom of the browser window to see where a click will lead you:

Image map with four buttons
Linux information Free office suite News for nerds - stuff that matters Connect your digital camera to Linux

This is called an imagemap, because it maps sections of your image to different URLS. Setting up an imagemap by hand is a multi-step process.

Create a <map> element

All of the information about the imagemap will be contained within a <map> element. Put the tag into your document (it can go anywhere; usually imagemaps go at the bottom of the document), and think up a name for your imagemap. This name will be the value of the map’s name attribute. In this example, we'll call our imagemap buttonmap. In XHTML, you must also have an id attribute, and it must have the same value as the name attribute.

<map name="buttonmap" id="buttonmap">
	<!-- imagemap info here -->
</map>

Identify the coordinates of the active sections of the image

Now figure out which areas of the image will be clickable, and where they are located within the image. If you open the image in a program like Adobe Photoshop on Windows or the GNU Image Manipulation Program on Linux, you can move a pointer around the image and see the cursor’s coordinates. Coordinates are a pair of numbers; the first number tells how many pixels from the left the cursor is, and the second tells how many pixels down from the top the cursor is. You can see the coordinates of the arrow in the lower left of this picture:

cursor and coordinate display

Make a table of the upper left and lower right coordinates of the active areas of your image, and where they should go when clicked:

Coordinates Destination
Upper leftLower right
7, 7103, 83 http://www.linux.org/
139, 7223, 83 http://www.openoffice.org/
62, 88145, 150 http://slashdot.org/
207, 88297, 150 http://www.gphoto.org/

Create <area> elements

Each of these table entries will become a rectangular area. You use the <area> element with an attribute of shape="rect" and a coords="..." attribute to describe the area. You may also have an alt="..." attribute to make the information accessible to blind users. You may also use a title="..." attribute to create a tooltip.

Here is what the imagemap will look like once you have entered all the information. When you enter the coordinates, you may not put blanks after the commmas.

<map name="buttonmap" id="buttonmap">
    <area shape="rect" coords="7,8,103,83" href="http://www.linux.org/"
        alt="Linux information" title="Linux information" />
    <area shape="rect" coords="139,8,223,83" href="http://www.openoffice.org/"
        alt="Free office suite" title="Free office suite" />
    <area shape="rect" coords="62,88,145,150" href="http://slashdot.org/"
        alt="News for nerds - stuff that matters"
        title="News for nerds - stuff that matters" />
    <area shape="rect" coords="207,88,297,150" href="http://www.gphoto.org/"
        alt="Connect your digital camera to Linux"
        title="Connect your digital camera to Linux" />
</map>

The shape attribute tells what shape the area has; rect is obviously the easiest. There are other possibilities.

Connect your image to the imagemap

Add a usemap attribute to your <img> element; its value will be the URL of the imagemap. Since the imagemap is in the same file as the image, you will use a # in the URL:

<img src="screenshots/imagemap.png" usemap="#buttonmap"
	alt="Image map with four buttons"
	width="311" height="157"  border="0" />

Imagemap shapes

Here is a summary of all the values for the shape attribute inside of an <area>:

shape= coords value example
shape="rect" Coordinates of upper left and lower right of a rectangular area, separated by commas coords="7,7,25,82"
shape="circle" Coordinates of the center of a circular area, followed by the radius of the area. The example shows a 20-pixel circular area centered at coordinates (60, 40). coords="60,40,20"
shape="poly" An arbitrary polygon; you list the coordinates of each vertex of the polygon. The example shows a triangular area with vertices at (10,10), (30,30), and (50,10) coords="10,10,30,30,50,10"
shape="default" No coordinates needed; this describes a click anywhere in the image that is not occupied by other <area> specifications. N/A