Definition and Usage
The <img> tag represents an image. It is used to embed an image into an HTML document.
The <img> tag is the most basic way to embed images into an HTML document, playing an important role in enhancing visual appeal and conveying meaning on web pages.
Basic Example
The following code embeds an image named earth.jpg into a web page:
<img src="earth.jpg" alt="A round, blue Earth glowing in space">
This example shows how to use the <img> tag.
- As mentioned earlier, the
srcattribute specifies the path (URL) of the image to be embedded. - The
<img>tag can optionally include thealtattribute. This attribute is very useful for web accessibility because it provides alternative text that describes the image to users with visual impairments who use screen readers. In addition, some browsers (such as Chrome) display the value of this attribute as text instead when an image cannot be displayed for any reason, such as a network error or blocked content.
The <img> tag is a void element, meaning it cannot have any content and does not have a closing tag (</img>).
Using Related Attributes
Let's look at the attributes related to the <img> tag.
src Attribute
Specifies the path (URL) of the image to be embedded.
The <img> tag must include at least one of the src or srcset attributes, with a valid value specifying an image resource. This is required for the browser to fetch and embed the image into the document.
Supported Image Formats
Although the HTML specification does not define specific image formats that must be supported, keep in mind that there are some limitations on the types of image files that can be embedded in a web page using the src attribute. Different browsers may support different image formats.
The following are the most commonly used image formats on the web, supported by most browsers:
JPEG (.jpg, .jpeg) |
JPEG offers broad compatibility and can reduce file sizes while maintaining acceptable image quality, making it one of the most widely used image formats.
Supported by: Chrome, Edge, Firefox, Opera, Safari. |
|---|---|
PNG (.png) |
PNG is suitable for preserving high image quality and is preferred when transparency is needed. It is especially common for logos, icons, and images containing text.
Supported by: Chrome, Edge, Firefox, Opera, Safari. |
GIF (.gif) |
GIF is commonly used to store simple images and animations.
Supported by: Chrome, Edge, Firefox, Opera, Safari. |
WebP (.webp) |
Developed by Google, WebP is an image format designed to provide smaller file sizes than JPEG and PNG while maintaining similar image quality.
Supported by: Chrome, Edge, Firefox, Opera, Safari. |
SVG (.svg) |
SVG is a vector image format used to represent vector graphics. Images in this format can be resized without losing image quality.
Supported by: Chrome, Edge, Firefox, Opera, Safari. |
The following list covers image formats that can be displayed on the web but should be avoided in web content. Generally, this is because they are not widely supported by browsers or because better alternatives are available.
BMP (.bmp) |
BMP files are generally stored without compression, resulting in very large file sizes. Better alternatives, such as JPEG and PNG, are available.
Supported by: Chrome, Edge, Firefox, Opera, Safari. |
|---|---|
ICO (.ico) |
ICO is primarily used for Windows icons and favicons. It is not suitable for general images in the main content of web pages.
Supported by: Chrome, Edge, Firefox, Opera, Safari. |
TIFF (.tif, .tiff) |
Browser support for TIFF is limited compared with JPEG and PNG.
Supported by: Safari. |
alt Attribute
The alt attribute specifies alternative text for an image. It is optional.
It is very useful for web accessibility because it provides alternative text that users of screen readers, which are designed for people with visual impairments, can understand in place of the image. In addition, some browsers (e.g., Chrome) display the value of this attribute as text in place of an image when it cannot be displayed for any reason, such as a network error or blocked content.
Requirements for Providing Text as an Alternative to Images
The value of the alt attribute must not contain text that could be considered a caption, title, or legend for the image. It must contain alternative text that users can understand in place of the image, rather than text that merely supplements the image. The title attribute can be used to provide supplementary information about the image.
The most general rule to consider when writing alternative text is that replacing all images with the text provided in their alt attributes should not change the meaning of the page.
When the alt Attribute Is Omitted or Has an Empty Value ("")
This means that the image is not a key part of the content (e.g., it is purely decorative or a tracking pixel used for analytics) or cannot be replaced with text.
Such cases should be kept to a minimum for accessibility reasons. If there is even a small possibility of providing actual alternative text, omitting the alt attribute or using an empty value ("") is not recommended.
width Attribute
Specifies the image width in CSS pixels. The value must be a unitless integer.
<img src="earth.jpg" alt="A round, blue Earth glowing in space" width="150">
- If the
widthattribute is not set, the image is displayed at its original size. - If both the
widthandheightattributes are set, the image is displayed at the specified dimensions without preserving its aspect ratio. This may distort the image. - If only the
widthattribute is set and theheightattribute is not set, the image is resized to the specified width while maintaining its original aspect ratio.
When the HTML width attribute and the CSS width property are both used, CSS takes precedence. CSS has a higher priority than the HTML width attribute, so when the image width is specified in CSS, the HTML width attribute is ignored.
height Attribute
Specifies the image height in CSS pixels. The value must be a unitless integer.
<img src="earth.jpg" alt="A round, blue Earth glowing in space" height="50">
- If the
heightattribute is not set, the image is displayed at its original size. - If both the
widthandheightattributes are set, the image is displayed at the specified dimensions without preserving its aspect ratio. This may distort the image. - If only the
heightattribute is set and thewidthattribute is not set, the image is resized to the specified height while maintaining its original aspect ratio.
When the HTML height attribute and the CSS height property are both used, CSS takes precedence. CSS has a higher priority than the HTML height attribute, so when the image height is specified in CSS, the HTML height attribute is ignored.
decoding Attribute
Indicates the preferred method for decoding the image.
Here, "image decoding in the browser" refers to the process of loading and displaying images on a web page. The browser decodes image files, converts them into pixel data, and displays them on the screen.
The values of the decoding attribute are as follows.
auto |
The default value.
The browser determines the most appropriate decoding mode for the user. |
|---|---|
sync |
Decodes the image synchronously so that it can be displayed along with other content. This means that page loading may be delayed until the image is loaded. |
async |
Decodes the image asynchronously to avoid delaying the display of other content. This means that image loading does not affect page loading. |
<img src="earth.jpg" alt="A round, blue Earth glowing in space" decoding="async">
In particular, setting the decoding attribute to async offers the following benefits.
- Improved page loading speed: It can reduce the impact of image loading on page loading speed, especially on pages with many images.
- Improved user experience: Content is displayed before image loading is complete, allowing users to start interacting with the page sooner.
crossorigin Attribute
The crossorigin attribute configures Cross-Origin Resource Sharing (CORS) so that image resources from an origin other than that of the current web page can be used.
In general, the crossorigin attribute is not required when specifying an image URL from another origin in the src attribute of an <img> tag. However, when an image from another origin inserted this way is drawn onto a <canvas> element, the browser may restrict operations such as accessing the canvas's pixel data under the same-origin policy. This is because drawing an image from another origin without CORS validation causes the canvas to become tainted (a "tainted canvas").
In this case, using the crossorigin attribute allows an image from another origin to be drawn onto a <canvas> element and its pixel data to be accessed. For this to work, the server must respond with an appropriate CORS header (Access-Control-Allow-Origin).
See MDN - Use cross-origin images in a canvas for more information.
srcset and sizes Attributes
Let's explore the concepts and usage of the srcset and sizes attributes, which allow you to automatically adjust and implement responsive images using the HTML <img> tag based on the browser's viewport width and resolution.
For detailed explanations and examples, see HTML <img> srcset and sizes Attributes – A Clear and Simple Guide.
title Attribute
The title attribute can be used to provide supplementary information about an image. This information may include the image's source or a description of the image.
<img src="example.jpg" alt="Example image" title="Supplementary information about the example image.">
Considerations When Using the title Attribute
- The
titleattribute cannot replace thealtattribute. - Avoid using the value of the
altattribute as the value of thetitleattribute. This may cause screen readers to announce the same content twice unnecessarily, which can confuse users. - Do not use the
titleattribute to provide additional explanations of the description in thealtattribute. If an image needs a caption or description, use the<figure>and<figcaption>tags. - Use the
titleattribute sparingly, only when important supplementary information about the image is needed.
usemap Attribute
Specifies the image map associated with the image represented by this tag.
Specify the value of the name attribute of the <map> tag corresponding to the image map associated with this tag as the value of the usemap attribute, preceded by the # symbol.
<!-- The actual image used for the image map -->
<img src="example.jpg" alt="example" usemap="#example-map"> <!-- Connect the <map> tag using the usemap attribute -->
<!-- Define the image map -->
<map name="example-map"> <!-- Assign a name to the image map using the name attribute -->
...
</map>
loading Attribute
The loading attribute indicates how the browser should load an image. The purpose of this attribute is to specify the loading policy for images outside the viewport.
The viewport is a web browser term that refers to the portion of a document currently visible in the browser window (or on the screen when viewing the document in full-screen mode). It is the area in which a web page is displayed.
The values of the loading attribute are as follows.
eager |
The default value.
Loads the image immediately, regardless of whether it is inside the viewport. |
|---|---|
lazy |
Defers image loading until the image reaches a browser-defined distance from the viewport. Deferring image loading in this way can improve performance in typical use cases by reducing network and storage bandwidth usage until the image is likely to be needed. |
fetchpriority Attribute
The fetchpriority attribute provides a hint about the relative priority that the browser calculates and assigns to an image when fetching it, compared with other resources on the page. The browser takes this hint into account when calculating the image's priority.
The values of the fetchpriority attribute are as follows.
auto |
The default value.
Does not specify a fetch priority preference. Allows the browser to determine the fetch priority automatically. |
|---|---|
high |
Fetches the image with a higher priority than other images. |
low |
Fetches the image with a lower priority than other images. |
Deprecated Attributes
The following attributes were once available specifically for this tag.
They have since been removed from the specification and are non-standard. They are no longer used and should not be used.
Since they still appear in many learning resources and existing code, they are listed below for reference only.
Browsers may still support these attributes, but they may not work consistently for all users.
There may be significant compatibility differences between implementations, and their behavior may change in the future.
alignborderhspacelongdescnameframerulesvspace
Technical Summary
| Permitted parents | Any element that accepts embedded content. |
|---|---|
| Permitted content | None; it is a void element. |
Implicit ARIA role |
|
Permitted ARIA roles |
|
Browser Compatibility
| Tag and Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<img>
|
1 | 12 | 1 | 1 |
src
|
1 | 12 | 1 | 1 |
alt
|
1 | 12 | 1 | 3 |
width
|
1 | 12 | 1 | 3 |
height
|
1 | 12 | 1 | 3 |
decoding
|
65 | 79 | 63 | 11.1 |
crossorigin
|
13 | 12 | 8 | 6 |
srcset
|
34 | 18 | 38 | 8 |
sizes
|
38 | 12 | 38 | 9.1 |
title
|
1 | 12 | 1 | 4 |
loading
|
77 | 79 | 75 | 15.4 |
fetchpriority
|
101 | 101 | 132 | 17.2 |
Specifications
| Specification | |
|---|---|
<img>
|
HTML Standard #the-img-element |