Introduction
HTML media elements allow you to add audio and video content to your web pages. This helps make websites more interactive and engaging for users.
Media elements are commonly used for music players, tutorials, video streaming, and presentations.
In this lesson, you will learn how to add audio and video in HTML using built-in tags.
What You’ll Learn
- HTML
<audio>tag - HTML
<video>tag - Media attributes
- Supported formats
- Best practices
HTML Audio Element
The <audio> tag is used to add sound or music to a webpage.
Basic Audio Example
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
Your browser does not support audio.
</audio>
Attributes:
controls→ Play, pause, volumeautoplay→ Plays automaticallyloop→ Repeats audiomuted→ Starts muted- Fallback text → Shown if browser doesn’t support audio
Audio Formats
Common supported formats:
- MP3
- WAV
- OGG
👉 MP3 is most widely supported across browsers.
HTML Video Element
The <video> tag is used to play video files.
Basic Video Example
<video width="400" controls>
<source src="video.mp4" type="video/mp4">
Your browser does not support video.
</video>
Common Attributes:
<video width="400" controls poster="thumb.jpg">
Video Formats
Common video formats:
- MP4
- WebM
- OGG
👉 MP4 is most commonly used.
Multiple Sources (Best Practice)
You can provide multiple formats for better browser support.
<video controls>
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
</video>
Browser chooses the best supported format.
Embedding External Video
You can also embed videos from platforms like YouTube.
<iframe width="400" height="250"
src="https://www.youtube.com/embed/VIDEO_ID"
title="YouTube video">
</iframe>
👉 This avoids hosting large files on your server.
Important Media Attributes
Controls
Shows play/pause buttons.
<audio controls>
Autoplay
Starts media automatically.
<video autoplay>
⚠️ Use carefully (can annoy users)
Loop
Repeats media continuously.
<audio loop>
Muted
Mutes audio by default.
<video muted>
Poster Attribute
The poster attribute is used to display an image before the video starts playing.
<video width="400" controls poster="thumbnail.jpg">
<source src="video.mp4" type="video/mp4">
</video>
👉 It works like a thumbnail for videos.
Preload Attribute
The preload attribute tells the browser how to load media files.
<audio controls preload="auto">
Values:
auto→ Loads entire mediametadata→ Loads only basic infonone→ Does not preload
👉 Helps improve performance.
Controls List
You can control which buttons appear in media controls.
<video controls controlsList="nodownload">
👉 Prevents download option in some browsers.
Adding Subtitles
Use the <track> tag to add subtitles to videos.
<video controls>
<source src="video.mp4" type="video/mp4">
<track src="subtitles.vtt" kind="subtitles" srclang="en" label="English">
</video>
👉 Improves accessibility and user experience.
Media Optimization
Large media files can slow down your website.
👉 Tips:
- Compress audio/video files
- Use modern formats like MP4 and WebM
- Avoid very large file sizes
- Use thumbnails for videos
How Media Appears in Browser
- Audio shows control bar
- Video shows player with controls
- Layout depends on browser
Complete Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Media</title>
</head>
<body><h2>Audio Example</h2>
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
</audio><h2>Video Example</h2>
<video width="400" controls>
<source src="video.mp4" type="video/mp4">
</video></body>
</html>
Important Notes
- Always include
controls - Provide fallback text
- Use supported formats
- Optimize file size for faster loading
Common Mistakes
- Missing
<source>tag - Using unsupported formats
- Overusing autoplay
- Large file sizes
Best Practices
- Use MP3 for audio and MP4 for video
- Provide multiple formats
- Keep media files optimized
- Avoid autoplay unless necessary
- Always add subtitles for better accessibility
- Use
posterfor better UI - Avoid autoplay with sound
- Use external hosting for heavy videos
Real-Life Uses
- Music players
- Video tutorials
- Online courses
- Product demos
Practice Tasks:
- Task 1: Add an audio file to a webpage.
- Task 2: Embed a video with controls.
- Task 3: Use autoplay and loop attributes.
- Task 4: Add multiple sources to a video.
- Task 5: Create a page with both audio and video.
FAQs
What is the <audio> tag?
It is used to add sound to a webpage.
What is the <video> tag?
It is used to embed videos.
Which format is best for video?
MP4 is the most widely supported format.
Conclusion
HTML media elements make it easy to add audio and video to your web pages. By using <audio> and <video> tags along with attributes like controls, autoplay, poster, and <track>, you can create rich and interactive user experiences.
Media is widely used in modern websites such as online courses, product demos, and entertainment platforms.
👉 With this, you have completed the HTML basics. Now you are ready to move to CSS to design and style your web pages.
