HyperText Markup Language/Images

From Wikibooks, open books for an open world
< HyperText Markup Language
Jump to: navigation, search

Let us start with a quick minimum example:

<img src="turtleneck-sweater.jpeg" />

And let us also have a look at more extended example:

<img src="turtleneck-sweater.jpeg" alt="Photograph of a Turtleneck Sweater"
     title="Photograph of a Turtleneck Sweater" />

Images are normally stored in external files. To place an image into an HTML source, use the img tag with the src attribute containing the URL of the image file. To support browsers that cannot render images, you can provide the alt attribute with a textual description of the image. To provide a tooltip to the image, use the title attribute.

The space before the /> in the examples is there on purpose. Some older browsers behave strangely if the space is omitted.


Per default, images are placed inline to their surroundings. To place the image as a block or float instead, you can use Cascading Style Sheets.


To do:
Show how to place images using CSS.

Alternative text and tooltip[edit]

The HTML specification requires that all images have an alt attribute. This is commonly known as alt text. Images can be split in to two categories:

  • those that add to the content of the page;
  • those that are purely decorative, e.g. spacers, fancy borders and bullets.

Decorative images should have empty alt text, i.e. alt="". Images used as bullets may use an asterisk, alt="*".

All other images should have meaningful alt text. Alt text is not a description of the image, use the title attribute for short descriptions. Alt text is something that will be read instead of the image. For example,

 <img src="company_logo.gif" alt="???"> makes the best widgets in the world.

The alt text should be the company's name not the ever popular 'Our logo', which would give the sentence 'Our logo makes the best widgets in the world.' when read in a text only browser.

The alt attribute stands for alternate which non-graphic-capable browsers (such as Lynx) may use to better enable its user to understand the purpose of the image. Some older browsers incorrectly use the alt attribute' tag to produce image tooltips. However, the title attribute should actually be used for this.

Width and height[edit]

To have an image appear smaller than it is in the external file, use the attributes width and height. When only one or the other is specified, images will scale to keep the same overall ratio.