How to overlay text on videos using videojs?

Screenshot of the overlay text


Videojs is a popular javascript library to do anything with videojs in web. As it is a very popular library, it has features that can do almost everything related to videos.

In this article we will learn about how to overlay a simple text on top of video. This is to show that this kind of thing is possible with videojs. You can build on top of this and can build complex UI’s and features with the help of css and javascript.

Step 1:

Install videojs libarary and setup your video within it.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="" rel="stylesheet" />

          class="video-js vjs-big-play-centered"
          <source src="./BigBuckBunny.mp4" type="video/mp4" />
          <p class="vjs-no-js">
            To view this video please enable JavaScript, and consider upgrading to a
            web browser that
            <a href="" target="_blank"
              >supports HTML5 video</a

        <script src=""></script>

In order to install videojs in your project you have to add a script file and a css file into your project.

<script src=""></script>
<link href="" rel="stylesheet" />

When I’m writing this article videojs is on version 7.18. When you are reading this article videojs might be of later version. Hence I suggest to check with videojs docs and update to latest version of videojs.

Step 2:

Now you need to create a instance of videojs player. We can do that as shown below.

 let playerInstance = new videojs("my-video");

 Here "my-video" is the id of video element.

Once we have instance of player with us we can listen for player ready event and append our div element to the player.

 playerInstance.on("ready", () => {
    let div = document.createElement("div");
    div.innerHTML = `
    <p style=" margin: 0; color: black; font-size: 5rem; position: absolute;">Giri Aakula</p>


That’s it! We have successfully overlayed a sample text on top of video. Now you can do some interesting things on top of this. Very obvious use case could be to add a form and take user feedback after video completion.

Write to me if you have any questions -