Modern HTML5 Features
⏱️ 40 minutesHTML5 revolutionized web development with powerful new features, APIs, and elements. Discover how to leverage these modern capabilities to create rich, interactive web applications without relying on external plugins.
🎯 Learning Objectives
- Understand HTML5's new capabilities and advantages
- Use modern HTML5 elements effectively
- Implement web storage for client-side data
- Work with HTML5 APIs and JavaScript integration
- Create graphics with Canvas and SVG
- Build progressive web applications with HTML5
HTML5 Revolution
💡 What's New in HTML5?
HTML5 introduced semantic elements, multimedia support, form enhancements, drawing capabilities, offline storage, and powerful APIs that enable desktop-like experiences in web browsers.
Key HTML5 Improvements
📱 Mobile First
- Touch-friendly input types
- Responsive design support
- Offline capabilities
- Performance optimizations
🎮 Rich Media
- Native audio/video support
- Canvas for dynamic graphics
- SVG integration
- WebGL 3D graphics
🔧 Enhanced Forms
- New input types (email, date, etc.)
- Built-in validation
- Placeholder text
- Form attributes
🌐 Web APIs
- Geolocation services
- Local storage
- Web workers
- WebSocket communication
New HTML5 Elements
Multimedia Elements
Progress and Meter Elements
<!-- Progress indicator -->
<label for="file-progress">File upload progress:</label>
<progress id="file-progress" value="32" max="100">32%</progress>
<!-- Measurement gauge -->
<label for="disk-usage">Disk usage:</label>
<meter id="disk-usage" value="0.6" min="0" max="1">60%</meter>
<!-- Battery level example -->
<meter value="75" min="0" max="100" optimum="80" high="60" low="20">
75% battery remaining
</meter>
Interactive Elements
Details and Dialog Elements
<!-- Collapsible content -->
<details>
<summary>Click to expand FAQ</summary>
<p>This content is hidden by default and shows when clicked.</p>
<p>Perfect for FAQs, help sections, and additional details.</p>
</details>
<!-- Modal dialog -->
<dialog id="myDialog">
<form method="dialog">
<h3>Confirm Action</h3>
<p>Are you sure you want to proceed?</p>
<button value="cancel">Cancel</button>
<button value="confirm">Confirm</button>
</form>
</dialog>
Time and Data Elements
Time and Data Elements
<!-- Time representation -->
<p>The event starts at <time datetime="2024-12-25T09:00">9:00 AM on Christmas</time></p>
<p>Posted <time datetime="2024-10-14" title="October 14, 2024">yesterday</time></p>
<!-- Machine-readable data -->
<p>The laptop costs <data value="1299.99">$1,299.99</data></p>
<p>Product weight: <data value="2.3">2.3 kg</data></p>
HTML5 APIs
HTML5 API Demonstrations
Web Storage
Local Storage vs Session Storage
💾 Local Storage
- Persists until manually cleared
- Shared across browser tabs
- Typically 5-10MB limit
- Perfect for user preferences
🔄 Session Storage
- Cleared when tab closes
- Isolated per browser tab
- Same storage limit as localStorage
- Good for temporary data
Web Storage API Usage
<script>
// Local Storage
localStorage.setItem('username', 'john_doe');
localStorage.setItem('preferences', JSON.stringify({
theme: 'dark',
language: 'en'
}));
// Retrieve data
const username = localStorage.getItem('username');
const prefs = JSON.parse(localStorage.getItem('preferences'));
// Session Storage
sessionStorage.setItem('cart', JSON.stringify([
{ id: 1, name: 'Laptop', price: 999 },
{ id: 2, name: 'Mouse', price: 29 }
]));
// Check if storage is supported
if (typeof(Storage) !== "undefined") {
// Storage supported
} else {
// No web storage support
console.log('Web Storage not supported');
}
// Storage events (listen for changes in other tabs)
window.addEventListener('storage', function(e) {
console.log('Storage changed:', e.key, e.newValue);
});
</script>
Graphics: Canvas vs SVG
Canvas for Raster Graphics
Canvas Drawing
<canvas id="myCanvas" width="400" height="300"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// Draw rectangle
ctx.fillStyle = '#3498db';
ctx.fillRect(10, 10, 150, 100);
// Draw circle
ctx.beginPath();
ctx.arc(200, 60, 50, 0, 2 * Math.PI);
ctx.fillStyle = '#e74c3c';
ctx.fill();
// Draw text
ctx.fillStyle = '#2c3e50';
ctx.font = '20px Arial';
ctx.fillText('Hello Canvas!', 10, 150);
// Animation loop
function animate() {
// Clear and redraw
ctx.clearRect(0, 0, canvas.width, canvas.height);
// ... drawing code
requestAnimationFrame(animate);
}
</script>
SVG for Vector Graphics
Inline SVG
<svg width="400" height="300" xmlns="http://www.w3.org/2000/svg">
<!-- Background -->
<rect width="100%" height="100%" fill="#ecf0f1"/>
<!-- Gradient definition -->
<defs>
<linearGradient id="blueGradient" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:#3498db;stop-opacity:1" />
<stop offset="100%" style="stop-color:#2980b9;stop-opacity:1" />
</linearGradient>
</defs>
<!-- Shapes -->
<rect x="20" y="20" width="120" height="80" fill="url(#blueGradient)" rx="10"/>
<circle cx="200" cy="60" r="40" fill="#e74c3c" opacity="0.8"/>
<polygon points="300,20 350,80 250,80" fill="#f39c12"/>
<!-- Text -->
<text x="20" y="150" font-family="Arial" font-size="18" fill="#2c3e50">
Scalable Vector Graphics
</text>
<!-- Animation -->
<circle cx="50" cy="200" r="10" fill="#27ae60">
<animate attributeName="cx" values="50;350;50" dur="3s" repeatCount="indefinite"/>
</circle>
</svg>
🖼️ Canvas (Raster)
- Pixel-based drawing
- Great for complex scenes
- Game development
- Image manipulation
- Better performance for animations
📐 SVG (Vector)
- Scalable without quality loss
- CSS and JavaScript controllable
- Perfect for icons and logos
- Accessible and SEO-friendly
- Smaller file sizes for simple graphics
Practice Exercises
Exercise 1: Progressive Web App
Build a simple note-taking app using HTML5 features:
- Local storage for saving notes
- Offline functionality
- File upload/download capabilities
- Responsive design with modern elements
Exercise 2: Interactive Dashboard
Create a data visualization dashboard:
- Canvas charts and graphs
- Geolocation for weather data
- Real-time updates using APIs
- Progress indicators and meters
Lesson Summary
🎯 What You Learned
- HTML5 brings powerful new capabilities to web development
- New elements enhance semantics and functionality
- APIs enable desktop-like web applications
- Storage solutions reduce server dependencies
- Graphics capabilities rival native applications
🛠️ Key APIs
- Geolocation
- Access user's geographic location
- Web Storage
- Client-side data persistence
- Canvas
- 2D and 3D graphics programming
- File API
- Access and manipulate user files
💡 Best Practices
- Always check for API support before using
- Provide fallbacks for older browsers
- Use progressive enhancement principles
- Optimize storage usage and performance
- Follow accessibility guidelines
- Consider user privacy and permissions