Contents
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.
This image above was added with the following code, including the HTML image tag.
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.
crossorigin
It specifies that images are from third-party websites, allowing cross-origin access.
height
It defines image height in pixels.
longdesc
It adds an URL to a detailed description of an image.
<img src="image.png" alt="Doggo" width="205" height="205" longdesc="#description">
src
img src
sets the URL of an image.
width
It defines image width in pixels.
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.
border
It defines border width around an image. Not supported in HTML5.
Instead, use CSS border
property. Learn how in the CSS Borders tutorial.
<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.
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.