The `` tag is the standard way to add video directly to a web page
HTML has a built-in <video> element that lets you embed video files without relying on external players or third-party services. You place the tag in your HTML, point it to a video file on your server, and the browser handles playback. The tag supports common formats like MP4, WebM, and Ogg, though browser support varies by format.
The basic structure is simple: an opening <video> tag with attributes that control width, height, and playback behavior, one or more <source> tags that point to your video files, and a closing </video> tag. If a browser cannot play any of the formats you provide, it shows a fallback message instead of a blank box.
Key Takeaways
- The <video> tag embeds video directly in HTML using <source> elements that point to video files on your server or a content delivery network.
- MP4 format works in nearly all modern browsers, while WebM and Ogg offer better compression but narrower support—provide multiple formats so every browser can play something.
- Add the controls attribute to show play, pause, and volume buttons; add autoplay to start playing when the page loads, though most browsers now require muted as well.
- Set width and height to prevent layout shift when the video loads, or use CSS to make the video responsive across different screen sizes.
- For videos hosted on YouTube or Vimeo, use their embed code (an <iframe> tag) instead of the <video> tag.
Basic video tag syntax with a single format
Here is the simplest working example:
<video width="640" height="360" controls> <source src="movie.mp4" type="video/mp4"> </video>
The width and height attributes set the player size in pixels. The controls attribute tells the browser to display standard playback controls—play, pause, seek bar, volume, and fullscreen. The <source> tag points to your video file and declares its MIME type so the browser knows what format it is.
The video file (movie.mp4 in this example) must be in the same directory as your HTML file, or you provide the full path. If the file is in a folder called "videos", write src="videos/movie.mp4". You can also use a full URL if the video is hosted elsewhere: src="https://example.com/videos/movie.mp4".
Supporting multiple formats for broader browser compatibility
Not all browsers support all video formats. MP4 works almost everywhere, but WebM offers better compression for smaller file sizes, and Ogg is an older open format. The safest approach is to provide at least two formats—usually MP4 and WebM—so every browser can play one of them.
<video width="640" height="360" controls> <source src="movie.mp4" type="video/mp4"> <source src="movie.webm" type="video/webm"> Your browser does not support the video tag. </video>
The browser reads the <source> tags in order and plays the first format it understands. If it cannot play MP4, it tries WebM. If it cannot play either, it displays the fallback text. You can add a third <source> for Ogg format (type="video/ogg") if you want to cover older browsers, but MP4 and WebM together cover nearly all current devices.
Autoplay, muting, and looping
The autoplay attribute starts the video playing as soon as the page loads. Most modern browsers now block autoplay unless the video is also muted, so you almost always need both attributes together:
<video width="640" height="360" controls autoplay muted> <source src="movie.mp4" type="video/mp4"> </video>
Add loop to make the video restart from the beginning when it reaches the end. This is useful for background videos or animated headers. You can combine all three: autoplay muted loop. The poster attribute lets you show a still image before the video plays—useful for giving viewers a preview of what they are about to watch:
<video width="640" height="360" controls poster="thumbnail.jpg"> <source src="movie.mp4" type="video/mp4"> </video>
Making video responsive across different screen sizes
Setting a fixed width and height works for desktop, but on mobile devices the video may be too large or too small. A common pattern is to set the width to 100% and let the height adjust automatically, then wrap the video in a container with a maximum width:
<div style="max-width: 800px;"> <video width="100%" controls> <source src="movie.mp4" type="video/mp4"> </video> </div>
For a more polished result, use CSS to maintain the correct aspect ratio (usually 16:9 for widescreen video) across all screen sizes. This prevents the video from stretching or squashing. Many developers use a padding-bottom trick or the newer CSS aspect-ratio property to lock the proportions while the width scales.
Hosting video files: your server versus a CDN
You can host video files on your own web server, but video files are large and bandwidth-heavy. Serving video from your own server works for small audiences or short clips, but for anything else, a content delivery network (CDN) is faster and cheaper. Services like AWS CloudFront, Bunny CDN, or Cloudflare deliver video from servers near your viewers, reducing load time and your hosting costs.
If you host on your own server, make sure your server is configured to serve video files with the correct MIME types. Most hosting providers handle this automatically for .mp4, .webm, and .ogg files, but check your server documentation if videos fail to play. If you use a CDN, you upload the video once and the CDN handles delivery—you just point your <source> tag to the CDN URL.
When to use <iframe> embed codes instead
If your video is hosted on YouTube, Vimeo, or another video platform, do not use the <video> tag. Instead, use the embed code those services provide, which is usually an <iframe> tag. YouTube and Vimeo handle all the technical details—format conversion, streaming, player controls, analytics—so you do not have to.
To get an embed code from YouTube, click Share below the video, then click Embed, and copy the code provided. It looks like this:
<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Paste this directly into your HTML. The <video> tag is for files you control; the <iframe> embed is for videos hosted elsewhere.
Frequently Asked Questions
What video formats should I use?
MP4 is the safest choice because it works in all modern browsers. If file size matters, also provide WebM, which compresses better. Most projects use MP4 and WebM together. Ogg is older and rarely needed unless you support very old browsers.
Can I add subtitles or captions to an HTML video?
Yes, use the <track> tag inside the <video> element. Create a WebVTT file (.vtt) with your captions, then add <track src="captions.vtt" kind="captions" srclang="en" label="English"> before the closing </video> tag. The browser displays a captions button in the player.
Why is my video not playing?
Check that the file path is correct and the file exists. Verify the MIME type matches the actual format. Test in a different browser to see if it is a format support issue. If the video is on a different domain, check that CORS (cross-origin resource sharing) is configured correctly on that server.
How do I make a video play in fullscreen?
The controls attribute automatically adds a fullscreen button to the player. Clicking it expands the video to fill the screen. If you want to trigger fullscreen with JavaScript, use the requestFullscreen() method on the video element.
Should I use autoplay on my website?
Use autoplay sparingly. Most browsers now block autoplay unless the video is muted, and autoplaying video can annoy visitors and slow down page load. Autoplay works best for background videos or animations where sound is not expected. For content videos, let users click play.