Description
HTML element <img>
is used to define an image in an HTML document, which improves the design, appearance, and user experience.
Image is not directly inserted into an HTML document, but it is linked using its URL.
It is an empty element, which doesn't need a closing tag.
The below table summarizes its usage.
Usage Details | |
Placement | It is displayed as an Inline element. |
Contents | None. It is an empty element. |
Tags | Only an opening tag is required. |
Versions | HTML 2, 3.2, 4, 4.01, 5 |
Syntax
Here is the basic syntax of the <img>
element.
- Attribute
src
defines the image location - Attribute
alt
defines an alternate text, which will be displayed when the image is not accessible
<img src="URL" alt="text">
Examples
Element <img>
linked an image using its reference path.
<img src="/assets/logo/logo.png" alt="randomcodez-logo">
Element <img>
linked an image using its absolute path.
<img src="https://randomcodez.com/assets/logo/logo.png" alt="randomcodez-logo">
Attributes
The following table shows the list of supported and unsupported attributes for the <img>
element.
Attribute Type | Details |
Element-Specific Attributes | The tags <img> has some element-specific attributes listed in the below table. |
Global Attributes | Like all other HTML tags, the tag <img> supports the HTML Global Attributes. |
Event Attributes | The tags <img> also supports the HTML Event Attributes. |
Here is a list of attributes that are specific to the <img>
element.
Attribute | Value | Description | Required? |
alt |
text | Provides an alternate text for an image. | Yes |
src |
URL | Specifies the URL of the image file to be displayed. | Yes |
align |
left |
Obsolete Specifies the alignment of an image with respect to the surrounding elements. |
No |
border |
pixels | Obsolete Specifies the width of the border around the image. |
No |
crossorigin |
|
Specifies how the element handles cross-origin requests. | No |
height |
length | Specifies the height of the image. | No |
hspace |
pixels | Obsolete Specifies the amount of whitespace on the left and right sides of an image. |
No |
ismap |
ismap |
This Boolean attribute defines that the image is part of a server-side map. | No |
longdesc |
URL | Obsolete Specifies a link to a long description of the image. |
No |
usemap |
URL | Obsolete Defines a client-side image map associated with the <img> element. |
No |
vspace |
pixels | Obsolete Specifies the amount of whitespace on the top and bottom sides of the image. |
No |
width |
length | Specifies the width of the image. | No |
Browser Compatibility
The tags <img>
is supported in all modern browsers.
- Google Chrome 1+
- Internet Explorer or Edge 2+
- Firefox 1+
- Apple Safari 1+
- Opera 2.1+