Lecture

img Tag

The <img> tag is used to insert images into web pages.

The main attributes of the img tag are src, alt, width, and height.


src Attribute

This is a required attribute that specifies the image's URL.

The URL can point to a different image on the same website or an image provided by an external website.

Using the <img> tag without the src attribute will not display any image.

img Tag src Attribute
<img src="https://example.com/myimage.jpg" />

alt Attribute

This attribute specifies alternative text for the image.

It's used as a fallback when the image fails to load or for screen readers to assist visually impaired users.

Though not mandatory, the alt attribute is essential for search engine optimization (SEO).

Setting Alternative Text
<img src="https://picsum.photos/300" alt="Sunset sea view" />

width and height

These attributes specify the width and height of the image, with the default unit being pixels (px).

Pixels refer to individual dots on a display screen. If width and height are not specified, the image is displayed at its original size.

Setting Image Size
<img src="https://picsum.photos/300" width="300" height="200" />

Using the img Tag

The img tag can be used as follows:

  1. Specify src and alt attributes
Basic Image Tag Usage
<img src="https://picsum.photos/300" alt="Profile picture" />

  1. Set image size with width and height
Setting Image Size
<img src="https://picsum.photos/300" alt="Random image" width="200" height="150" />
Mission
0 / 1

The <img> tag will display a fallback image if there is no src attribute with the image's URL.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

HTML
CSS
Loading...