HTML <img> Tag
<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 is written as
<img src="" alt=""> (no end tag) with the image URL inserted between the double quotes of 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.
<img src="myimage.jpg" alt="My Image">
You can also provide the image dimensions using the
<img src="myimage.jpg" alt="My Image" width="100px" height="50px">
Or you can provide the image dimensions using CSS.
<img src="myimage.jpg" alt="My Image" style="width:100px;height:50px;">
Basic tag usage
You can use the
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.
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
height attributes to set the dimensions of a large image.
Here are two ways of dealing with this situation.
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.
HTML 5.1 and the HTML Living Standard have introduced the
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.
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
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.
Depending on your resolution, and whether your browser supports the
srcset attribute, you should have seen one of the following images:
- If your browser doesn't support the
- 1x (one device pixel per CSS pixel): phi-phi-200x150.jpg
- 2x (two device pixels per CSS pixel): boracay-resort-1000x750.jpg
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
sizes attributes are still being worked out by the WHATWG and W3C and browser support is limited.
You can link your image to another web page by nesting it inside the
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
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:
srcset- This specifies images to use in different situations (e.g. high-resolution displays, small monitors, etc)
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.
<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="" >
Here are the official specifications for the
- HTML5 Specification (W3C)
- HTML Living Standard (WHATWG)
- Current W3C Draft (the next version that is currently being worked on)
- HTML 4 (W3C)
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.