Skip to main content

Create a Complete HTML Video Player with CSS (Step-by-Step)

The default HTML <video> player works well, but sometimes you want a custom player with your own design. In this tutorial, we will build a modern video player using HTML, CSS, and a little JavaScript.


💡 Features of This Video Player

  • Custom Play & Pause button
  • Progress bar showing playback position
  • Time display
  • Modern clean UI
  • No external libraries

📌 Step 1: HTML Structure

First, create the HTML layout for the player.

<div class="video-container">
  <video id="video" src="video.mp4"></video>

  <div class="controls">
    <button id="playBtn">Play</button>
    <div class="progress">
      <div id="progressBar"></div>
    </div>
    <span id="time">00:00 / 00:00</span>
  </div>
</div>

🎨 Step 2: CSS Styling

Add the following CSS to style the player:

<style>
body{
  background:#f7f9ff;
  font-family:Inter, Arial;
  padding:20px;
}

.video-container{
  width:650px;
  max-width:100%;
  margin:auto;
  background:#000;
  border-radius:10px;
  overflow:hidden;
  position:relative;
}

video{
  width:100%;
  display:block;
}

.controls{
  background:#111827;
  padding:10px;
  display:flex;
  align-items:center;
  gap:10px;
}

.controls button{
  background:#16a34a;
  color:#fff;
  border:none;
  padding:6px 14px;
  border-radius:6px;
  cursor:pointer;
  font-weight:600;
}

.controls button:hover{
  background:#15803d;
}

.progress{
  flex:1;
  height:8px;
  background:#374151;
  border-radius:6px;
  position:relative;
  cursor:pointer;
}

#progressBar{
  width:0%;
  height:100%;
  background:#22d3ee;
  border-radius:6px;
}

#time{
  color:#e5e7eb;
  font-size:13px;
}
</style>

⚙ Step 3: JavaScript Functionality

Now, add the logic for play, pause, progress bar update, and duration tracking.

<script>

const video = document.getElementById("video");
const playBtn = document.getElementById("playBtn");
const progressBar = document.getElementById("progressBar");
const timeText = document.getElementById("time");
const progressContainer = document.querySelector(".progress");

// Toggle play / pause
playBtn.addEventListener("click", () => {
  if(video.paused){
    video.play();
    playBtn.textContent = "Pause";
  } else {
    video.pause();
    playBtn.textContent = "Play";
  }
});

// Update progress bar
video.addEventListener("timeupdate", () => {
  const percent = (video.currentTime / video.duration) * 100;
  progressBar.style.width = percent + "%";

  // Update time display
  timeText.textContent = 
    formatTime(video.currentTime) + " / " + 
    formatTime(video.duration);
});

// Seek by clicking on progress bar
progressContainer.addEventListener("click", (e) => {
  const rect = progressContainer.getBoundingClientRect();
  const clickX = e.clientX - rect.left;
  const width = rect.width;

  video.currentTime = (clickX / width) * video.duration;
});

// Format time
function formatTime(sec){
  if(isNaN(sec)) return "00:00";
  
  const m = Math.floor(sec / 60);
  const s = Math.floor(sec % 60);
  
  return String(m).padStart(2,"0") + ":" + String(s).padStart(2,"0");
}

</script>

📂 Folder Structure

index.html
video.mp4

📌 Result

You now have a modern video player with:

  • Custom play button
  • Interactive progress bar
  • Dynamic time display
  • Clean and professional design

🎯 Challenge

Try adding more features:

  • Volume control
  • Full-screen toggle
  • Playback speed (1x / 2x)
  • Dark & light themes

We will create these in future tutorials.

Comments

Popular posts from this blog

HTML Tables Tutorial — Learn Tables with Examples Tables organize tabular data (rows & columns). They are great for schedules, price lists, spec sheets, comparison charts, and any data that fits a grid. This guide covers: Table basics: <table> , <tr> , <td> , <th> Headers, captions, and groups ( <caption> , <thead> , <tbody> , <tfoot> ) Colspan & rowspan Styling and responsive tables Accessibility and best practices Table of Contents Basic Structure Headers & Captions Colspan & Rowspan Grouping Rows: thead / tbody / tfoot Styling Tables Responsive Tables Accessibility Tips Practical Examples Best Practices 1. Basic Structure Minimal valid table: <table> <tr> <td>Cell 1</td> <td>Cell 2</td> </tr> </table> Elements: <table> — container for the table <tr> — table ...
HTML Forms Tutorial HTML Forms Tutorial HTML Forms allow users to enter and submit data. They are used everywhere—login forms, signup forms, search boxes, feedback forms, etc. In this complete tutorial, you will learn all form elements with examples and demos. 1. What is an HTML Form? A form collects user input using different form elements like text fields, checkboxes, radio buttons, buttons, and more. <form> Form elements go here... </form> 2. Basic Form Structure <form action="#" method="post"> <input type="text" placeholder="Enter name"> <button>Submit</button> </form> Submit Attributes: action → URL where form data is sent method="POST" → Secure data sending method="GET" → Data shows in URL 3. Text Input Field <input type="text" placeholder="Your Name...
What is HTML? A Beginner-Friendly Explanation If you want to become a web developer, HTML is the first language you should learn. It is the foundation of every website on the internet. In simple words: HTML (HyperText Markup Language) is used to create the structure and content of a webpage. Why HTML is important? Websites cannot exist without HTML It is simple and beginner-friendly Used with CSS and JavaScript Works on all browsers How does HTML work? HTML is made of tags . Tags tell the browser what type of content is being shown. Example of an HTML tag: <p>This is a paragraph.</p> Here: <p> is the opening tag </p> is the closing tag The text inside is the content Basic HTML Page Example Below is a simple webpage example you can try: <!DOCTYPE html> <html> <head> <title>My First Webpage</title> </head> <body> <h1>Hello World!</h1> <p>This is my...