The HTML5 Power-Up: Mastering the <video>
and <audio>
Tags
Embed rich media directly into your site—no more relying on outdated plugins.
Before HTML5, embedding media like video or audio required complex, proprietary plugins such as Flash. The HTML5 specification changed this with two simple, powerful tags: **<video>
** and **<audio>
**. These tags allow modern browsers to natively handle media playback, ensuring a faster, more secure, and accessible experience for your users.
The `
The core structure of embedding a video is straightforward. The **src
** attribute points to the video file, and the **controls
** attribute is essential, as it adds the standard play/pause, volume, and fullscreen buttons.
<video width="640" height="360" controls>
<source src="path/to/my-video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
The text between the opening and closing tags (e.g., "Your browser...") is fallback content displayed only if the browser cannot render the video, a good practice for accessibility.
Common & Critical Attributes
Both the `
controls
: Displays the browser's built-in playback controls (highly recommended).autoplay
: Starts playback automatically (use sparingly, as it can annoy users). *Note: Browsers often block autoplay without the **muted
** attribute.*loop
: Automatically restarts the media when it finishes.preload
: Specifies how the browser should load the media when the page loads (e.g.,none
,metadata
, orauto
).
The `
The `
<audio controls>
<source src="path/to/my-audio.mp3" type="audio/mpeg">
<source src="path/to/my-audio.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
Pro Tip: Multiple Sources! Notice the two `
Comments
Post a Comment