Working with Images and Media
⏱️ 30 minutesVisual content makes websites engaging and memorable. Learn to add images, videos, and other multimedia elements that enhance user experience while maintaining accessibility and performance.
🎯 Learning Objectives
- Add images with proper attributes and accessibility
- Create responsive images for different screen sizes
- Embed audio and video content
- Understand image formats and optimization
- Implement lazy loading for better performance
- Follow media accessibility best practices
HTML Images
💡 The Image Element
The <img> element embeds images in web pages. It's a self-closing
tag that requires the src attribute to specify the image source.
<img src="image.jpg" alt="Description of the image">
<img src="photos/sunset.png" alt="Beautiful sunset over the ocean">
<img src="https://example.com/logo.svg" alt="Company logo">
Essential Image Attributes
src
Source Path
Required: URL or file path to the image
alt
Alternative Text
Required: Description for accessibility
width/height
Dimensions
Optional: Control image size
<img
src="photos/team-meeting.jpg"
alt="Team members collaborating in a modern office space"
width="600"
height="400"
title="Our team at work"
loading="lazy"
>
Image Formats Guide
📸 JPEG (.jpg)
- Best for photographs
- Good compression
- Lossy format
- No transparency
🎨 PNG (.png)
- Best for graphics with transparency
- Lossless compression
- Supports transparency
- Larger file sizes
📊 SVG (.svg)
- Vector graphics (scalable)
- Perfect for icons/logos
- Small file sizes
- Can be styled with CSS
🚀 WebP (.webp)
- Modern format, smaller files
- Better compression than JPEG
- Supports transparency
- Not supported in older browsers
Responsive Images
Responsive images adapt to different screen sizes and device capabilities for optimal performance and user experience.
Using the Picture Element
<picture>
<source srcset="image.webp" type="image/webp">
<source srcset="image.jpg" type="image/jpeg">
<img src="image.jpg" alt="Fallback image description">
</picture>
Responsive Images with Srcset
<img
src="image-800w.jpg"
srcset="
image-400w.jpg 400w,
image-800w.jpg 800w,
image-1200w.jpg 1200w
"
sizes="(max-width: 600px) 400px,
(max-width: 900px) 800px,
1200px"
alt="Responsive image example"
>
Art Direction with Picture
<picture>
<source media="(max-width: 600px)" srcset="mobile-hero.jpg">
<source media="(max-width: 1024px)" srcset="tablet-hero.jpg">
<img src="desktop-hero.jpg" alt="Hero image adapting to screen size">
</picture>
Audio and Video Elements
HTML5 Video
Video Implementation
Video Attributes
controls
Player Controls
Show play/pause, volume, progress bar
poster
Preview Image
Image shown before video plays
autoplay
Auto Play
Start playing automatically (use carefully)
Audio Element
<audio controls preload="metadata">
<source src="audio/song.mp3" type="audio/mp3">
<source src="audio/song.ogg" type="audio/ogg">
<source src="audio/song.wav" type="audio/wav">
<p>Your browser doesn't support HTML5 audio.
<a href="audio/song.mp3">Download the audio file</a>.</p>
</audio>
Media Accessibility
Alt Text Best Practices
✅ Good Alt Text
- "Team meeting in conference room"
- "Red sports car parked in driveway"
- "Chart showing 25% sales increase"
- "" (empty for decorative images)
❌ Poor Alt Text
- "Image" or "Photo"
- "DSC_001234.jpg"
- "Click here to see image"
- Very long descriptions
Figure and Figcaption
<figure>
<img src="chart.png" alt="Sales data visualization">
<figcaption>
Sales increased by 25% in Q3 2024 compared to the previous quarter
</figcaption>
</figure>
Video Accessibility
<video controls>
<source src="video.mp4" type="video/mp4">
<track kind="captions"
src="captions-en.vtt"
srclang="en"
label="English captions">
<track kind="subtitles"
src="subtitles-es.vtt"
srclang="es"
label="Spanish subtitles">
</video>
Performance Optimization
Lazy Loading
<!-- Images load only when needed -->
<img src="image1.jpg" alt="Description" loading="lazy">
<img src="image2.jpg" alt="Description" loading="lazy">
<img src="hero-image.jpg" alt="Description" loading="eager">
Image Optimization Checklist
🚀 Performance Tips
- Choose the right format: JPEG for photos, PNG for graphics, SVG for icons
- Compress images: Use tools like TinyPNG or ImageOptim
- Use appropriate dimensions: Don't load huge images for small displays
- Implement lazy loading: Improve initial page load times
- Consider WebP format: Better compression for modern browsers
- Use CDNs: Faster delivery through geographically distributed servers
Practice Exercises
Exercise 1: Photo Gallery
Create a responsive photo gallery:
- Grid layout with 6 images
- Proper alt text for each image
- Lazy loading implementation
- Figure and figcaption elements
Exercise 2: Media Portfolio
Build a multimedia portfolio page:
- Mix of images, video, and audio
- Responsive image implementation
- Video with multiple format support
- Accessible media descriptions
Lesson Summary
🎯 What You Learned
- Image elements require src and alt attributes
- Responsive images adapt to different screens
- HTML5 supports native audio and video
- Accessibility makes media usable for everyone
- Optimization improves performance and UX
📁 Image Formats
- JPEG
- Photographs, lossy compression
- PNG
- Graphics with transparency
- SVG
- Scalable vector graphics
- WebP
- Modern format, better compression
⚡ Performance
- Use loading="lazy" for images below the fold
- Provide multiple formats with picture element
- Optimize file sizes before uploading
- Use appropriate dimensions
- Consider using a CDN