Multimedia
HTML5 makes it easy to embed audio and video content directly into your web pages using the <audio>
and <video>
elements. This provides a standardized way to include multimedia without relying on external plugins.
Embedding Audio (<audio>
)
The <audio>
element is used to embed sound content in a document.
<audio controls src="audio/background-music.mp3">
Your browser does not support the audio element.
<a href="audio/background-music.mp3">Download the audio</a>
</audio>
Key Attributes for <audio>
:
controls
: A boolean attribute. When present, it displays the browser's default audio playback controls (play/pause button, volume control, seek bar, duration). If omitted, the audio element won't be visible unless you create custom controls using JavaScript.src
: Specifies the URL of the audio file to be embedded. You can use this if you only need to provide one audio format.autoplay
: A boolean attribute. If present, the audio will attempt to play automatically as soon as the page loads. Caution: Autoplaying audio (especially with sound enabled) is often disruptive and annoying to users. Many modern browsers block autoplay with sound unless the user has interacted with the page first, or if the audio ismuted
. Useautoplay
sparingly and consider settingmuted
alongside it if needed.loop
: A boolean attribute. If present, the audio will automatically restart from the beginning when it reaches the end.muted
: A boolean attribute. If present, the audio output will be muted initially.preload
: Hints to the browser about how the audio file should be loaded when the page loads.none
: Do not preload the file. Wait for the user to hit play.metadata
: Load only the file's metadata (e.g., duration, dimensions).auto
: (Default) The browser can preload the entire audio file, even if the user doesn't play it.
Providing Multiple Audio Formats (<source>
)
Different browsers support different audio file formats (like MP3, Ogg Vorbis, WAV, AAC). To maximize compatibility, you can provide multiple audio sources using the <source>
element nested inside the <audio>
tags. The browser will use the first <source>
format it supports.
<audio controls preload="metadata">
<source src="audio/track.ogg" type="audio/ogg">
<source src="audio/track.mp3" type="audio/mpeg">
Your browser does not support the audio element. Try downloading
<a href="audio/track.mp3">the MP3</a> or
<a href="audio/track.ogg">the Ogg</a> file.
</audio>
<source>
Element: An empty element specifying alternative media resources.src
Attribute (on<source>
): URL of the specific audio file format.type
Attribute (on<source>
): Crucial. Specifies the MIME type of the audio file. This allows the browser to quickly check if it supports the format without having to download it first. Common types:audio/mpeg
(for MP3),audio/ogg
(for Ogg Vorbis),audio/wav
,audio/aac
.
Fallback Content
Any content placed between the opening <audio>
and closing </audio>
tags (and after any <source>
elements) serves as fallback content. It will only be displayed by browsers that do not support the <audio>
element at all. This is typically a simple message and/or a direct link to download the audio file.
Embedding Video (<video>
)
The <video>
element works very similarly to <audio>
but is used for embedding video content.
<video controls width="640" height="360" poster="images/video-poster.jpg" preload="metadata">
<source src="videos/tutorial.mp4" type="video/mp4">
<source src="videos/tutorial.webm" type="video/webm">
<track kind="captions" src="videos/captions_en.vtt" srclang="en" label="English Captions" default>
Sorry, your browser doesn't support embedded videos.
You can <a href="videos/tutorial.mp4">download the video</a>.
</video>
Key Attributes for <video>
:
- Shares many attributes with
<audio>
:src
,controls
,autoplay
(with same cautions/restrictions),loop
,muted
,preload
. width
/height
: Specify the dimensions of the video player area in pixels. Setting these helps the browser reserve space, preventing layout shifts as the video loads.poster
: Specifies the URL of an image to display before the video loads or while it's downloading (like a thumbnail or splash screen).
Multiple Video Formats (<source>
)
Just like with audio, use nested <source>
elements with src
and type
attributes to provide different video formats (e.g., MP4, WebM) for broader browser compatibility. Common MIME types: video/mp4
, video/webm
, video/ogg
.
Text Tracks (<track>
) for Accessibility
The <track>
element is used inside <video>
(or <audio>
) to specify timed text tracks, such as subtitles, captions, descriptions, or chapters. This is essential for accessibility and can also benefit SEO.
kind
: Specifies the type of track:subtitles
: Transcription or translation of dialogue, suitable for when the audio is available but not understood (e.g., foreign language).captions
: Transcription of dialogue, sound effects, musical cues, and other relevant audio information, suitable for users who are deaf or hard-of-hearing, or when the video is muted.descriptions
: Textual descriptions of the visual content, for visually impaired users (read aloud by screen readers or synthesized).chapters
: Chapter titles for navigating the media resource.metadata
: Tracks used by scripts (not visible to user).
src
: The URL of the track file. The most common format is WebVTT (.vtt
).srclang
: The language of the track text (e.g.,en
,es
,ar
). Required ifkind
issubtitles
.label
: A user-readable title for the track that the browser can display in its interface (e.g., in a subtitles menu).default
: A boolean attribute. If present, specifies that this track should be enabled by default (unless user preferences indicate otherwise). Only one track perkind
per video can have thedefault
attribute.
<video controls width="720" height="480">
<source src="movie.mp4" type="video/mp4">
<source src="movie.webm" type="video/webm">
<track kind="captions" src="captions/en.vtt" srclang="en" label="English" default>
<track kind="subtitles" src="subtitles/es.vtt" srclang="es" label="Español">
<track kind="subtitles" src="subtitles/fr.vtt" srclang="fr" label="Français">
Your browser does not support the video tag.
</video>
Fallback Content
As with <audio>
, content placed between the opening <video>
and closing </video>
tags (after <source>
and <track>
elements) serves as fallback for browsers that don't support the <video>
element.