<img>
Tag
The <img>
tag is used to insert images into web pages.
Key Attributes
The key attributes of the <img>
tag are src
, alt
, width
, and height
.
src
The src
attribute specifies the URL of the image. It is a required attribute. The URL can point to an image within the same website or an image hosted on an external website. Without this attribute, the <img>
tag will not display an image.
```html title="The src
Attribute in the <img>
Tag"
<br /> ## `alt` The `alt` attribute provides alternative text for the image. It is used as a fallback when the image fails to load or to describe the image for screen readers, improving web accessibility. While not mandatory, it is essential for SEO (Search Engine Optimization) and accessibility enhancement. ```html title="Setting Alternative Text" <img src="https://picsum.photos/300" alt="A sunset view of the ocean" />
width
and height
These attributes define the width and height of the image. The default unit is pixels (px
).
<img src="https://picsum.photos/300" width="300" height="200" />
Usage Examples
- Using
src
andalt
```html title="Basic Usage of the <img>
Tag"
<br /> 2. Specifying Image Dimensions with `width` and `height` ```html title="Setting Image Dimensions" <img src="https://picsum.photos/300" alt="Random image" width="200" height="150" />
Summary of the <img>
Tag
-
Use the
<img>
tag to embed images into web pages. -
The
src
attribute specifies the URL of the image. -
The
alt
attribute provides a description or alternative text when the image is unavailable. -
Use the
width
andheight
attributes to adjust the image size.
The src
attribute in the HTML <img>
tag is optional.
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help