Code has been added to clipboard!

HTML Image Tag

Reading time 2 min
Published Jun 29, 2017
Updated Jan 21, 2020

HTML Image Tag: Main Tips

  • HTML <img> tag defines an image in an HTML page.
  • To link an image to another document, use the <img> inside <a> tags.

Use and Purpose of img

HTML <img> tag sets an area for an image. The img scr attribute is required (usually a link).

Tip: the img element only needs an opening tag.

Example
<img src="image.png" alt="Space Doggo" width="50" height="50">

This image above was added with the following code, including the HTML image tag.

Example
<img src="image.png" alt="Doggo" width="205" >

Note: images are not inserted into an HTML page, but linked to the HTML page.

Attributes to Use

alt

It indicates an alternative text for an image when it cannot be displayed.

Example
<img src="image.png" alt="Space Doggo" width="50" height="50">

crossorigin

It specifies that images are from third-party websites, allowing cross-origin access.

Example
<img src="image.png" alt="Lemon" crossorigin="anonymous">

height

It defines image height in pixels.

Example
<img src="image.png" alt="Doggo" height="60">

longdesc

It adds an URL to a detailed description of an image.

Example
<img src="image.png" alt="Doggo" width="205" height="205" longdesc="#description">

src

img src sets the URL of an image.

Example
<img src="image.png" alt="Doggo" width="205">

width

It defines image width in pixels.

Example
<img src="image.png" alt="Doggo" width="100">

Deprecated Attributes

align

It defines the alignment of an image according to surrounding elements. Not supported in HTML5.

You should use CSS instead. Learn how to position elements in the Horizontal & Vertical Align tutorial.

Example
<img src="image.png" alt="Doggo" align="middle">

border

It defines border width around an image. Not supported in HTML5.

Instead, use CSS border property. Learn how in the CSS Borders tutorial.

Example
<img src="image.png" alt="Doggo with a 5 pixels border" border="5">

hspace

It creates a whitespace on the left and the right side of an image. Not supported in HTML5.

Instead, use CSS margin property. Learn how in the CSS Margins tutorial.

Example
<img src="image.png" alt="Doggo" hspace="20">

vspace

It creates a whitespace on top and bottom of an image. Not supported in HTML5.

Instead, use CSS margin property. Learn how in the CSS Margins tutorial.

Example
<img src="image.png" alt="Doggo" vspace="50">

Browser support

Chrome
All
Edge
All
Firefox
All
IE
All
Opera
All
Safari
All

Mobile browser support

Chrome
All
Firefox
All
Opera
All
Safari
All
Basics
Introduction
Syntax
Editors
Basic Examples
Head Section
<!DOCTYPE>
Tags and Elements
Semantic Elements
Tags Reference
Attributes
Comments
Block and Inline Elements
Forms
Form Elements
Input
Responsive Web Design
Inline Scripts
Uniform Resource Locator
Redirect
XHTML
Geolocation
Drag and Drop
Local Storage
Web Workers
Server-Sent Events
Character Encoding
Text Formatting
Quotation and Citation Elements
Headings
Paragraphs
Links
Tables
Lists
Symbols
Space
Tab
Styles
Computer Code
Layout
Classes
Colors
Images
iframes
Audio Player
Video Player
YouTube Videos
Multimedia
Canvas
SVG
<!-- -->
<a>
<abbr>
<acronym> DEPRECATED
<address>
<applet> DEPRECATED
<article>
<aside>
<audio>
<b>
<base>
<basefont> DEPRECATED
<bdi>
<bdo>
<big> DEPRECATED
<blink> DEPRECATED
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center> DEPRECATED
<cite>
<code>
<col>
<colgroup>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir> DEPRECATED
<div>
<dl>
<dt>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font> DEPRECATED
<footer>
<form>
<frame> DEPRECATED
<frameset> DEPRECATED
<h1> – <h6>
<head>
<header>
<hr>
<html>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<keygen> DEPRECATED
<label>
<legend>
<li>
<link>
<main>
<map>
<mark>
<menu>
<menuitem> DEPRECATED
<meta>
<meter>
<nav>
<noframes> DEPRECATED
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<pre>
<progress>
<q>
<rp>
<rt>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<small>
<source>
<span>
<strike> DEPRECATED
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt> DEPRECATED
<u>
<ul>
<var>
<video>
<wbr>