HTML <img> Tag

The HTML <img> tag is used for embedding images into an HTML document.

Before you use the <img> tag, you need to ensure that the image exists on the internet. You then reference the location of that image when using the <img> tag.


The <img> tag is written as <img src="" alt=""> (no end tag) with the image URL inserted between the double quotes of the src attribute.

The alt attribute provides alternative text for users who are unable to view the image. Some reasons why users can't view the image might include having a slow Internet connection, they are vision impaired and are using text-to-speech software, the image won't load for some reason, etc. Note that the alt attribute is a required attribute.

Like this:

You can also provide the image dimensions using the width and height attributes.

Or you can provide the image dimensions using CSS.


Basic tag usage

Using width and height

You can use the width and height attributes to provide the dimensions for the image.

In this example, we'll scale the image to be smaller (for demonstration purposes), however, this is not recommended. It's better to scale the image using image-editing software first (i.e. before it's uploaded to the internet) - so that it is the correct size to start with. Doing this reduces the file size. Scaling it using HTML does not reduce the file size - it uses the same (larger) file and simply resizes it in the browser.

Responsive Design

If your website needs to be displayed on multiple sized screens (eg, mobiles, tablets, desktops, laptops, etc) you may find that your images are too large for some devices. This will be particularly true if you use the width and height attributes to set the dimensions of a large image.

Here are two ways of dealing with this situation.


One trick is to use the CSS max-width property to set a maximum width for the image of 100%. By specifying a maximum width, you are not explicitly setting the actual width. You are simply telling the browser not to go any bigger than the width that you specify - 100%. In this case, the browser should still reduce the size of the image if 100% is too big for the screen.

Like this:

The srcset & sizes Attributes

HTML 5.1 and the HTML Living Standard have introduced the srcset and sizes attributes. These are designed to deal with the issue of multiple sized screens and resolutions.

Note that at the time of writing, browser support was extremely limited for these attributes.

The srcset Attribute

The srcset attribute accepts a comma-separated list of URLs combined with a pixel-density value. Each of these would normally represent the same image but at a different size.

You should still use the standard src attribute to cater for user agents that don't support the srcset attribute.

In this example, I've intentionally used a different image for each resolution. This makes it easier to distinguish which image is being displayed. This is for demonstration purposes only. In reality, you would use the same image but at different resolutions.

Depending on your resolution, and whether your browser supports the srcset attribute, you should have seen one of the following images:

The sizes Attribute

The sizes attribute specifies how large the image should render within the layout. You can use media queries to specify a list of width values to use, depending on the space available for the image. For example, sizes="(min-width: 36em) 33.3vw, 100vw".

At the time of writing, the srcset and sizes attributes are still being worked out by the WHATWG and W3C and browser support is limited.

Linked Image

You can link your image to another web page by nesting it inside the <a> tag.

Like this:

Image Maps

You can use the <img> tag along with the <area> and <map> tags to create an image map.

Image maps enables one image to link to multiple pages. You can specify different shapes for the "hotspot" area too.

The following example demonstrates this (click on each country to see where the link goes). For more information on image maps, see the <map> tag.


Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.

The <img> element accepts the following attributes.

Element-Specific Attributes

This table shows the attributes that are specific to the <img> tag/element.

altAlternate text. This specifies text to be used in case the browser/user agent can't render the image.
srcLocation of the image.
crossoriginThis attribute is a CORS settings attribute. CORS stands for Cross-Origin Resource Sharing. The purpose of the crossorigin attribute is to allow you to configure the CORS requests for the element's fetched data. The values for the crossorigin attribute are enumerated.

Possible values:

anonymousCross-origin CORS requests for the element will not have the credentials flag set. In other words, there will be no exchange of user credentials via cookies, client-side SSL certificates or HTTP authentication.
use-credentialsCross-origin CORS requests for the element will have the credentials flag set.

If this attribute is not specified, CORS is not used at all.

An invalid keyword and an empty string will be handled as the anonymous value.

ismapFor image maps. See HTML map tag
usemapFor image maps. See HTML map tag
widthSpecifies the width of the image.
heightSpecifies the height of the image.

Global Attributes

The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <img> tag , as well as with all other HTML tags.

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handler Content Attributes

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Below are the standard HTML5 event handler content attributes.

Again, you can use any of these with the <img> element, as well as any other HTML5 element.

For a full explanation of these attributes, see HTML 5 event handler content attributes.

Differences Between HTML 4 & HTML 5

HTML5 doesn't support the following attributes:

HTML5 (and the HTML Living Standard) introduced the following attribute:

HTML 5.1 and the HTML Living Standard introduced the following attributes:

To see more detail on the two versions see HTML5 <img> Tag and HTML4 <img> Tag. Also check out the links to the official specifications below.


Here's a template for the <img> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

For more information on attributes for this tag, see HTML5 <img> Tag and HTML4 <img> Tag.

Tag Details

For more details about the <img> tag, see HTML5 <img> Tag and HTML4 <img> Tag.


Here are the official specifications for the <img> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.