Display Completion Progress of a Task in HTML5



To visually demonstrate the completion of task or goal, we use the <progress> element. The max and value attributes show the progress made towards task completion. The <progress> element offers a visual representation of the completion status of task. It's typically displayed as a progress bar, indicating how much of a particular task has been completed. Here is the syntax for the <progress> tag in HTML ?

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

The <progress> element has both opening and closing tags. It is a new semantic element in HTML5. This progress bar id also used to display the progress of file being uploaded.

The HTML <progress> tag supports the following attributes ?

Attribute

Value

Description

max

Max

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

value

value

Specifies the amount of task completion. It should be a floating-point number between 0 and the max value, or between 0 and 1 if max is excluded.

It also supports the global and event attributes.

Example

The following example demonstrates how to display task completion progress in HTML. This code creates a progress bar labeled "file copying Progress" Showing 45% completion. The <progress> element uses value and max attributes to visually represent the progress.

<!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

This HTML code creates a progress bar labeled "File Downloading Progress" showing 35% completion. The CSS styles the progress bar, setting its width to 250px and height to 20px.

<!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

The following example creates a progress bar with "Increase" and "Decrease" buttons using HTML and JavaScript. Clicking the buttons adjusts the progress bar's width by 5%. The CSS styles the progress bar, while the JavaScript functions manage the width adjustments.

<!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: 2025-01-21T11:01:09+05:30

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements