HTML <img> Tag
<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 is written as
<img src="" alt=""> (no end tag) with the image URL inserted between the double quotes of 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.
You can also provide the image dimensions using the
Or you can provide the image dimensions using CSS.
Basic tag usage
You can use the
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.
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
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.
HTML 5.1 and the HTML Living Standard have introduced the
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.
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
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:
- If your browser doesn't support the
- 1x (one device pixel per CSS pixel): phi-phi-200x150.jpg
- 2x (two device pixels per CSS pixel): boracay-resort-1000x750.jpg
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
sizes attributes are still being worked out by the WHATWG and W3C and browser support is limited.
You can link your image to another web page by nesting it inside the
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
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.
<img> element accepts the following attributes.
This table shows the attributes that are specific to the
|alt||Alternate text. This specifies text to be used in case the browser/user agent can't render the image.|
|src||Location of the image.|
|crossorigin||This attribute is a CORS settings attribute. CORS stands for Cross-Origin Resource Sharing. The purpose of the
If this attribute is not specified, CORS is not used at all.
An invalid keyword and an empty string will be handled as the
|ismap||For image maps. See HTML map tag|
|usemap||For image maps. See HTML map tag|
|width||Specifies the width of the image.|
|height||Specifies the height of the image.|
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:
srcset- This specifies images to use in different situations (e.g. high-resolution displays, small monitors, etc)
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.
Here are the official specifications for the
- HTML5 Specification (W3C)
- HTML Living Standard (WHATWG)
- Current W3C Draft (the next version that is currently being worked on)
- HTML 4 (W3C)
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.