Always specify image size. If the size is unknown, the page will flicker while the image loads. |
In HTML, images are defined with the <img> tag.
The <img> tag is empty, it contains attributes only, and does not have a closing tag.
The src attribute defines the url (web address) of the image:
The alt attribute specifies an alternate text for the image, if it cannot be displayed.
The value of the alt attribute should describe the image in words:
The alt attribute is required. A web page will not validate correctly without it.
Screen readers are software programs that can read what is displayed on a screen.
Used on the web, screen readers can "reproduce" HTML as text-to-speech, sound icons, or braille output.
Screen readers are used by people who are blind, visually impaired, or learning disabled.
Screen readers can read the alt attribute. |
You can use the style attribute to specify the width and height of an image.
The values are specified in pixels (use px after the value):
Alternatively, you can use width and height attributes.
The values are specified in pixels (without px after the value):
Both the width, the height, and the style attributes, are valid in the latest HTML5 standard.
We suggest you use the style attribute. It prevents styles sheets from changing the default size of images:
At W3schools we prefer to use the style attribute. |
If not specified, the browser expects to find the image in the same folder as the web page.
However, it is common on the web, to store images in a sub-folder, and refer to the folder in the image name:
If a browser cannot find an image, it will display a broken link icon:
Some web sites store their images on image servers.
Actually, you can access images from any web address in the world:
The GIF standard allows animated images:
Note that the syntax of inserting animated images is no different from non-animated images.
It is common to use images as links:
We have added border:0 to prevent IE9 (and earlier) from displaying a border around the image. |
For an image, you can create an image map, with clickable areas:
You can let an image float to the left or right of a paragraph:
Loading images takes time. Large images can slow down your page. Use images carefully. |
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 »
Tag | Description |
---|---|
<img> | Defines an image |
<map> | Defines an image-map |
<area> | Defines a clickable area inside an image-map |