🎵 HTML Multimedia Elements

Complete Guide to HTML Audio, Video, Canvas, and SVG

Student: Majharul Islam | ID: 232002256 | Date: October 2025

🎵 HTML Multimedia Overview

What are HTML Multimedia Elements?

HTML multimedia elements allow you to embed audio, video, graphics, and other interactive content directly into web pages without requiring external plugins.

Multimedia Element Types

Element Purpose Description
<audio> Audio Content Embeds audio files and streams
<video> Video Content Embeds video files and streams
<canvas> Graphics Drawing Dynamic graphics using JavaScript
<svg> Vector Graphics Scalable vector graphics
<iframe> Embedded Content Embeds external web pages
<embed> Plugin Content Embeds external applications
<object> Object Embedding Generic object embedding

🎵 Audio Element

Basic Audio
<audio controls> <source src="audio.mp3" type="audio/mpeg"> <source src="audio.ogg" type="audio/ogg"> Your browser does not support the audio element. </audio>
Audio with Attributes
<audio controls autoplay loop muted preload="metadata"> <source src="music.mp3" type="audio/mpeg"> </audio>

Audio Attributes

Attribute Description Values
controls Shows audio controls Boolean
autoplay Starts playing automatically Boolean
loop Repeats audio continuously Boolean
muted Mutes audio by default Boolean
preload How to load audio auto, metadata, none
src Audio file URL URL

🎬 Video Element

Basic Video
<video controls> <source src="video.mp4" type="video/mp4"> <source src="video.webm" type="video/webm"> Your browser does not support the video element. </video>
Video with Attributes
<video controls width="640" height="360" poster="thumbnail.jpg" preload="metadata"> <source src="movie.mp4" type="video/mp4"> </video>

Video Attributes

Attribute Description Values
controls Shows video controls Boolean
autoplay Starts playing automatically Boolean
loop Repeats video continuously Boolean
muted Mutes video by default Boolean
poster Image shown before video plays URL
width Video width in pixels Number
height Video height in pixels Number
preload How to load video auto, metadata, none

🎨 Canvas Element

Basic Canvas

Your browser does not support the canvas element.
<canvas id="myCanvas" width="300" height="150"> Your browser does not support the canvas element. </canvas> <script> const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); // Draw a rectangle ctx.fillStyle = '#f1c40f'; ctx.fillRect(10, 10, 100, 50); // Draw text ctx.fillStyle = '#2c3e50'; ctx.font = '16px Arial'; ctx.fillText('Hello Canvas!', 130, 35); // Draw a circle ctx.beginPath(); ctx.arc(150, 100, 30, 0, 2 * Math.PI); ctx.fillStyle = '#e74c3c'; ctx.fill(); </script>

Canvas Drawing Methods

Method Description Example
fillRect() Draws filled rectangle ctx.fillRect(x, y, width, height)
strokeRect() Draws rectangle outline ctx.strokeRect(x, y, width, height)
fillText() Draws filled text ctx.fillText(text, x, y)
strokeText() Draws text outline ctx.strokeText(text, x, y)
arc() Draws arc/circle ctx.arc(x, y, radius, start, end)
lineTo() Draws line to point ctx.lineTo(x, y)
moveTo() Moves to point ctx.moveTo(x, y)

🖼️ SVG Element

Basic SVG

Hello SVG!
<svg width="300" height="150" xmlns="http://www.w3.org/2000/svg"> <!-- Rectangle --> <rect x="10" y="10" width="80" height="50" fill="#f1c40f" stroke="#2c3e50" stroke-width="2"/> <!-- Circle --> <circle cx="150" cy="35" r="25" fill="#e74c3c" stroke="#2c3e50" stroke-width="2"/> <!-- Line --> <line x1="200" y1="10" x2="280" y2="60" stroke="#27ae60" stroke-width="3"/> <!-- Text --> <text x="10" y="100" font-family="Arial" font-size="16" fill="#2c3e50">Hello SVG!</text> <!-- Path --> <path d="M 10 120 Q 150 100 290 120" stroke="#9b59b6" stroke-width="2" fill="none"/> </svg>

SVG Elements

Element Description Attributes
<rect> Rectangle shape x, y, width, height
<circle> Circle shape cx, cy, r
<ellipse> Ellipse shape cx, cy, rx, ry
<line> Line shape x1, y1, x2, y2
<polyline> Connected line segments points
<polygon> Closed shape points
<path> Complex shapes d (path data)
<text> Text element x, y, font-family, font-size

🖼️ Iframe Element

Basic Iframe

<iframe src="https://example.com" width="100%" height="300" title="Example Website" frameborder="0"> Your browser does not support iframes. </iframe>

Iframe Attributes

Attribute Description Values
src URL of embedded content URL
width Width of iframe Number or percentage
height Height of iframe Number or percentage
title Title for accessibility Text
frameborder Border around iframe 0 or 1
sandbox Security restrictions allow-same-origin, allow-scripts, etc.
loading Loading behavior lazy, eager

📁 Media Formats

Supported Audio Formats

Format MIME Type Browser Support Description
MP3 audio/mpeg Excellent Most widely supported
OGG audio/ogg Good Open source format
WAV audio/wav Good Uncompressed audio
WebM audio/webm Good Modern web format

Supported Video Formats

Format MIME Type Browser Support Description
MP4 video/mp4 Excellent Most widely supported
WebM video/webm Good Modern web format
OGV video/ogg Limited Open source format

📱 Responsive Media

Responsive Video

<!-- Responsive Video Container --> <div class="video-container"> <video controls> <source src="video.mp4" type="video/mp4"> Your browser does not support the video element. </video> </div> <style> .video-container { position: relative; width: 100%; height: 0; padding-bottom: 56.25%; /* 16:9 aspect ratio */ } .video-container video { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style>

Responsive Iframe

<!-- Responsive YouTube Embed --> <div class="iframe-container"> <iframe src="https://www.youtube.com/embed/VIDEO_ID" title="YouTube video" frameborder="0" allowfullscreen> </iframe> </div> <style> .iframe-container { position: relative; width: 100%; height: 0; padding-bottom: 56.25%; /* 16:9 aspect ratio */ } .iframe-container iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style>

♿ Multimedia Accessibility

✅ Accessibility Best Practices:

  • Alt Text: Provide alternative text for images and graphics
  • Captions: Include captions for video content
  • Transcripts: Provide transcripts for audio content
  • Controls: Ensure media controls are keyboard accessible
  • Descriptions: Provide descriptions for visual content
  • Fallbacks: Include fallback content for unsupported media

Accessible Video Example

<video controls poster="video-poster.jpg"> <source src="video.mp4" type="video/mp4"> <source src="video.webm" type="video/webm"> <track kind="captions" src="captions.vtt" srclang="en" label="English captions" default> <track kind="descriptions" src="descriptions.vtt" srclang="en" label="English descriptions"> <p>Your browser does not support the video element. <a href="video.mp4">Download the video</a> instead.</p> </video>

✅ Multimedia Best Practices

✅ Good Practices:

  • Optimize File Sizes: Compress media files for web delivery
  • Multiple Formats: Provide multiple format options
  • Fallback Content: Include fallback for unsupported browsers
  • Responsive Design: Make media responsive to screen size
  • Loading Optimization: Use appropriate preload settings
  • Accessibility: Ensure media is accessible to all users

❌ Common Mistakes:

  • Large Files: Don't use uncompressed media files
  • Missing Fallbacks: Always provide fallback content
  • Poor Accessibility: Don't ignore accessibility requirements
  • No Controls: Provide user controls for media playback
  • Fixed Sizes: Don't use fixed pixel sizes for responsive design

🌍 Real-World Multimedia Examples

Music Player

<div class="music-player"> <h3>Background Music</h3> <audio controls loop> <source src="background-music.mp3" type="audio/mpeg"> <source src="background-music.ogg" type="audio/ogg"> Your browser does not support the audio element. </audio> <p>Relaxing background music for your website</p> </div>

Video Tutorial

<div class="video-tutorial"> <h3>HTML Tutorial Video</h3> <video controls poster="tutorial-thumbnail.jpg"> <source src="html-tutorial.mp4" type="video/mp4"> <source src="html-tutorial.webm" type="video/webm"> <track kind="captions" src="tutorial-captions.vtt" srclang="en" label="English"> <p>This video demonstrates HTML basics. <a href="tutorial-transcript.txt">Read the transcript</a>.</p> </video> </div>

Interactive Chart

<div class="chart-container"> <h3>Sales Data Visualization</h3> <canvas id="salesChart" width="400" height="200"> <p>Your browser does not support canvas. <a href="sales-data.csv">View data as CSV</a>.</p> </canvas> </div> <script> const canvas = document.getElementById('salesChart'); const ctx = canvas.getContext('2d'); // Draw a simple bar chart const data = [120, 150, 180, 200, 160]; const colors = ['#f1c40f', '#e74c3c', '#27ae60', '#3498db', '#9b59b6']; data.forEach((value, index) => { ctx.fillStyle = colors[index]; ctx.fillRect(50 + index * 60, 150 - value, 40, value); }); </script>