Metadata
- Date :: 13-04-2025
- Tags :: web-dev
Notes
πΌοΈ HTML Image Element (<img>) β Complete Notes with Explanation and Examples
π Why Images Matter in Web Development
A website with no images would be quite dull and lifeless. Images help:
-
Visually communicate ideas and moods.
-
Enhance aesthetics and engagement.
-
Provide context to content.
-
Increase user interest and reduce bounce rates.
-
Contribute to branding and storytelling.
Hence, learning how to properly use images is a core part of HTML and web development.
π§± 1. Introducing the <img> Element
The <img> element is used to embed images in a web page.
π€ Basic Syntax:
<img src="image-url" alt="description" />β Key Characteristics:
-
It is a void (self-closing) HTML element.
-
Does not require a closing tag like
</img>(because it doesnβt contain content inside it). -
Must have at least the
srcattribute, and should have analtattribute for accessibility.
π§© 2. Understanding Attributes of <img>
πΉ src (Required)
-
Stands for: Source
-
Purpose: Tells the browser where to find the image file.
-
Can be:
-
A URL to an online image (e.g.
https://example.com/image.jpg) -
A relative path to an image stored locally (e.g.
images/photo.jpg)
-
πΉ alt (Highly Recommended)
-
Stands for: Alternative Text
-
Purpose:
-
Describes the image content for screen readers.
-
Helps visually impaired users understand the image.
-
Is shown when an image fails to load due to network or file issues.
-
-
Always write meaningful alt text, unless the image is decorative.
β Example:
<img src="https://picsum.photos/200" alt="A random placeholder image" />This will show a random 200x200 px image from the picsum.photos service.
π― What is a βVoidβ Element?
Void (self-closing) elements do not have a closing tag. They exist alone.
Examples of void elements:
-
<img />β for images -
<br />β line break -
<hr />β horizontal rule -
<input />β form input field
<img src="..." alt="..." />Since an image has no βtext content,β thereβs no reason for a closing tag.
π¨ Image Display in Browser
Once rendered in the browser:
-
The image appears visually using the source defined in the
srcattribute. -
The alt text is not visible, but acts as fallback content and for accessibility.
-
If the image file is missing or broken, the browser shows the alt text instead.
πΈ Using Placeholder Images
π§° Tool: Picsum.photos
-
Itβs like βLorem Ipsumβ but for photos.
-
Provides random placeholder images.
-
Great for mockups, drafts, and UI layout without needing real assets.
π‘ Example Usage:
<img src="https://picsum.photos/300/300" alt="Placeholder image" />-
Displays a random 300x300 image every time the page reloads.
-
Ideal for learning or rapid prototyping.
πΆ Challenge Activity: Are You a Cat or Dog Person?
π― Task:
Create a mini web page with:
-
An
<h1>that says βIβm a dog personβ or βIβm a cat personβ -
An image (
<img>) showing a dog or a cat -
Add proper
alttext to the image
π‘ Code Example (Dog Person):
<h1>I'm a dog person</h1>
<img src="https://example.com/dog.gif" alt="A happy puppy digging in the sand" />-
srccan be a GIF too! GIFs display animated content. -
Image will autoplay in the browser just like regular images.
-
Makes your site feel more dynamic and fun.
π± Code Example (Cat Person):
<h1>I'm a cat person</h1>
<img src="https://example.com/cat.jpg" alt="A calm cat sitting on a couch" />-
Here, the image is a static JPEG.
-
Reflects the laid-back nature of cats πΎ.
π§βπ¦― 3. Accessibility and the alt Attribute
π§ Why is alt Important?
-
Helps screen readers describe images to blind or visually impaired users.
-
Makes your website inclusive and user-friendly.
-
Required for ADA compliance and WCAG accessibility standards.
π§ Screen Reader Example:
-
Screen reader might read:
βImage: A dolphin leaping from the sea.β
π Tool Mentioned: Silktide Toolbar
-
Chrome extension that simulates a screen reader experience
-
Helps developers understand how accessible their site is
-
Used in this lesson to show alt text in action
π οΈ Best Practices for Using <img>
| Tip | Why Itβs Important |
|---|---|
β
Always use alt | Accessibility and fallback content |
| β Use meaningful descriptions | Helps screen readers |
| β Optimize image size | Improves load time |
| β Use correct file paths | Avoid broken images |
β
Use responsive sizes (CSS or width/height) | Better mobile experience |
β
Use placeholder services like picsum.photos during development | Saves time |
β Summary Table
| Property | Description | Required |
|---|---|---|
<img> | Embeds an image into a webpage | Yes |
src | URL or path of the image | β Yes |
alt | Alternative description of image | β οΈ Strongly recommended |
| Self-closing? | Yes (<img />) | β |
π Example: Full Mini Project
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cat or Dog?</title>
</head>
<body>
<h1>I'm a dog person</h1>
<img src="https://media.giphy.com/media/l0MYt5jPR6QX5pnqM/giphy.gif" alt="Excited puppy digging in the sand" />
</body>
</html>π§ Review Questions
-
What does the
srcattribute do in an<img>tag? -
Why is the
altattribute important? -
What is a void element in HTML?
-
Can the
<img>tag contain text between opening and closing tags? -
Name a tool to test how a screen reader reads your website.
π§ͺ Practice Task
Try creating:
-
A new HTML file
-
An
<h1>that says your pet preference -
An image using either a random
picsum.photosimage or a cute animal image -
Add meaningful
alttext -
Preview it in the browser
π Bonus: Image File Types
| Type | When to Use |
|---|---|
.jpg or .jpeg | Great for real-world photos |
.png | Supports transparency (logos/icons) |
.gif | Simple animations |
.webp | Modern format with better compression |