mc0de
Advanced HTML · Lesson 07 of 13

HTML Video

Bring your pages to life with moving pictures! Learn how to embed videos, control playback, add captions, and build a responsive player visitors can enjoy.

HTML5 VideoPlayback Controls CaptionsInteractive Player
● ● ●   video.html
<video controls width="320">
  <source src="movie.mp4"
          type="video/mp4">
</video>
01 / Getting Started

Video Belongs on the Modern Web

From tutorials and product demonstrations to educational content and creative projects, video is everywhere online.

HTML makes it possible to add a video player directly to your page using the <video> element. No browser plugin is required!

Try it: Press Play, pause the video, and use the timeline. Those controls are provided by the browser.
02 / Your First Video

Meet the <video> Element

The simplest video player needs a video file and the controls attribute.

A basic HTML video player
<video controls width="640">
  <source src="my-video.mp4" type="video/mp4">
  Your browser does not support HTML video.
</video>

The <source> element points to the media file. The type attribute helps the browser determine whether it can play that format.

Without controls, the browser typically won’t show the familiar Play, Pause, and timeline interface.

03 / Playback Attributes

Control How Your Video Behaves

The video element supports several useful attributes that change the viewing experience.

Video playback options
<video
  controls
  muted
  loop
  playsinline
  preload="metadata"
  width="640"
>
  <source src="my-video.mp4" type="video/mp4">
</video>
  • controls displays playback controls.
  • muted starts playback without audio.
  • loop repeats the video when it finishes.
  • playsinline requests inline playback on supported mobile devices.
  • preload="metadata" hints that the browser should fetch basic media information.
  • poster sets an image to display before playback.
Important: Autoplay is restricted in many browsers, especially when audio is enabled. Avoid surprising visitors with automatically playing media.
04 / Video Formats

Support More Browsers

Different browsers and devices may support different video codecs and containers. You can offer multiple sources so the browser can choose one it supports.

Multiple video sources
<video controls width="640">
  <source src="movie.webm" type="video/webm">
  <source src="movie.mp4" type="video/mp4">
  Your browser does not support HTML video.
</video>

The browser checks the sources in order and attempts to use a playable one. MP4 and WebM are common web video containers, but actual playback also depends on the codecs inside the file.

05 / Captions & Accessibility

Make Video More Inclusive

Captions help people who are deaf or hard of hearing, and they’re useful for anyone watching with the sound off.

Use the <track> element to connect a WebVTT caption file to your video.

Adding captions
<video controls width="640">
  <source src="lesson.mp4" type="video/mp4">
  <track
    kind="captions"
    src="captions-en.vtt"
    srclang="en"
    label="English"
    default
  >
</video>

A WebVTT file contains time-coded caption text. For meaningful spoken content, provide accurate captions and consider a transcript as well.

Accessibility tip: Captions should include important spoken words and relevant non-speech sounds. A transcript can make content easier to review and search.
06 / Responsive Video

Make Your Player Fit Any Screen

A video that looks perfect on desktop shouldn’t overflow on a phone. A few CSS rules can help your player adapt.

Responsive video styling
<style>
  video {
    display: block;
    width: 100%;
    max-width: 800px;
    height: auto;
    border-radius: 12px;
    background: #0b1426;
  }
</style>

<video controls>
  <source src="my-video.mp4" type="video/mp4">
</video>

Using width: 100% with a sensible max-width lets the player shrink for smaller screens without growing too large.

07 / JavaScript Video Controls

Build Your Own Player Buttons

HTML gives you a ready-made player, but JavaScript lets you create custom controls too.

Custom Play and Pause
<video id="myVideo" controls>
  <source src="my-video.mp4" type="video/mp4">
</video>

<button type="button" id="toggle">
  Play / Pause
</button>

<script>
  const video = document.getElementById("myVideo");

  document.getElementById("toggle")
    .addEventListener("click", function() {
      if (video.paused) {
        video.play();
      } else {
        video.pause();
      }
    });
</script>

You can also use currentTime to jump to a point in the video and muted to control sound.

Good habit: Keep the browser’s built-in controls available while learning. A fully custom player needs accessible keyboard controls, labels, and other features to match native controls.
🚀 mC0de Challenge

Build a Custom Video Player

You’re creating a mini video experience for your coding portfolio. Give visitors an easy way to watch and control a video!

Your Mission

  1. Add a heading and a short introduction.
  2. Embed a video using <video> and <source>.
  3. Enable the browser’s playback controls.
  4. Make the player responsive with CSS.
  5. Add a custom Play/Pause button using JavaScript.
  6. Add a Restart button that returns playback to the beginning.
Extra challenge: Add a Mute/Unmute button and update its label based on the current sound setting.
💻 Interactive HTML Sandbox

Build Your Video Player!

Edit the player, change its styling, and add custom controls. Click Run Code to see your changes. The sample video requires an internet connection.

Live Result
Lesson 07 Recap

What You’ve Learned

Your Pages Are Coming to Life!

You’ve learned how to add video to a website and create a more polished viewing experience.

  • How to embed video using HTML.
  • How to configure playback attributes.
  • How to offer multiple video sources.
  • How captions and transcripts improve accessibility.
  • How to create responsive and custom video controls.

Next up: HTML Audio — learn how to bring sound, music, and audio controls into your web pages!

Your Learning Journey

0 / 13 Completed

Keep going! Every completed lesson brings you closer to mastering Advanced HTML.

🎉

Nice Work!

You've learned how to embed videos, configure playback, improve accessibility with captions, and build your own custom video controls. Your web pages are becoming more dynamic!