How to display completion progress of a task in HTML5?


If we want to serve a visual demonstration of completion of any task or goal, we use <progress> element. To display how much progress has been towards task completion we use max and value attributes. Following is the syntax of <progress> tag in HTML -

<progress value=”20”  max=”100”></progress>

Progress has open and closing tags. It is a new semantic element in HTML5. For displaying a file is being uploaded also this progress bar is used.

The HTML <progress> tag supports the following attributes −

Attribute

Value

Description

max

Max

It should have a value greater than zero and a valid floating-point number.

value

value

Specifies how much of the task has been completed. It should be a floating-point number between 0 and max or 0 and 1 if max is omitted.

It also supports the global and event attributes.

Example

Following example demonstrates how to display completion progress of a task in HTML −

<!DOCTYPE html>
<html>
<body>
   <h1>Progress element in HTML</h1>
   <label for="file">File Copying Progress:</label>
   <progress id="file" value="45" max="100"> 45% </progress>
</body>
</html>

Now, let us see how to style the progress bar in HTML −

CSS Styling the Progress bar

Now let’s style the progress bas using CSS –

<!DOCTYPE html>
<html>
<head>
   <style>
      progress[value] {
         -webkit-appearance: none;
         appearance: none;
         width: 250px;
         height: 20px;
      }
   </style>
</head>
<body>
   <h1>Progress element in HTML</h1>
   <label for="file">File Downloading Progress:</label>
   <progress id="file" value="35" max="100"> 35% </progress>
</body>

Example

Following example, creates a progress bar with two buttons, increase and decrease the bar by applying JavaScript code.

<!DOCTYPE html>
<html>
<style>
   #container {
      width: 100%;
      background-color: gray;
   }

   #bar {
      width: 0%;
      height: 40px;
      background-color: red;
   }
</style>
<body>
   <h1>Progress Bar</h1>
   <div id="container">
      <div id="bar"></div>
   </div>
   <br>
   <button onclick="Increase()">Increase</button>
   <button onclick="Decrease()">Decrease</button>
   <!-- dispaying message for user -->
   <h2>Increase by 5%</h2>
   <h2>Decrease by 5%</h2>
   <script>
      function Increase() {
         var elem = document.getElementById("bar");
         var width = elem.style.width;
         width = width.replace(/%/g, '');
         if (width == "") width = '5';
         else width = parseInt(width) + 5;
         if (width == 100) {
            width = width.toString() + '%';
            elem.style.width = width;
         } else if (width < 100) {
            width = width.toString() + '%';
            elem.style.width = width;
         }
      }

      function Decrease() {
         var elem = document.getElementById("bar");
         var width = elem.style.width;
         width = width.replace(/%/g, '');
         if (width == "") width = '0';
         else width = parseInt(width) - 5;
         if (width == 0) {
            width = width.toString() + '%';
            elem.style.width = width;
         } else if (width > 0) {
            width = width.toString() + '%';
            elem.style.width = width;
         }
      }
   </script>
</body>
</html>

Updated on: 10-Oct-2023

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements