The img tag allows you to easily add images to your website. It supports numerous formats and a variety of at­trib­utes can enhance the HTML tag in img even further.

What is the img tag needed for?

The HTML tag “img” inserts images and graphics into websites. The img tag functions as a place­hold­er, whereby the stored src attribute links to the actual storage location of the graphic when a website is accessed. The image is then embedded into the website. An alt attribute is required in addition to the src attribute. This will store an al­tern­at­ive text in case the image cannot be displayed. JPEG, GIF, PNG, and several others are supported by the img tag in HTML.

What is the img tag syntax?

The img tag syntax is straight­for­ward and only requires a few com­pon­ents. The simplest variant is:

<img src="redcar.png" alt="small red car at a traffic light">

To ensure the graphic is displayed correctly, it is a good idea to include the image’s height and width on the website when using the img tag. These settings can be found in HTML at­trib­utes width and height. The code should look like this:

<img src="redcar.png" width="500" height="200" alt="small red car at a traffic light">

The loading attribute is also re­com­men­ded when using the HTML tag img. This ensures that the cor­res­pond­ing graphic is only loaded when users scroll to the cor­res­pond­ing position. Set the value of the loading attribute to lazy for this feature. This is how it should look in code:

<img src="redcar.png" width="500" height="200" alt="small red car at a traffic light" loading="lazy">

Which at­trib­utes can be used in the HTML tag img?

In addition to the width, height, and loading at­trib­utes, there are several other at­trib­utes which are supported by the img tag in HTML. These include all global, event, and other specific at­trib­utes. Below is an overview of the at­trib­utes which can be used with the img tag:

Attribute Value De­scrip­tion
old "Text" Mandatory; specifies an al­tern­at­ive text for the image.
cros­s­ori­gin anonymous, use-cre­den­tials Specifies which third-party websites can access the cor­res­pond­ing image with a script.
decoding auto, sync, async Defines how and whether an image can be decoded.
height Pixel or percent Sets the image’s height on the website.
ismap true, false Controls image’s access to a server-side image map.
loading car, eager, lazy De­term­ines when an image should load.
re­fer­rer­policy no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, unsafe-url States whether the image comes from an unsafe source.
sizes sizes Defines image sizes for different page layouts.
src URL Mandatory; specifies the image’s URI address.
srcset URL lists Stores a list of images for different purposes, e.g. different devices and sizes.
usemap #mapname Links the element to a specific image map.
width Pixel or percent Specifies the image’s width.

While the above at­trib­utes are commonly used in img tag, there are other at­trib­utes which have become outdated since HTML5 and HTML 5.1, such as align, border, hspace, longdesk, and vspace.

Tip

The Website Builder from IONOS is the ideal tool to design an in­di­vidu­al and pro­fes­sion­al website. It allows you to cultivate your online presence with your ideas in mind ⁠— without much prior knowledge. Our ex­per­i­enced experts will also be happy to help you if you would prefer to leave this task to the pro­fes­sion­als.

Go to Main Menu