HTML Media
π§ Introduction to HTML Media Elements
HTML supports embedding media like audio π΅, video π¬, and images πΌοΈ directly into your webpages. Media tags in HTML5 make it easy to create rich, interactive web experiences without requiring plugins.
>>βA web without media is like a song without sound.β πΆ
πΊ Key Media Elements in HTML
- <audio> β Embeds sound/audio files
- <video> β Embeds video content
- <source> β Specifies multiple media file sources
- <track> β Adds subtitles/captions to videos
πΌ The <audio> Element
Use the <audio> tag to play audio files (like MP3, WAV). Controls allow the user to play/pause the sound.
Audio Tag Example
<audio controls>
<source src="sound.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>Note
π‘ Always include fallback text inside the tag for unsupported browsers.
πΉ The <video> Element
HTML5 introduced the <video> tag to play video files like MP4 or WebM. You can control playback using the controls attribute.
Video Tag Example
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>Adding Captions with <track>
Video with Subtitles
<video controls>
<source src="movie.mp4" type="video/mp4" />
<track src="subtitles.vtt" kind="subtitles" srclang="en" label="English" />
</video>Note
β οΈ Make sure your .vtt subtitle file is properly formatted.
π Using Multiple <source> Elements
The <source> tag lets browsers pick the first supported media file from a list. This enhances cross-browser compatibility.
Video with Multiple Sources
<video controls>
<source src="movie.mp4" type="video/mp4" />
<source src="movie.webm" type="video/webm" />
Your browser does not support the video tag.
</video>π Supported File Types
| Media | Formats |
|---|---|
| Audio | MP3, WAV, OGG |
| Video | MP4, WebM, OGG |
| Subtitles | VTT |
π§ Tips for Media Integration
- Use preload to specify media loading behavior (e.g., none, auto, metadata)
- Use autoplay and loop attributes carefully β they can affect user experience
- Use muted on autoplay videos to comply with browser policies
- Always include alternative text or fallback content
π Resources
>>βGreat websites donβt just inform β they play, speak, and show.β π