Image Tag (<img>)

Definition

The <img> tag is used to display images on a webpage.

  • It is an empty tag (no closing tag).
  • Images make webpages more attractive and informative.

Basic Syntax

<img src="image.jpg" alt="Image description">

Important Image Attributes

1. src (Source)

Specifies the path of the image.

<img src="photo.jpg">

2. alt (Alternate Text)

Displayed when the image fails to load and used by screen readers.

<img src="logo.png" alt="Company Logo">

3. width and height

Used to control image size.

<img src="pic.jpg" width="200" height="150">

4. title

Shows text when mouse is hovered.

<img src="img.png" title="Hover text">

Image Path Types

Relative Path

<img src="images/photo.jpg">

Absolute Path

<img src="https://example.com/photo.jpg">

Example

<img src="nature.jpg" alt="Nature Image" width="300">

Important Points

  • <img> is a self-closing tag
  • alt attribute is mandatory (best practice)
  • Images do not break lines automatically