Images can improve the design and the appearance of a web page.
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 specifies the URL (web address) of the image:
The alt attribute provides an alternate text for an image, if the user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).
The value of the alt attribute should describe the image:
If a browser cannot find an image, it will display the value of the alt attribute:
Note: The alt attribute is required. A web page will not validate correctly without it.
You can use the style attribute to specify the width and height of an image.
Alternatively, you can use the width and height attributes:
The width and height attributes always defines the width and height of the image in pixels.
Note: Always specify the width and height of an image. If width and height are not specified, the page might flicker while the image loads.
Both the width, height, and style attributes are valid in HTML5.
However, we suggest using the style attribute. It prevents styles sheets from changing the size of images:
If not specified, the browser expects to find the image in the same folder as the web page.
However, it is common to store images in a sub-folder. You must then include the folder name in the src attribute:
Some web sites store their images on image servers.
Actually, you can access images from any web address in the world:
You can read more about file paths in the chapter HTML File Paths.
HTML allows animated GIFs:
To use an image as a link, put the <img> tag inside the <a> tag:
Note: border:0; is added to prevent IE9 (and earlier) from displaying a border around the image (when the image is a link).
Use the CSS float property to let the image float to the right or to the left of a text:
Use the <map> tag to define an image-map. An image-map is an image with clickable areas.
In the image below, click on the computer, the phone, or the cup of coffee:
The name attribute of the <map> tag is associated with the <img>'s usemap attribute and creates a relationship between the image and the map.
The <map> tag contains a number of <area> tags, that defines the clickable areas in the image-map.
To add a background image on an HTML element, use the CSS property
To add a background image on a web page, specify the background-image property on the BODY element:
To add a background image on a paragraph, specify the background-image property on the P element:
To learn more about background images, study our CSS Background Tutorial.
HTML5 introduced the <picture> element to add more flexibility when specifying image resources.
The <picture> element contains a number of <source> elements, each referring to different image sources. This way the browser can choose the image that best fit the current view and/or device.
Each <source> element have attributes describing when their image is the most suitable.
The browser will use the first <source> element with matching attribute values, and ignore any following <source> elements.
Show one picture if the browser window (viewport) is a minimum of 650 pixels, and another image if not, but larger than 465 pixels.
Note: Always specify an <img> element as the last child element of the <picture> element. The <img> element is used by browsers that to not support the <picture> element, or if none of the <source> tags matched.
A screen reader is a software program that reads the HTML code, converts the text, and allows the user to "listen" to the content. Screen readers are useful for people who are blind, visually impaired, or learning disabled.
Note: Loading images takes time. Large images can slow down your page. Use images carefully.