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.
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.
Examples
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.
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 in HTML5 in order to deal with the issue of multiple sized screens and resolutions.
It goes a step further than a pure CSS solution, because it allows you to specify which images to use in different situations (e.g., high-resolution displays, small monitors, etc).
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 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 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
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. This specifies text to be used in case the browser/user agent can't render the image. | ||||||
src | URL of the image. | ||||||
srcset | Comma-separated list of images 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 between 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. The values for the crossorigin attribute are enumerated.
Possible values:
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. | ||||||
referrerpolicy | Referrer policy for fetches initiated by the element. | ||||||
longdesc | A url that provides a link to an expanded description of the image. | ||||||
decoding |
Indicates the preferred method to decode this image. The attribute, if present, must be an image decoding hint. This attribute's missing value default and invalid value default are both the auto state. |
||||||
loading | This is a lazy loading attribute. Its purpose is to indicate the policy for loading images that are 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.
accesskey
autocapitalize
class
contenteditable
data-*
dir
draggable
hidden
id
inputmode
is
itemid
itemprop
itemref
itemscope
itemtype
lang
part
slot
spellcheck
style
tabindex
title
translate
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.
onabort
onauxclick
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncopy
oncuechange
oncut
ondblclick
ondrag
ondragend
ondragenter
ondragexit
ondragleave
ondragover
ondragstart
ondrop
ondurationchange
onemptied
onended
onerror
onfocus
onformdata
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onlanguagechange
onload
onloadeddata
onloadedmetadata
onloadstart
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onpaste
onpause
onplay
onplaying
onprogress
onratechange
onreset
onresize
onscroll
onsecuritypolicyviolation
onseeked
onseeking
onselect
onslotchange
onstalled
onsubmit
onsuspend
ontimeupdate
ontoggle
onvolumechange
onwaiting
onwheel
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.