HTML Tutorial

HTML Tag: <img>

Usage of <img>

The <img> tag creates an image element on a web page. It references an image file external to the HTML document and displays it at the specified location.

Attributes of <img>

Attribute Description
src Specifies the URL or file path of the image
alt Provides alternative text for the image, important for accessibility
height Sets the height of the image in pixels
width Sets the width of the image in pixels
class Assigns a CSS class to the image for styling

Examples with <img>

  • Display an image with default dimensions:
<img src="image.jpg">
  • Set image dimensions:
<img src="image.jpg" height="200" width="300">
  • Provide alternative text for accessibility:
<img src="image.jpg" alt="A photo of a cat">

HTML Example: Exploring the <img> Tag

<!DOCTYPE html>
<html>
<body>
  <img src="cat.jpg" alt="A cute cat">
</body>
</html>

This code will create an image element on the web page that displays the image named "cat.jpg". If the image cannot be loaded or is hidden, the alternative text "A cute cat" will be displayed instead, making the content accessible to users with disabilities.