HTML img tag
Example
How to insert an image:
Definition and Usage
The <img>
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 <img>
tag creates a holding space for the referenced image.
The <img>
tag has two required attributes:
- src - Specifies the path to the image
- alt - Specifies an alternate text for the image, if the image for some reason cannot be displayed
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 <img>
tag inside an <a> tag (see example below).
Attributes
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 |
Global Attributes
The <img>
tag also supports the Global Attributes in HTML.
Event Attributes
The <img>
tag also supports the Event Attributes in HTML.
More Examples
Example
Align image (with CSS):
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle"/>
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:top"/>
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="float:right"/>
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="float:left"/>
Align image with CSS
vertical-align: bottom
This is some text. This is some text. This is some text. This is some text.
vertical-align: middle
This is some text. This is some text. This is some text. This is some text.
vertical-align: top
This is some text. This is some text. This is some text. This is some text.
float: right
This is some text. This is some text. This is some text. This is some text.
float: left
This is some text. This is some text. This is some text. This is some text.
Example
Add image border (with CSS):

Example
Add left and right margins to image (with CSS):

Example
Add top and bottom margins to image (with CSS):

Example
How to insert images from another folder or from another web site:
<img src="https://pinfinitys.com/images/Love.jpg" alt="Love" width="32" height="32"/>


Example
How to add a hyperlink to an image:
<img src="pi.gif" alt="PI.com" width="100" height="132"/>
</a>

Example
How to create an image map, with clickable regions. Each region is a hyperlink:
<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>
The map and area elements
Click on the computer, the phone, or the cup of coffee to go to a new page and read more about the topic:

Default CSS Settings
Most browsers will display the <img>
element with the following default values:
Example
display: inline-block;
}
Comments
Post a Comment