The essentials
Quick reference
One focused task per row. Jump to the related section for complete, working examples.
| Use | Syntax | Examples |
|---|---|---|
| Embed controlled video | <video controls width="960" height="540" src="demo.mp4">
</video> | View examples |
| Embed controlled audio | <audio controls src="episode.mp3"></audio> | View examples |
| Show a poster image | <video controls poster="poster.jpg" src="demo.mp4">
</video> | View examples |
| Offer alternate formats | <source src="demo.webm" type="video/webm"> | View examples |
| Provide legacy fallback content | <video controls>
<a href="demo.mp4">Download the video</a>
</video> | View examples |
| Add captions | <track
kind="captions"
src="captions-en.vtt"
srclang="en"
label="English"
default> | View examples |
| Add translated subtitles | <track
kind="subtitles"
src="subtitles-pt.vtt"
srclang="pt-BR"
label="Português"> | View examples |
| Link a transcript | <a href="transcript.html">Read the transcript</a> | View examples |
| Hint metadata loading | <video controls preload="metadata" src="demo.mp4">
</video> | View examples |
| Defer media loading | <audio controls preload="none" src="episode.mp3">
</audio> | View examples |
| Request inline playback | <video controls playsinline src="demo.mp4"></video> | View examples |
| Request muted autoplay | <video autoplay muted playsinline src="ambient.mp4">
</video> | View examples |
| Size video responsively | video { max-inline-size: 100%; block-size: auto; } | View examples |
| Reserve media space | video { aspect-ratio: 16 / 9; inline-size: 100%; } | View examples |
| Pause with JavaScript | document.querySelector('video').pause(); | View examples |
| Request playback | await video.play(); | View examples |
| Seek to a time | video.currentTime = 30; | View examples |
Native media elements provide keyboard-aware controls and format negotiation without a custom player. Supply controls, dimensions, text alternatives, and honest loading hints; treat autoplay as an exceptional enhancement and never make essential information available only through sound or moving images.
Step by step
Detailed examples
Start with native, user-controlled playback
controls exposes the browser's playback UI and should be the default unless an equally accessible custom interface is fully implemented. Width and height attributes reserve the video's intrinsic ratio before loading; CSS can then scale it. A poster should represent the content and must not contain essential text unavailable elsewhere.
<video controls width="960" height="540" poster="poster.jpg" src="demo.mp4">
<a href="demo.mp4">Download the demonstration video</a>
</video>
<audio controls src="episode.mp3">
<a href="episode.mp3">Download the episode audio</a>
</audio> Offer sources in preference order
When src is absent on the media element, source children provide candidates in document order. A correct type lets the browser skip formats it knows it cannot decode without fetching them, though codec support can vary within a container. Put source elements before track elements. Child fallback content serves user agents without video-element support; place a normal link outside the element when modern users also need a dependable download option.
<video controls width="960" height="540" poster="poster.jpg">
<source src="demo.webm" type="video/webm">
<source src="demo.mp4" type="video/mp4">
<a href="demo.mp4">Download the MP4 video</a>
</video>
<p><a href="demo.mp4" download>Download the video</a></p> Provide captions, subtitles, and transcripts
Captions include dialogue plus meaningful sounds for people who cannot hear the audio; subtitles primarily translate dialogue. A subtitles track requires srclang, and labels help users choose among tracks. WebVTT text tracks do not replace a well-structured transcript, which supports reading at another pace and can include descriptions of important visual information.
<figure>
<video controls width="960" height="540">
<source src="interview.webm" type="video/webm">
<track kind="captions" src="captions-en.vtt" srclang="en" label="English" default>
<track kind="subtitles" src="subtitles-pt.vtt" srclang="pt-BR" label="Português">
</video>
<figcaption><a href="interview-transcript.html">Read the interview transcript</a></figcaption>
</figure> Treat loading and autoplay as hints, not guarantees
preload is a browser hint: metadata is a balanced default for many videos, while none helps large media lists avoid eager fetching. Autoplay is commonly blocked and can be disruptive even when muted; never depend on it for essential content. If motion starts automatically, provide an obvious way to pause it and respect reduced-motion preferences in the surrounding experience.
<video controls playsinline preload="metadata" width="960" height="540" src="lesson.mp4"></video>
<audio controls preload="none" src="episode.mp3"></audio> <video autoplay muted loop playsinline preload="metadata" poster="ambient.jpg" aria-hidden="true">
<source src="ambient.webm" type="video/webm">
</video> Note: Do not use aria-hidden when the media communicates information.
Reserve space and scale without distortion
HTML dimensions communicate the resource's intended aspect ratio and reduce layout movement. CSS max-inline-size and block-size:auto let a video shrink proportionally, while aspect-ratio can reserve space for a known ratio. Avoid forcing unrelated width and height values that stretch the picture, and ensure native controls remain large enough to operate.
.media-frame {
max-inline-size: 60rem;
margin-inline: auto;
}
.media-frame video {
display: block;
inline-size: 100%;
block-size: auto;
aspect-ratio: 16 / 9;
background: #000;
} Enhance playback through the media API
HTMLMediaElement exposes play, pause, currentTime, duration, volume, and events. play returns a promise because playback can be rejected by policy or fail during loading; handle that result instead of assuming success. Custom controls must retain names, focus order, keyboard operation, state communication, and caption access, so native controls are usually safer.
<video id="lesson" controls preload="metadata" src="lesson.mp4"></video>
<button type="button" id="replay">Replay from the start</button>
<script>
const video = document.querySelector('#lesson');
document.querySelector('#replay').addEventListener('click', async () => {
video.currentTime = 0;
try {
await video.play();
} catch (error) {
console.warn('Playback was not started', error);
}
});
</script> Local code tester
Try HTML media markup
Edit a native media figure with fallback text and a transcript link; substitute local media URLs to test playback.
Sources and further reading
References
Authoritative documentation used to verify and expand this cheat sheet.
Help us improve
Found a typo or missing example?
Tell us what would make this cheat sheet clearer, more complete, or more useful.



