Images make websites attractive, interactive, and visually appealing. Almost every modern website uses images for:
Logos
Banners
Products
Icons
Advertisements
Galleries
Backgrounds
HTML provides the <img> tag to display images on webpages.
In this article, we will learn:
HTML image tag
Image attributes
Image paths
Image sizes
Alternative text
Image links
Background images
Image mapping
Responsive images
Practical examples
What is an HTML Image?
An HTML image is a picture displayed on a webpage using the <img> tag.
The <img> tag is an empty tag, meaning it does not require a closing tag.
Syntax of HTML Image
<img src="image.jpg" alt="Sample Image">
Important Attributes of <img>
| Attribute | Purpose |
|---|---|
src | Specifies image path |
alt | Alternative text |
width | Image width |
height | Image height |
title | Tooltip text |
1. The src Attribute
The src attribute specifies the image location.
Example:
<img src="flower.jpg">
Browser loads and displays the image file.
2. The alt Attribute
The alt attribute displays text if the image cannot load.
Example:
<img src="flower.jpg" alt="Beautiful Flower">
Why alt is Important
Improves accessibility
Helps visually impaired users
Useful when image fails to load
Improves SEO
Search engines like Google analyze alt text for image understanding.
3. Width and Height
These attributes control image size.
Example:
<img src="flower.jpg" width="300" height="200">
Complete Example Program
<!DOCTYPE html>
<html>
<head>
<title>HTML Images</title>
</head>
<body>
<h1>My Image</h1>
<img src="nature.jpg"
alt="Nature Image"
width="400"
height="250">
</body>
</html>
Image File Formats Supported by HTML
| Format | Extension |
|---|---|
| JPEG | .jpg, .jpeg |
| PNG | .png |
| GIF | .gif |
| SVG | .svg |
| WebP | .webp |
JPEG Images
Best for:
Photographs
Large colorful images
Example:
photo.jpg
PNG Images
Best for:
Transparent backgrounds
Logos
Icons
Example:
logo.png
GIF Images
Best for:
Simple animations
Example:
animation.gif
SVG Images
Best for:
Scalable vector graphics
Logos
Icons
Example:
icon.svg
Image Paths in HTML
There are two main types of image paths.
| Path Type | Description |
|---|---|
| Relative Path | Local file location |
| Absolute Path | Full internet URL |
Relative Path Example
<img src="images/photo.jpg">
Image is stored inside the website folder.
Absolute Path Example
<img src="https://example.com/photo.jpg">
Image loads from the internet.
Folder Structure Example
Project Folder
│
├── index.html
└── images
└── flower.jpg
HTML code:
<img src="images/flower.jpg">
Image Title Attribute
The title attribute shows tooltip text.
Example:
<img src="flower.jpg"
title="Beautiful Flower">
Tooltip appears when mouse hovers over image.
Image as a Link
Images can work as clickable links.
Example:
<a href="https://www.youtube.com">
<img src="youtube.png" width="200">
</a>
This creates a clickable image linking to YouTube.
Animated GIF Example
<img src="funny.gif" width="300">
GIF images automatically animate.
HTML Figure and Figcaption
HTML5 introduced semantic image tags.
| Tag | Purpose |
|---|---|
<figure> | Image container |
<figcaption> | Image caption |
Example
<figure>
<img src="mountain.jpg" width="400">
<figcaption>
Beautiful Mountain View
</figcaption>
</figure>
Advantages of Figure Tag
Better structure
Improved accessibility
Professional webpage design
Background Images in HTML
Images can be used as webpage backgrounds.
Example:
<body background="background.jpg">
However, modern websites usually use CSS for background images.
Responsive Images
Responsive images automatically adjust according to screen size.
Example:
<img src="nature.jpg"
width="100%">
The image adjusts according to browser width.
Importance of Responsive Images
Necessary for:
Mobile phones
Tablets
Laptops
Smart TVs
Companies like Amazon and Netflix use responsive images extensively.
Lazy Loading Images
Lazy loading improves website speed.
Example:
<img src="photo.jpg" loading="lazy">
Images load only when needed.
Benefits of Lazy Loading
Faster webpage loading
Reduced internet usage
Better performance
HTML Image Map
Image maps create clickable areas inside images.
Example
<img src="computer.jpg"
usemap="#workmap">
<map name="workmap">
<area shape="rect"
coords="34,44,270,350"
href="computer.html">
</map>
Common Shapes in Image Maps
| Shape | Description |
|---|---|
| rect | Rectangle |
| circle | Circle |
| poly | Polygon |
Complete Practical Program
<!DOCTYPE html>
<html>
<head>
<title>HTML Images</title>
</head>
<body>
<h1>Welcome to My Gallery</h1>
<figure>
<img src="nature.jpg"
alt="Nature"
width="400"
title="Nature View">
<figcaption>
Beautiful Nature
</figcaption>
</figure>
<br>
<a href="https://www.google.com">
<img src="google.png"
width="200">
</a>
</body>
</html>
Common Mistakes Beginners Make
1. Wrong File Path
Wrong:
<img src="flower.jp">
Correct:
<img src="flower.jpg">
2. Forgetting alt Attribute
Wrong:
<img src="photo.jpg">
Better:
<img src="photo.jpg" alt="Student Photo">
3. Using Very Large Images
Large images slow websites.
Always optimize image size.
4. Distorted Images
Wrong size combinations distort images.
Bad example:
<img src="photo.jpg"
width="500"
height="50">
Image Optimization Tips
Compress large images
Use modern formats like WebP
Use proper dimensions
Use lazy loading
Use responsive design
Advantages of HTML Images
Makes websites attractive
Improves user engagement
Enhances visual communication
Helps branding
Improves understanding
Real-Life Uses of Images
Images are used in:
Social media
E-commerce websites
Educational platforms
News websites
Blogs
Online advertisements
Platforms like Instagram, Pinterest, and Facebook rely heavily on images.
Conclusion
HTML images are essential for modern webpage design. They improve appearance, communication, and user experience.
Comments
Post a Comment