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.
Syntax
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 srcset attribute can also be used in order to specify different images to use in different situations (e.g. high-resolution displays, small monitors, etc). The value of the alt attribute appears if the image cannot be loaded.
Like this:
You can provide the image dimensions using the width and height attributes. It is strongly recommended to always include width and height (or aspect ratio CSS) to prevent layout shifts while the image loads.
Or you can provide the image dimensions using CSS.
alt attribute is strictly required on all <img> elements. It provides alternative text for users who are unable to view the image (such as screen reader users or those on slow connections). If an image is purely decorative and conveys no meaningful information, use an empty alt attribute (alt="") so screen readers skip it.
Examples
Basic tag usage
Using width and height
You can use the width and height attributes to provide the intrinsic 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.
CSS
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
The srcset and sizes attributes were introduced to deal with the issue of multiple sized screens and resolutions efficiently.
It goes a step further than a pure CSS solution, because it allows the browser to download the most appropriate image size based on the device's screen resolution and available layout space.
The srcset Attribute
The srcset attribute accepts a comma-separated list of URLs, optionally combined with either a width descriptor or a pixel-density descriptor. Each URL would normally represent the same image but at a different size or cropping.
Here's an example of what that might look like when using width descriptors:
And here's an example that uses pixel density descriptors:
You should still use the standard src attribute to cater for user agents that don't support the srcset attribute. In fact, the HTML specification states that you must include the src attribute when using the <img> tag.
The sizes Attribute
The sizes attribute allows you to specify different image display sizes for different page layouts.
Example:
You can use media queries to specify a list of width values to use, depending on the space available for the image.
Example:
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 enable 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
Attributes can be added to an HTML element to provide more information about how the element should appear or behave.
The <img> element accepts the following attributes.
| Attribute | Description | ||||||
|---|---|---|---|---|---|---|---|
alt | Alternate text. Specifies alternative text for the image, which is required for accessibility and fallback display. | ||||||
src | URL of the image. Required for the <img> tag. |
||||||
srcset | Comma-separated list of image candidate strings to use in different situations (e.g., high-resolution displays, small monitors, etc). Each image URL can be optionally followed by a whitespace and a descriptor. The descriptor, if any, can be one of the following:
|
||||||
sizes | Image sizes condition list indicating the intended size of the image across different layout breakpoints. | ||||||
crossorigin | This 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.
Possible values:
If this attribute is not specified, CORS is not used at all. |
||||||
ismap | Specifies a server-side image map. See HTML map tag | ||||||
usemap | Hash-name reference to a <map> element to use as an image map. See HTML map tag |
||||||
width | Specifies the intrinsic width of the image in pixels. | ||||||
height | Specifies the intrinsic height of the image in pixels. | ||||||
referrerpolicy | Indicates which referrer to use when fetching the resource.
Possible values: |
||||||
decoding |
Provides an image decoding hint to the browser on whether to decode the image synchronously or asynchronously. Possible values: |
||||||
loading | Indicates the browser loading preference for images outside the viewport.
Possible values:
|
Global Attributes
The following attributes are standard across all HTML elements. Therefore, you can use these attributes with the <img> tag , as well as with all other HTML tags.
accesskeyautocapitalizeclasscontenteditabledata-*dirdraggablehiddenidinputmodeisitemiditempropitemrefitemscopeitemtypelangpartslotspellcheckstyletabindextitletranslate
For a full explanation of these attributes, see HTML 5 global attributes.
Event Handlers
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.
onabortonauxclickonbluroncanceloncanplayoncanplaythroughonchangeonclickoncloseoncontextmenuoncopyoncuechangeoncutondblclickondragondragendondragenterondragexitondragleaveondragoverondragstartondropondurationchangeonemptiedonendedonerroronfocusonformdataoninputoninvalidonkeydownonkeypressonkeyuponlanguagechangeonloadonloadeddataonloadedmetadataonloadstartonmousedownonmouseenteronmouseleaveonmousemoveonmouseoutonmouseoveronmouseuponpasteonpauseonplayonplayingonprogressonratechangeonresetonresizeonscrollonsecuritypolicyviolationonseekedonseekingonselectonslotchangeonstalledonsubmitonsuspendontimeupdateontoggleonvolumechangeonwaitingonwheel
Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.
For more detail, see HTML event handler content attributes.