HTML Tags – Image Tag


What is HTML Image <img> tag?

HTML <img> tag is used to display image on the web page.

Inserting images in the webpage improves the design and the appearance of a web page.

What are the attributes used for image tag?

There are four attributes used for image tag, listed below

1. src

2. alt

3. width

4. height

1. src

src attribute describes the source or path of the image.

This instructs the browser where to look for the image on the server.

2. alt

alt attribute defines an alternate text for the image.

alt attribute “value” describes the image in words.


alt attribute “value” is mainly used in SEO prospective, which will be discussed later.

alt attribute is optional param.

3. width

width attribute is used to specify the width to display the image.

width attribute is optional param.

4. height

height attribute is used to specify the height to display the image.

Usually this takes the height of <h3> as default.

HTML beginners tutorial !!!

Click to Learn More about – HTML Tutorial for beginners

How to insert an image in webpage ?

There are three ways to insert image in webpage, listed below

1. Insert image directly from the same folder.

2. Insert image from any of the sub-folder.

3. Insert image from server or website.

1. Simple image insert with src attribute

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Image tag</title>
        </head>
        <body>
                <img src="html-icon-copyright-img-frontpage.png">
        </body>
        </html>

Output :

2. Insert image with other attributes / Insert image directly from the same folder.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Image tag</title>
        </head>
        <body>
                <img src="html-icon-copyright-img-frontpage.png" alt="HTML Topics Tutorials" width="200px" height="400px">
        </body>
        </html>

Output :

3. Insert image from any of the sub-folder.


Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Image tag</title>
        </head>
        <body>
                <img src="../images/html-icon-copyright-img-frontpage.png" alt="HTML Topics Tutorials" width="200px" height="400px">
        </body>
        </html>

Output :

4. Insert image from server or website.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Image tag</title>
        </head>
        <body>
                <img src="https://secureservercdn.net/192.169.221.188/s30.e0b.myftpupload.com/wp-content/uploads/2020/06/core-java-copyright-img-1.png" alt="Core Java Topics Tutorials" width="200px" height="400px">
        </body>
        </html>

Output :

<img> Tag Attributes

HTML <img> tag support following specific attributes.

SnoAttributesValueDescription
1alttextSpecify an alternate text of an image.
2crossoriginanonymous
use-credentials
This attribute allow to load images from an Cross-0rigin access (Other sites).
3heightpixelsSpecify the height of an image.
4ismapismapSpecify an image as a server side image map.
5longdescURLSpecify an URL that contains details description of an image.
6srcURLSpecify an URL of an image.
7usemap#idnameSpecify an image as a client-side image-map.
8widthpixelsSpecify the width of an image.
9Following attributes has been removed in HTML5.
10alignleft
right
top
middle
bottom
Specify the alignment of an image according to parent element.
11borderpixelsSpecify the width of the border. Border display around an image.
12hspacepixelsSpecify the whitespace that added on left and right side of an image.
13vspacepixelsSpecify the whitespace that added on top and bottom of an image.


Global Attributes

HTML <img> tag support following global attributes.

SnoAttributesValueDescription
1idunique_nameDeclared unique id for an element.
2classclass_nameDeclared one or more classnames for an element.
3stylestylesCSS inline styles specify an element.
4titletitleSpecify extra details of element contain, this will display as a “tooltip” for an elements.


Event Attributes

HTML <img> tag support following event attributes.

SnoAttributesValueDescription
1onfocusscriptelement gets focus on object when script tobe run.
2onblurscriptelement lose the focus on object when scrip tobe run.
3onabortscriptelement gets aborted on object when script tobe run.
4onchangescriptelement gets anytime change on object when script tobe run.
5onbeforeunloadscriptelement gets unloaded on object when scrip tobe run.
6onclickscriptclicked on object when script tobe run.
7ondblclickscriptdouble click on object when script tobe run.
8onkeydownscriptkey is pressed when script tobe run.
9onkeypressscriptkey is pressed over element then released when script tobe run.
10onkeyupscriptkey is released over element when script tobe run.
11onmousedownscriptmouse button was pressed over an element when script tobe run.
12onmouseoutscriptmouse pointer release over an element when script tobe run.
13onmousemovescriptrun mouse pointer moved when script tobe run.
14onmouseoverscriptrun mouse pointer move over when script tobe run.
15onmouseupscriptmouse button is released when script tobe run.
16onresetscriptform has been reset when script tobe run.
17onselectscriptSelect some content when script tobe run.
18onsubmitscriptform has been submitted when script tobe run.
19onloadscriptobject has load when script tobe run.
20onchangescriptallow to change the object when script tobe run.
21onunloadscriptunload to the browser window when script tobe run.
22ondragscriptelement being dragged when script tobe run.
23ondragendscriptelement being stop dragged when script tobe run.
24ondragenterscriptelement being go target dragged when script tobe run.
25ondragleavescriptelement being leave to target dragged when script tobe run.
26ondragoverscriptelement being over to target dragged when script tobe run.
27ondragstartscriptelement being start dragged when script tobe run.
28ondropscriptelement being dropped when script tobe run.
29onerrorscriptelement error occurs when script tobe run.
30onmessagescriptelement message display when script tobe run.
31onerrorscriptelement error occurs when script tobe run.
32onmousewheelscriptmouse wheel will be rotate when script tobe run.
33onscrollscriptscrollbar is scroll when script tobe run.
34onresizescriptelement should be resize when script tobe run.
35onselectscriptall element content selected when script tobe run.
36onstoragescriptelement should be store in target when script tobe run.

Browser Compatibility

SnoBrowserSupport
1ChromeYes
2FirefoxYes
3EdgeYes
4OperaYes
5SafariYes
6Internet ExplorerYes