HTML Tags

HTML <img> Tag

The <img> tag in HTML is used to embed images into a webpage. It is an empty tag (no closing tag) and requires attributes like src (source URL of the image) and alt (alternative text for accessibility and SEO).

Syntax

html

<img src="image.jpg" alt="Description of image">

Attributes

AttributeDescriptionExample
srcSpecifies the path to the image file.src="photo.png"
altProvides alternate text if image fails to load.alt="Profile picture"
widthDefines the image width (in px or %).width="300"
heightDefines the image height (in px or %).height="200"
loadingDefines how the image should load (lazy or eager).loading="lazy"
titleProvides extra info on hover.title="HTML5 Logo"
srcsetDefines multiple image sources for responsive design.srcset="img-400.jpg 400w, img-800.jpg 800w"
sizesSpecifies how different image sizes are used for screen widths.sizes="(max-width: 600px) 480px, 800px"
crossoriginIndicates if image should be fetched with CORS.crossorigin="anonymous"

Example

html

<h2>Example: Adding an Image</h2>
<img src="https://picsum.photos/seed/picsum/400/300" alt="Beautiful sunrise over the mountains" width="400" height="250">

Output

Browser Output

html

Please use our TryIt Editor to see the output

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

(Displays an image of sunrise on the webpage - width 400px, height 250px)

Notes

  • The <img> tag does not have a closing tag.
  • Always include the alt attribute - it improves accessibility and SEO.
  • Use loading="lazy" for better performance (defers loading until the image is visible).
  • Use responsive image techniques (srcset, sizes) for adaptive layouts.

Conclusion

The <img> tag is the backbone of visual content in web design. When used correctly with attributes like alt, srcset, and loading, it makes websites both beautiful and efficient - ensuring faster loading, better SEO, and accessibility compliance.