Images

Once you understand links, adding images is straightforward.

Images are a little different than the HTML headings and paragraphs that we’ve seen so far. An image is actually a separate file that you embed in your web page using the img element. Let’s see how this works:

Example 2.15. Example Image

<p>
  Yet more evidence that I did not go to Art School:
</p>
<img src="/images/html/oscope.gif" alt="a cartoon of an oscilloscope">

To try this example on your own, you not only need to copy the HTML, but you also need to download the image “oscope.gif” to your system. If you don’t know how to do this, refer to “Save Images”. Save the file in the same directory as your test page, and try it out.

The img element has two required attributes:

An image file is not coded in the HTML language. Images have their own formats, such as GIF, JPEG, or PNG. There are several ways to acquire or create digital images:

Floating Images

Positioning elements in HTML can be tricky, and we’ll be talking about how to do this in the more advanced sections of this tutorial. That said, let’s take a sneak preview at one of the basic concepts of positioning: the float property.

Example 2.16. Floating Images

<h1>Not Your Father's Oscilloscope...</h1>

<img src="/images/html/oscope.gif" height="64" width="90"
alt="Right-floating oscilloscope"
style="float: right">

<p>
The <b>Model XJ-2000</b>:
Quality, quality, quality. That's what our bosses
asked for when we designed the XJ-2000, and that's
what we delivered. Our competitors claim to deliver
"quality"... but do they? Sure, they prattle on about
about sample rates, picosecond/div sweep rates,
Fast Fourier Transforms, and other technical
mumbo-jumbo. But the fact is that the XJ-2000 is
light-years ahead of the competition. From the
scratch-resistant chrome case to the innovative
green phosophorescent screen, only one word should
spring to mind when you look at the XJ-2000: quality.
</p>

The float: right directive does something peculiar: it removes the image from the regular flow of the page and “floats” it to the right. The paragraph flows around the image. If we had specified float: left, the image would float to the left of the paragraph. (Try it!)

Just like the color and background properties, you can actually apply the float property to nearly any element. We’ll return to this concept later.

Further Reading