img-element

İmg Element

Example

How to insert an image:

<img src=”img_girl.jpg” alt=”Girl in a jacket” width=”500″ height=”600″>

More “Try it Yourself” examples below.

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

AttributeValueDescription
alttextSpecifies an alternate text for an image
crossoriginanonymous
use-credentials
Allow images from third-party sites that allow cross-origin access to be used with canvas
heightpixelsSpecifies the height of an image
ismapismapSpecifies an image as a server-side image map
loadingeager
lazy
Specifies whether a browser should load an image immediately or to defer loading of images until some conditions are met
longdescURLSpecifies a URL to a detailed description of an image
referrerpolicyno-referrer
no-referrer-when-downgrade
origin
origin-when-cross-origin
unsafe-url
Specifies which referrer information to use when fetching an image
sizessizesSpecifies image sizes for different page layouts
srcURLSpecifies the path to the image
srcsetURL-listSpecifies a list of image files to use in different situations
usemap#mapnameSpecifies an image as a client-side image map
widthpixelsSpecifies 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:bottom”>
<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”>

Example

Add image border (with CSS):

<img src=”smiley.gif” alt=”Smiley face” width=”42″ height=”42″ style=”border:5px solid black”>

Example

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

<img src=”smiley.gif” alt=”Smiley face” width=”42″ height=”42″ style=”vertical-align:middle;margin:0px 50px”>

Example

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

<img src=”smiley.gif” alt=”Smiley face” width=”42″ height=”42″ style=”vertical-align:middle;margin:50px 0px”>

Example

How to insert images from another folder or from another web site:

<img src=”/images/stickman.gif” alt=”Stickman” width=”24″ height=”39″>
<img src=”https://www.w3schools.com/images/lamp.jpg” alt=”Lamp” width=”32″ height=”32″>

Example

How to add a hyperlink to an image:

<a href=”https://www.w3schools.com”>
<img src=”w3html.gif” alt=”W3Schools.com” width=”100″ height=”132″>
</a>

Example

How to create an image map, with clickable regions. Each region is a hyperlink:

<img src=”workplace.jpg” 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>

Default CSS Settings

Most browsers will display the <img> element with the following default values:

Example

img {
display: inline-block;
}