Skip to main content

HTML Images

Images can improve the design and look of a web page.

Trulli houses

An image of Trulli houses.

Prompt
A page that shows an image of Trulli houses using the img tag, src "pic_trulli.jpg", alt "Italian Trulli".
Output: complete HTML page

Girl in a jacket

An image with alt text.

Prompt
A page that shows an image using the img tag, src "img_girl.jpg", alt "Girl in a jacket".
Output: complete HTML page

Flowers in Chania

Another image with alt text.

Prompt
A page that shows an image using the img tag, src "img_chania.jpg", alt "Flowers in Chania".
Output: complete HTML page

HTML Images Syntax

The <img> tag puts an image on a page. Images are not really inserted into the page; they are linked to it, and the <img> tag makes a holding space for the linked image. It is an empty tag with attributes only and no closing tag.

It has two required attributes: src, the path to the image, and alt, alternate text for the image.

Image syntax

The shape of an image tag.

index.html
<img src="url" alt="alternatetext">

The src Attribute

The required src attribute sets the path (URL) to the image.

Note: When the page loads, the browser fetches the image from the server and inserts it. Keep the image in the same place relative to the page, or visitors will see a broken-link icon. The broken-link icon and the alt text appear when the browser cannot find the image.

Example

An image set with the src attribute.

Prompt
A page with an image whose src attribute points to img_chania.jpg, alt "Flowers in Chania".
Output: complete HTML page

The alt Attribute

The required alt attribute gives alternate text shown when the image cannot be viewed, such as on a slow connection, a wrong src, or for screen-reader users. The text should describe the image.

Tip: A screen reader reads the HTML aloud so the user can listen to the content; it helps people who are visually impaired or have a learning disability.

Descriptive alt text

An image with describing alt text.

Prompt
A page with an image that includes a descriptive alt attribute "Flowers in Chania", src img_chania.jpg.
Output: complete HTML page

When the image is missing

The alt text shows because the image cannot be found.

Prompt
A page with an image whose src does not exist (wrongname.gif) so the alt text "Flowers in Chania" is shown instead.
Output: complete HTML page

Image Size - Width and Height

You can set an image's width and height with the style attribute, or with the width and height attributes. Both always measure in pixels.

Note: Always set the width and height. If you do not, the page may flicker while the image loads.

With the style attribute

Sizing an image with CSS in the style attribute.

Prompt
A page with an image sized using the style attribute: width 500px and height 600px, src img_girl.jpg, alt "Girl in a jacket".
Output: complete HTML page

With width and height attributes

Sizing an image with the width and height attributes.

Prompt
A page with an image sized using the width and height attributes: 500 by 600 pixels, src img_girl.jpg, alt "Girl in a jacket".
Output: complete HTML page

Width and Height, or Style?

The width, height, and style attributes are all valid. PromptCookbook suggests the style attribute, because it stops style sheets from changing the image's size.

Example

A page where a style sheet would otherwise resize the images.

Prompt
A page with internal CSS making all images 100 percent width, then two HTML5 icon images: one sized with width and height attributes (128x128) and one sized with the style attribute (128px).
Output: complete HTML page

Images in Another Folder

If your image is in a sub-folder, include the folder name in the src attribute.

Example

An image referenced from a sub-folder.

Prompt
A page with an image stored in a sub-folder, referenced as /images/html5.gif, alt "HTML5 Icon", sized 128px with the style attribute.
Output: complete HTML page

Images on Another Server/Website

To use an image from another server, give a full (absolute) URL in the src attribute.

Note: External images may be under copyright, and using one without permission could break copyright laws. You also cannot control external images; they can change or disappear without warning.

Example

An image loaded from another website.

Prompt
A page with an image hosted on another website, referenced with a full URL https://prompt-cookbook.com alt "PromptCookbook".
Output: complete HTML page

Animated Images

HTML can show animated GIFs the same way as any other image.

Example

An animated GIF.

Prompt
A page with an animated GIF using the img tag, src "programming.gif", alt "Computer Man", sized 48px with the style attribute.
Output: complete HTML page

Image as a Link

To use an image as a link, put the <img> tag inside the <a> tag.

Example

An image that works as a link.

Prompt
A page using an image as a link: an img (smiley.gif, alt "HTML tutorial", 42px) inside an a tag pointing to default.asp.
Output: complete HTML page

Image Floating

Use the CSS float property to let an image float to the right or left of the text around it.

Example

Images floating to the right and to the left of text.

Prompt
A page with two paragraphs, each containing a smiley image floated with CSS: the first floats right, the second floats left, with text wrapping around each.
Output: complete HTML page

Common Image Formats

The most common image formats, supported in all browsers, are: APNG (Animated Portable Network Graphics, .apng), GIF (Graphics Interchange Format, .gif), ICO (Microsoft Icon, .ico), JPEG (.jpg, .jpeg), PNG (Portable Network Graphics, .png), and SVG (Scalable Vector Graphics, .svg).


Chapter Summary

To recap: use the <img> element to define an image, the src attribute for its URL, and the alt attribute for alternate text. Set the size with the width and height attributes or the CSS width and height properties, and use the CSS float property to float an image left or right.

Note: Large images take time to load and can slow your page, so use images carefully.


HTML Image Tags

The image-related tags are: <img> (defines an image), <map> (defines an image map), <area> (defines a clickable area inside an image map), and <picture> (defines a container for multiple image resources).