What are Images in HTML?
Images are visual elements used to enhance the content of a webpage, making it more attractive, engaging, and informative. In HTML, images are added using the <img>
tag, which is a self-closing (empty) tag. The <img>
tag requires attributes such as src
to specify the image source and alt
to provide alternative text for accessibility. Additional attributes like width
, height
, title
, srcset
, and loading
allow developers to control size, responsiveness, performance, and user interaction, making images an integral part of modern web design.
Basic Image Tag in HTML
Syntax:
<img src="image.jpg" alt="Description of image">
- src → Path of the image file.
- alt → Alternate text shown if the image doesn’t load (important for SEO and accessibility).
Example:
<img src="nature.jpg" alt="Beautiful Nature Image">
Example with additional attributes:
<img src="nature.jpg" alt="Beautiful Nature" width="400" height="300" title="Nature Image">
How Images Are Added
Local Images
-
Images stored within your project folder.
-
Best practice: keep images in a dedicated folder like
images
orassets
.
Example :
<img src="images/photo.jpg" alt="Local Image Example">
External Images
-
Images loaded directly from the internet using a full URL.
-
Useful when using images from CDNs, stock photos, or external websites.
Example :
<img src="https://www.example.com/image.jpg" alt="External Image Example">
Importance of Images:
-
Enhance Visual Appeal: Breaks up large blocks of text and makes content more visually interesting.
-
Improve Engagement: Users are more likely to stay on pages with images.
-
Communicate Information Quickly: Complex ideas can be illustrated using diagrams, charts, or infographics.
-
Support Branding: Logos, banners, and promotional images help create brand identity.