The HTML5 Video Element and WebM
The HTML5 <video> element is the standard way to embed video directly in web pages — no plugins required. WebM is one of the best-supported formats for this element, making it an ideal choice for web-delivered video content.
Basic WebM Embed
The simplest way to embed a WebM file is:
<video src="video.webm" controls></video>
The controls attribute adds the browser's native playback controls (play/pause, volume, scrubber). Without it, the video has no UI — useful for background or decorative videos, but not for user-facing content.
Providing Multiple Sources for Browser Compatibility
Because Safari has limited WebM support, it's best practice to provide both a WebM and an MP4 source. The browser picks the first format it can play:
<video controls width="800">
<source src="video.webm" type="video/webm">
<source src="video.mp4" type="video/mp4">
<p>Your browser doesn't support HTML5 video. <a href="video.mp4">Download the video</a>.</p>
</video>
Always list WebM first — browsers that support it (Chrome, Firefox, Edge) will use it, while Safari will fall through to MP4.
Key Video Attributes Explained
| Attribute | Purpose |
|---|---|
controls | Shows playback controls |
autoplay | Starts playing automatically (requires muted in most browsers) |
muted | Mutes audio by default |
loop | Loops the video continuously |
playsinline | Prevents fullscreen on iOS |
poster | Image shown before video plays |
preload | Controls how much data is preloaded (none, metadata, auto) |
Autoplaying Background Video
A common pattern for hero sections and background animations uses autoplay with mute and loop:
<video autoplay muted loop playsinline poster="preview.jpg">
<source src="background.webm" type="video/webm">
<source src="background.mp4" type="video/mp4">
</video>
Important: Browsers block autoplay with audio. Always add muted if you want autoplay to work reliably across all browsers.
Replacing GIFs with WebM for Animations
Animated GIFs are notoriously large. Replacing them with looping WebM videos can reduce file sizes dramatically. Use this pattern:
<video autoplay muted loop playsinline style="max-width:100%;">
<source src="animation.webm" type="video/webm">
<source src="animation.mp4" type="video/mp4">
</video>
This behaves like a GIF but with far better compression and quality.
Accessibility Considerations
Video on the web must be accessible. Follow these guidelines:
- Add captions using
<track>elements with a WebVTT file - Provide a text transcript for important informational video
- Avoid autoplaying video with sound — it's disorienting for screen reader users
- Use
aria-labelon the video element if it lacks visible controls
<video controls aria-label="Product demo video">
<source src="demo.webm" type="video/webm">
<track kind="captions" src="captions.vtt" srclang="en" label="English">
</video>
Performance Tips for Web Video
- Use
preload="none"orpreload="metadata"for videos below the fold to reduce initial page load - Lazy-load off-screen videos using the Intersection Observer API
- Set explicit
widthandheightattributes to prevent layout shifts (CLS) - Use a CDN to serve video files — never serve large media from your web server directly
Following these practices ensures your WebM videos are fast, accessible, and compatible across all modern browsers.