The tag is used to embed an image in an HTML page. Images are not technically inserted into a web page; images are linked to web pages. The tag creates a holding space for the referenced image. The tag has two required attributes:
Note: Also, always specify the width and height of an image. If width and height are not specified, the page might flicker while the image loads. Tip: To link an image to another document, simply nest the tag inside an tag (see example below).
|
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Girl in a jacket" width="500" height="600">
Attribute | Value | Description |
---|---|---|
alt | text | Specifies an alternate text for an image |
crossorigin | anonymous use-credentials |
Allow images from third-party sites that allow cross-origin access to be used with canvas |
height | pixels | Specifies the height of an image |
ismap | ismap | Specifies an image as a server-side image map |
loading | eager lazy |
Specifies whether a browser should load an image immediately or to defer loading of images until some conditions are met |
longdesc | URL | Specifies a URL to a detailed description of an image |
referrerpolicy | no-referrer no-referrer-when-downgrade origin origin-when-cross-origin unsafe-url |
Specifies which referrer information to use when fetching an image |
sizes | sizes | Specifies image sizes for different page layouts |
src | URL | Specifies the path to the image |
srcset | URL-list | Specifies a list of image files to use in different situations |
usemap | #mapname | Specifies an image as a client-side image map |
width | pixels | Specifies the width of an image |
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42" style="vertical-align:bottom">
<img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42" style="vertical-align:middle">
<img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42" style="vertical-align:top">
<img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42" style="float:right">
<img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42" style="float:left">
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42" style="border:5px solid black">
See the Example
Example:
HTML
<p><img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42"> This is some text. This is some text. This is some text.</p>
<h2>Image with left and right margins</h2>
<p><img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:0px 50px">This is some text. This is some text. This is some text.</p>
See the Example
Example:
HTML
<p><img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42"> This is some text. This is some text. This is some text.</p>
<h2>Image with top and bottom margins</h2>
<p><img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:50px 0px">This is some text. This is some text. This is some text.</p>
See the Example
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Stickman" width="24" height="39">
<p>Insert an image from a web site:</p>
<img src="https://horje.com/avatar.png" alt="Lamp" width="32" height="32">
See The Example
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h1>Add a hyperlink to an image</h1>
<p><a href="https://horje.com">
<img src="https://horje.com/avatar.png" alt="Horje.com" width="100" height="132">
</a></p>
</body>
</html>
See the example
Example:
HTML
<img src="https://horje.com/uploads/demo/2024-02-03-15-39-40-screenshot_2024-02-03_at_21-36-38_workplace.jpg_(jpeg_image_400_%C3%97_379_pixels).png" alt="Workplace" usemap="#workmap" width="400" height="379">
<map name="workmap">
<area shape="rect" coords="34,44,270,350" alt="Computer" href="https://horje.com/learn/684/what-is-html-img-tag">
<area shape="rect" coords="290,172,333,250" alt="Phone" href="https://horje.com/learn/684/what-is-html-img-tag">
<area shape="circle" coords="337,300,44" alt="Cup of coffee" href="coffee.htm">
</map>
See the Example
Example:
HTML
<style>
img {
display: inline-block;
}
</style>
<p>Some text <img src="https://horje.com/avatar.png" alt="Smiley face" width="42" height="42"> some text.</p>
The required alt
attribute specifies an alternate text for an image, if the image cannot be displayed.
The alt
attribute provides alternative information for an image if a user for some reason cannot view it (because of slow connection, an error in the src
attribute, or if the user uses a screen reader).
Tip: To create a tooltip for an image, use the title
attribute!
<img alt="text">
Value | Description |
---|---|
text | Specifies an alternate text for an image.
Guidelines for the alt text:
|
An image with an alternate text specified.
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Girl in a jacket" width="500" height="600">
The height
attribute specifies the height of an image, in pixels.
Tip: Always specify both the height
and width
attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the image, and cannot reserve the appropriate space to it. The effect will be that the page layout will change during loading (while the images load).
Tip: Downsizing a large image with the height
and width
attributes forces a user to download the large image (even if it looks small on the page). To avoid this, rescale the image with a program before using it on a page.
<img height="pixels">
Value | Description |
---|---|
pixels | The height in pixels (e.g. height="100") |
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Girl in a jacket" width="500" height="600">
The ismap
attribute is a boolean attribute.
When present, it specifies that the image is part of a server-side image map (an image map is an image with clickable areas).
When clicking on a server-side image map, the click coordinates are sent to the server as a URL query string.
Note: The ismap
attribute is allowed only if the <img>
element is a descendant of an <a>
element with a valid href
attribute.
<img ismap>
Example:
HTML
<a href="/action_page.php">
<img src="https://horje.com/avatar.png" alt="Horje.com" width="100" height="132" ismap>
</a>
Add lazy loading to images below the fold:
he loading
attribute specifies whether a browser should load an image immediately or to defer loading of off-screen images until for example the user scrolls near them.
Tip: Add loading="lazy"
only to images which are positioned below the fold.
Value | Description |
---|---|
eager | Default. Loads an image immediately |
lazy | Defer loading of images until some conditions are met |
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h1>The img loading attribute</h1>
<!-- visible in viewport -->
<img src="https://horje.com/avatar.png" alt="Wedding" style="width:100%">
<img src="https://horje.com/avatar.png" alt="Rocks" style="width:100%">
<!-- off-screen images -->
<img src="https://horje.com/avatar.png" alt="Paris" style="width:100%" loading="lazy">
<img src="https://horje.com/avatar.png" alt="Nature" style="width:100%" loading="lazy">
<img src="https://horje.com/avatar.png" alt="Underwater" style="width:100%" loading="lazy">
<img src="https://horje.com/avatar.png" alt="Ocean" style="width:100%" loading="lazy">
<img src="https://horje.com/avatar.png" alt="Mountains" style="width:100%" loading="lazy">
</body>
</html>
The longdesc
attribute specifies a hyperlink to a detailed description of an image.
Several examples of how to use the longdesc attribute.
<img longdesc="string">
Value | Description |
---|---|
string |
A hyperlink to a detailed description of an image. Possible values:
|
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h1>The img longdesc attribute</h1>
<!-- The description is on the same page as the image -->
<img src="https://horje.com/avatar.png" alt="Horje.com" width="100" height="132" longdesc="#w3htmlExplained">
<!-- The description is in an external page -->
<img src="https://horje.com/avatar.png" alt="Horje.com" width="100" height="132" longdesc="w3html.txt">
<!-- The description is one of several within an external page -->
<img src="https://horje.com/avatar.png" alt="Horje.com" width="100" height="132" longdesc="http://example.com/desc#item3">
<!-- The description is included in a data:URI -->
<img src="https://horje.com/avatar.png" alt="Horje.com" width="100" height="132" longdesc="data:text/html;charset=utf-8;,%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3Ctitle%3EDescription%20of%20the%20Logo%3C/title%3E%3C/head%3E%3Cbody%3E%3Cp%3ESome%20description%20goes%20here%3C/body%3E%3C/html%3E">
<div id="w3htmlExplained">
<h2>Image https://horje.com/avatar.png</h2>
<p>Description of the image...</p>
</div>
</body>
</html>
Set the referrerpolicy for an image:
The referrerpolicy
attribute specifies which referrer information to use when fetching an image.
The numbers in the table specify the first browser version that fully supports the attribute.
<img referrerpolicy="no-referrer|no-referrer-when-downgrade|origin|origin-when-cross-origin|unsafe-url">
Value | Description |
---|---|
no-referrer | No referrer information is sent |
no-referrer-when-downgrade | Default. The referrer header will not be sent to origins without HTTPS |
origin | Sends the origin (scheme, host, and port) of the document |
origin-when-cross-origin | For cross-origin requests: Send only scheme, host, and port. For same-origin requests: Also include the path |
unsafe-url | Send origin, path and query string (but not fragment, password, or username). This value is considered unsafe |
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Some image" referrerpolicy="no-referrer">
An image with a height of 600 pixels and a width of 500 pixels:
The width
attribute specifies the width of an image, in pixels.
Tip: Always specify both the height
and width
attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the image, and cannot reserve the appropriate space to it. The effect will be that the page layout will change during loading (while the images load).
Tip: Downsizing a large image with the height
and width
attributes forces a user to download the large image (even if it looks small on the page). To avoid this, rescale the image with a program before using it on a page.
<img width="pixels">
Value | Description |
---|---|
pixels | The width in pixels (e.g. width="100") |
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Girl in a jacket" width="500" height="600">
The crossorigin attribute on an <img> tag specifies that CORS is supported when loading an image from a third party server or domain.
CORS is a standard mechanism used to retrieve files from other domains.
Example:
HTML
<img crossorigin="anonymous" src="https://horje.com/avatar.png" alt="Van Gogh, self-portrait">
An image is marked up as follows.
The required src
attribute specifies the URL of the image.
There are two ways to specify the URL in the src
attribute:
1. Absolute URL - Links to an external image that is hosted on another website. Example: src="https://horje.com/avatar.png".
Notes: External images might be under copyright. If you do not get permission to use it, you may be in violation of copyright laws. In addition, you cannot control external images; it can suddenly be removed or changed.
2. Relative URL - Links to an image that is hosted within the website. Here, the URL does not include the domain name. If the URL begins without a slash, it will be relative to the current page. Example: src="img_girl.jpg". If the URL begins with a slash, it will be relative to the domain. Example: src="/images/img_girl.jpg".
Tip: It is almost always best to use relative URLs. They will not break if you change domain.
Note: A broken link icon and the alt
text are shown if the browser cannot find the image.
<img src="URL">
Value | Description |
---|---|
URL | The URL of the image.
Possible values:
|
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Girl in a jacket" width="500" height="600">
The srcset attribute on an <img> tag specifies multiple image resources (URLs) for the img element.
Together with the sizes attribute they create responsive images that adjust according to browser conditions.
A srcset attribute on an <img> element.
Resizing the browser will adjust the image file used.
Example:
HTML
<img srcset="https://horje.com/avatar.png 120w,
https://horje.com/avatar.png 193w,
https://horje.com/avatar.png 278w"
sizes="(max-width: 710px) 120px,
(max-width: 991px) 193px,
278px">
An image map, with clickable areas.
The usemap
attribute specifies an image as a client-side image map (an image map is an image with clickable areas).
The usemap
attribute is associated with a <map>
element's name attribute, and creates a relationship between the <img>
and the <map>
.
Note: The usemap
attribute cannot be used if the <img>
element is a descendant of an <a>
or <button>
element.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h1>The map and area elements</h1>
<p>Click on the computer, the phone, or the cup of coffee to go to a new page and read more about the topic:</p>
<img src="https://horje.com/avatar.png" alt="Workplace" usemap="#workmap" width="400" height="379">
<map name="workmap">
<area shape="rect" coords="34,44,270,350" alt="Computer" href="computer.htm">
<area shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm">
<area shape="circle" coords="337,300,44" alt="Cup of coffee" href="coffee.htm">
</map>
</body>
</html>
An image with a height of 600 pixels and a width of 500 pixels:
The width
attribute specifies the width of an image, in pixels.
Tip: Always specify both the height
and width
attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the image, and cannot reserve the appropriate space to it. The effect will be that the page layout will change during loading (while the images load).
Tip: Downsizing a large image with the height
and width
attributes forces a user to download the large image (even if it looks small on the page). To avoid this, rescale the image with a program before using it on a page.
<img width="pixels">
Value | Description |
---|---|
pixels | The width in pixels (e.g. width="100") |
Example:
HTML
<img src="https://horje.com/avatar.png" alt="Girl in a jacket" width="200" height="300">
Read Full: | HTML Tag |
Category: | Web Tutorial |
Sub Category: | HTML Tag |
Uploaded: | 8 months ago |
Uploaded by: | Admin |
Views: | 13 |
Ref on: | View |