HTML <img> Tag

The HTML <img> tag is used for embedding images into an HTML document.

Before you use the <img> tag, you need to ensure that the image exists on the internet. You then reference the location of that image when using the <img> tag.

Syntax

The <img> tag is written as <img src="" alt=""> (no end tag) with the image URL inserted between the double quotes of the src attribute.

The alt attribute provides alternative text for users who are unable to view the image. Some reasons why users can't view the image might include having a slow Internet connection, they are vision impaired and are using text-to-speech software, the image won't load for some reason, etc. Note that the alt attribute is a required attribute.

Like this:

Copy to Clipboard
<img src="myimage.jpg" alt="My Image">

You can also provide the image dimensions using the width and height attributes.

Copy to Clipboard
<img 
	src="myimage.jpg" 
	alt="My Image"
	width="100px"
	height="50px">

Or you can provide the image dimensions using CSS.

Copy to Clipboard
<img 
	src="myimage.jpg" 
	alt="My Image"
	style="width:100px;height:50px;">

Examples

Basic tag usage

CodeResult

Photo of Ton Sai Bay on Koh Phi-Phi Island in Thailand

Using width and height

You can use the width and height attributes to provide the dimensions for the image.

In this example, we'll scale the image to be smaller (for demonstration purposes), however, this is not recommended. It's better to scale the image using image-editing software first (i.e. before it's uploaded to the internet) - so that it is the correct size to start with. Doing this reduces the file size. Scaling it using HTML does not reduce the file size - it uses the same (larger) file and simply resizes it in the browser.

CodeResult

Photo of Ton Sai Bay on Koh Phi-Phi Island in Thailand

Responsive Design

If your website needs to be displayed on multiple sized screens (eg, mobiles, tablets, desktops, laptops, etc) you may find that your images are too large for some devices. This will be particularly true if you use the width and height attributes to set the dimensions of a large image.

Here are two ways of dealing with this situation.

CSS

One trick is to use the CSS max-width property to set a maximum width for the image of 100%. By specifying a maximum width, you are not explicitly setting the actual width. You are simply telling the browser not to go any bigger than the width that you specify - 100%. In this case, the browser should still reduce the size of the image if 100% is too big for the screen.

Like this:

CodeResult

Photo of Ton Sai Bay on Koh Phi-Phi Island in Thailand

The srcset & sizes Attributes

HTML 5.1 and the HTML Living Standard have introduced the srcset and sizes attributes. These are designed to deal with the issue of multiple sized screens and resolutions.

Note that at the time of writing, browser support was extremely limited for these attributes.

The srcset Attribute

The srcset attribute accepts a comma-separated list of URLs combined with a pixel-density value. Each of these would normally represent the same image but at a different size.

You should still use the standard src attribute to cater for user agents that don't support the srcset attribute.

In this example, I've intentionally used a different image for each resolution. This makes it easier to distinguish which image is being displayed. This is for demonstration purposes only. In reality, you would use the same image but at different resolutions.

CodeResult

Photo of either Boracay beach, boats at Phi-Phi, or Boracay Resort

Depending on your resolution, and whether your browser supports the srcset attribute, you should have seen one of the following images:

The sizes Attribute

The sizes attribute specifies how large the image should render within the layout. You can use media queries to specify a list of width values to use, depending on the space available for the image. For example, sizes="(min-width: 36em) 33.3vw, 100vw".

At the time of writing, the srcset and sizes attributes are still being worked out by the WHATWG and W3C and browser support is limited.

Linked Image

You can link your image to another web page by nesting it inside the <a> tag.

Like this:

CodeResult

Photo of Ton Sai Bay on Koh Phi-Phi Island in Thailand

Image Maps

You can use the <img> tag along with the <area> and <map> tags to create an image map.

Image maps enables one image to link to multiple pages. You can specify different shapes for the "hotspot" area too.

The following example demonstrates this (click on each country to see where the link goes). For more information on image maps, see the <map> tag.

CodeResult

Map of Australia and New Zealand Australia New Zealand

Differences Between HTML 4 & HTML 5

HTML5 doesn't support the following attributes:

HTML5 (and the HTML Living Standard) introduced the following attribute:

HTML 5.1 and the HTML Living Standard introduced the following attributes:

To see more detail on the two versions see HTML5 <img> Tag and HTML4 <img> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <img> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

For more information on attributes for this tag, see HTML5 <img> Tag and HTML4 <img> Tag.

Copy to Clipboard
<img  
 alt=""
 source=""
 crossorigin=""
 usemap=""
 ismap=""
 width=""
 height=""
 
 accesskey=""
 class=""
 contenteditable=""
 contextmenu=""
 dir=""
 draggable=""
 dropzone=""
 hidden=""
 id=""
 itemid=""
 itemprop=""
 itemref=""
 itemscope=""
 itemtype=""
 lang=""
 spellcheck=""
 style=""
 tabindex=""
 title=""
 translate=""

 onabort=""
 onautocomplete=""
 onautocompleteerror=""
 onblur=""
 oncancel=""
 oncanplay=""
 oncanplaythrough=""
 onchange=""
 onclick=""
 onclose=""
 oncontextmenu=""
 oncuechange=""
 ondblclick=""
 ondrag=""
 ondragend=""
 ondragenter=""
 ondragexit=""
 ondragleave=""
 ondragover=""
 ondragstart=""
 ondrop=""
 ondurationchange=""
 onemptied=""
 onended=""
 onerror=""
 onfocus=""
 oninput=""
 oninvalid=""
 onkeydown=""
 onkeypress=""
 onkeyup=""
 onload=""
 onloadeddata=""
 onloadedmetadata=""
 onloadstart=""
 onmousedown=""
 onmouseenter=""
 onmouseleave=""
 onmousemove=""
 onmouseout=""
 onmouseover=""
 onmouseup=""
 onmousewheel=""
 onpause=""
 onplay=""
 onplaying=""
 onprogress=""
 onratechange=""
 onreset=""
 onresize=""
 onscroll=""
 onseeked=""
 onseeking=""
 onselect=""
 onshow=""
 onsort=""
 onstalled=""
 onsubmit=""
 onsuspend=""
 ontimeupdate=""
 ontoggle=""
 onvolumechange=""
 onwaiting=""
	>

Tag Details

For more details about the <img> tag, see HTML5 <img> Tag and HTML4 <img> Tag.

Specifications

Here are the official specifications for the <img> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.

Enjoy this page?