Advertisement

Google Ad Slot: content-top

Image

~1 min read · HTML
On this page

In HTML, images are embedded in a webpage using the <img> tag.

Syntax

<img src="URL" alt="Description of the image">
Example 1 : Image tag
whereisstuff.com
Try it yourself

Common Attributes of the <img> Tag

src:

  • The src attribute defines the path to the image file.

alt:

  • The alt attribute provides alternative text for the image, which is important for screen readers used by visually impaired users. It also appears if the image cannot be displayed.
  • Example: alt="A picture of a mountain at sunset"

width and height:

  • These attributes specify the width and height of the image in pixels. You can define either one or both.
  • Example: width="300" or height="200"

title:

  • The title attribute provides additional information about the image, often displayed as a tooltip when the user hovers over the image.
  • Example: title="Sample Image"

loading:

  • The loading attribute allows you to defer the loading of off-screen images until the user scrolls near them (lazy loading), improving performance.
  • Values: lazy (default), eager.
  • Example: loading="lazy"

srcset and sizes:

  • These attributes help with responsive images by allowing you to define multiple image sources for different screen sizes or resolutions.
  • srcset: Specifies different images for different screen resolutions.
  • sizes: Specifies which image size to use based on the display size.



Example 1 : Image with attributes
A beautiful mountain landscape A responsive image of a mountain
Try it yourself