HTML Images


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



HTML Images Syntax

The HTML <img> tag is used to display images on web page.

Images are not physically inserted into a web page; instead, they are linked to web pages. The <img> tag reserves a space on the web page to display the referenced image.

The <img> tag is tag is a self-closing tag that only contains attributes and does not require a closing tag.

The <img> tag has two required attributes:

  • src - Specifies the path to the image
  • alt - Specifies an alternate text for the image

The src Attribute

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

Note: When a web page loads, the browser retrieves the image from a web server and places it within the page. To avoid displaying a broken link icon to visitors, ensure that the image remains in the same position in relation to the web page. alt text are shown if the browser cannot find the image.


The alt Attribute

The required alt attribute supplies an alternative text for an image in case the user is unable to view it due to slow connection, errors in the src attribute, or if they are using a screen reader.

The value of the alt attribute should describe the image:


Image Size - Width and Height

You can use the style attribute is used to define the width and height of an image.

Instead, you have the option to use the width and height attributes for specifying the dimensions of an image:


Width and Height, or Style?

The width, height, and style attributes are all acceptable in HTML for controlling the size of images.

However, it is recommended to use the style attribute to preserve the image size, as it prevents style sheets from altering the image dimensions:


Images in Another Folder

When your images are located in a sub-folder, it is essential to include the folder name in the src attribute of the image tag:


Images on Another Server/Website

Certain websites link to images hosted on a different server.

To link to an image on another server, you need to provide an absolute (full) URL in thesrc attribute of the image tag:

Notes on external images: External images may be subject to copyright restrictions. Using them without proper permission could lead to copyright infringement. Moreover, you have no control over external images, and they might be removed or altered without warning.


Animated Images

HTML allows animated GIFs:


Image as a Link

To create an image link, simply place the <img> tag inside the <a> tag:


Image Floating

Employ the CSS float property to let the image float to the right or to the left of a text:


HTML Image Maps

HTML image maps let you make clickable parts on a picture.

The HTML tag is used to create an image map. An image map is essentially an image that has clickable areas. These clickable areas are specified using one or more tags.

You can click on specific regions when you move your cursor over different parts of the image.

benches plant sunset mushroom

HTML Background Images

You can set a background image for almost any HTML element.

If you want to put a background image on an HTML element, you can do it by using the HTML style attribute along with the CSS background-image property:


Background Repeat

If the background picture is not as big as the element, it will keep appearing repeatedly both horizontally and vertically until it covers the entire element.

To prevent the background image from repeating, use the background-repeat property and set it to no-repeat.


Background Cover

If you wish to make sure the background image fills the whole element, just use the background-size property and set it to cover.

Additionally, ensure the complete coverage of the entire element by setting the background-attachment property to fixed.

In this method, the background picture will completely cover the element without getting distorted, maintaining its original proportions:


Background Stretch

If you wish the background picture to expand and cover the whole element, you can use the background-size property and set it to 100% 100%.


Common Image Formats

Abbreviation File Format File Extension
APNG Animated Portable Network Graphics .apng
GIF Graphics Interchange Format .gif
ICO Microsoft Icon .ico, .cur
JPEG Joint Photographic Expert Group image .jpg, .jpeg, .jfif, .pjpeg, .pjp
PNG Portable Network Graphics .png
SVG Scalable Vector Graphics .svg

Chapter Summary

  • Use the HTML <img> element to display an image
  • Use the HTML src attribute can be used to give URL to the image
  • Use the HTML alt attribute is used to give an alternate text for an image, if it cannot be displayed
  • Use the HTML width and height attributes or the CSS width and height properties used to specify the size of the image
  • Use the CSS float property to let the image float to the left or to the right