HTML Images
Master the art of adding visual content to your web pages with HTML images!
What are HTML Images?
The <img> tag is used to embed images in web pages. It's a self-closing tag that pulls images from external sources.
šÆ Why Images Matter:
- šļøVisual Appeal - Make content engaging and memorable
- šContent Enhancement - Show what words can't describe
- āæAccessibility - alt text helps screen readers
- šSEO Benefits - Images can appear in search results
Image Formats
š Basic Image Syntax
The <img> Tag
<img src="image.jpg" alt="Description">Self-Closing Tag
The <img> tag doesn't need a closing tag - it's self-contained!
External Reference
Images are loaded from external files, not embedded in HTML
Live Example
<img src="landscape.jpg" alt="Beautiful landscape with mountains and lake">š§ Image Attributes Explained
šÆ Required Attributes
src (Source)
Specifies the path to the image file
src="images/photo.jpg"alt (Alternative Text)
Provides text description for accessibility
alt="A red apple on a wooden table"ā” Optional Attributes
width & height
Specifies image dimensions in pixels
width="400" height="300"loading
Controls when the image loads
loading="lazy"š Visual Attribute Guide
Attribute Explorer
Live Demonstration
src Attribute
<img src="images/photo.jpg" ...>What it does:
The image file path or URL
Example:
src="images/photo.jpg"
Importance:
Required
š How Browsers Process Images
Browser Processing Journey
Follow how the browser processes an image from HTML to screen
HTML Parsing
Browser reads the HTML and encounters the <img> tag
Source Resolution
Browser determines the image file location
HTTP Request
Browser sends request to fetch the image file
Download & Decode
Image data is downloaded and processed
Rendering
Image is displayed on the page
Accessibility
Alt text is available for screen readers
Step 1: HTML Parsing
HTML parser identifies the image element and its attributes
<img src="photo.jpg" alt="Description">What happens:
Browser reads the HTML and encounters the <img> tag
Browser Action:
Creates DOM element and collects attributes
Complete Processing Flow
š” Real-World Examples
Basic Image
<img src="landscape.jpg" alt="Mountain landscape at sunset">Simple image with descriptive alt text
With Dimensions
<img src="photo.jpg" alt="Portrait" width="400" height="600">Image with specified width and height
Lazy Loading
<img src="banner.jpg" alt="Promotional banner" loading="lazy">Image loads when it enters viewport
External URL
<img src="https://example.com/image.jpg" alt="External image">Image loaded from external source
With CSS Class
<img src="avatar.jpg" alt="User avatar" class="rounded-full">Image with CSS styling class
Decorative Image
<img src="divider.png" alt="">Empty alt for decorative images
āæ Accessibility & Best Practices
šÆ Writing Good Alt Text
ā Do's
- āBe descriptive but concise
- āInclude text that's in the image
- āDescribe the purpose, not just content
- āUse empty alt for decorative images
ā Don'ts
- āStart with "image of" or "picture of"
- āUse filenames as alt text
- āWrite overly long descriptions
- āSkip alt attribute entirely
š Alt Text Examples
Descriptive & Concise
alt="Golden retriever playing fetch in the park"
Too Vague
alt="Dog" (What kind? What is it doing?)
Functional Context
alt="Download PDF file" (for download button image)
Filename as Alt
alt="IMG_0234.jpg" (Not helpful for users)
Empty for Decorative
alt="" (For purely decorative images)
Missing Alt
No alt attribute (Poor accessibility)
š® Image Playground
Configure Your Image
š” Try These URLs:
Live Preview & Code
Generated HTML Code:
<img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4" alt="Snowy mountain landscape under blue sky" width="400" height="300" loading="eager" >
Current Settings:
Size: 400Ć300px
Loading: eager
Accessibility:
ā Alt text provided
ā” Performance & Optimization
Right Size & Format
Use appropriate dimensions and modern formats like WebP for better performance.
Lazy Loading
Use loading="lazy" for images below the fold to improve initial page load.
Dimensions
Always specify width and height to prevent layout shifts and improve CLS.
š Performance Best Practices:
- ⢠Compress images without losing quality
- ⢠Use responsive images with srcset
- ⢠Implement lazy loading for off-screen images
- ⢠Consider using CDN for image delivery
- ⢠Specify dimensions to prevent layout shifts