EthicsComputerCourses
Join training +91 90532 10052

Progress & Meter Elements

HTML · Lesson 55 of 234 Intermediate 8 min read Updated

By the end of this lesson you will be able to show how far a task has got with <progress>, switch it between "unknown" and "measured" states, update it from JavaScript, and use <meter> with low, high and optimum so the browser colours a value as good, okay or bad.

You met both elements briefly in Datalist & Output Elements. This lesson goes deeper into their attributes, their states, the colour rules of <meter> and how to label them properly.

progress: how much of a job is done

Think of the bar that fills while an app installs on your phone. That is <progress>. It answers one question: "How much of this task is finished?"

It has only two attributes:

  • max is the total amount of work. It must be greater than zero. The default is 1.
  • value is how much is done, from 0 up to max.

A progress bar can be in one of two states:

StateHow you write itWhat the user seesUse it when
Determinate<progress value="40" max="100">A bar filled to 40%You know the total, such as bytes of a file
Indeterminate<progress> with no valueA moving "busy" animationYou do not know how long it will take yet

The CSS pseudo-class :indeterminate matches a progress bar without a value, so you can style the waiting state separately. In JavaScript, the read-only position property gives value / max for a determinate bar and -1 for an indeterminate one.

To go back from determinate to indeterminate, remove the attribute with progress.removeAttribute("value"). Setting value to an empty string does not do it.

Live coding: a simulated upload

We will fake an upload of a 4,800 KB video for a Jaipur travel blog, so you can watch the bar change. A real upload would use the Fetch API or XMLHttpRequest progress events, which come in Level 5.

Step 1: start with an indeterminate bar labelled "Upload progress", because the browser is still "preparing".

Step 2: after one second, set max to the file size and start increasing value. The bar becomes determinate and fills up.

Step 3: when value reaches max, stop the timer and update a status message.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Upload progress</title>
  <style>
    progress { inline-size: 100%; block-size: 1.2rem; accent-color: #2e7d32; }
    progress:indeterminate { accent-color: #888; }
  </style>
</head>
<body>
  <h1>Uploading jaipur-hawa-mahal.mp4</h1>
  <label for="upload">Upload progress</label>
  <progress id="upload">Preparing</progress>
  <p id="status" aria-live="polite">Preparing upload...</p>
  <button id="again">Start again</button>

  <script>
    const bar = document.querySelector("#upload");
    const statusText = document.querySelector("#status");
    const totalKB = 4800;
    let timer;

    function start() {
      clearInterval(timer);
      bar.removeAttribute("value");          // Step 1: indeterminate
      statusText.textContent = "Preparing upload...";

      setTimeout(() => {
        bar.max = totalKB;                   // Step 2: determinate
        bar.value = 0;
        timer = setInterval(() => {
          bar.value = Math.min(bar.value + 400, totalKB);
          bar.textContent = `${Math.round(bar.position * 100)}%`;
          if (bar.value === totalKB) {       // Step 3: finished
            clearInterval(timer);
            statusText.textContent = "Upload complete.";
          }
        }, 300);
        statusText.textContent = "Uploading...";
      }, 1000);
    }

    document.querySelector("#again").addEventListener("click", start);
    start();
  </script>
</body>
</html>

You should see a grey busy bar for one second, then a green bar that fills in about four seconds, then "Upload complete." The script also updates the text inside <progress>. That text is fallback content, shown only by browsers that cannot draw the bar.

The status paragraph has aria-live="polite", so screen readers announce "Uploading..." and "Upload complete." Do not announce every small step; that would talk over the user non-stop.

meter: a measurement in a known range

A <meter> is like the fuel gauge in a car. It shows a value inside a fixed range, and some parts of the range are better than others. It is not about a task finishing, so it never animates.

AttributeMeaningDefault
valueThe measured value (required)0
min, maxThe range0 and 1
lowBelow this is the "low" regionSame as min
highAbove this is the "high" regionSame as max
optimumThe best possible valueHalfway between min and max

The low and high marks split the range into three regions. The region that contains optimum is the good one. Most browsers then colour the bar green for good, yellow for "not ideal" and red for "bad".

Where optimum isLow regionMiddle regionHigh region
In the low region (less is better, for example disk used)GoodNot idealBad
In the middle region (the middle is best, for example humidity)Not idealGoodNot ideal
In the high region (more is better, for example battery)BadNot idealGood

Notice the middle case: when the best value is in the middle, both ends are only "not ideal", never "bad".

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Meter colours</title>
  <style>
    meter { inline-size: 220px; }
    p { display: grid; grid-template-columns: 160px auto; gap: 8px; align-items: center; }
  </style>
</head>
<body>
  <h1>Isha's phone dashboard</h1>
  <label for="level">Move the slider: <output id="shown" for="level">50</output></label>
  <input id="level" type="range" min="0" max="100" value="50">

  <p>
    <label for="battery">Battery (more is better)</label>
    <meter id="battery" min="0" max="100" low="20" high="80" optimum="100" value="50">50%</meter>
  </p>
  <p>
    <label for="disk">Storage used (less is better)</label>
    <meter id="disk" min="0" max="100" low="60" high="85" optimum="0" value="50">50%</meter>
  </p>
  <p>
    <label for="humid">Room humidity (middle is best)</label>
    <meter id="humid" min="0" max="100" low="30" high="60" optimum="45" value="50">50%</meter>
  </p>

  <script>
    const slider = document.querySelector("#level");
    slider.addEventListener("input", () => {
      for (const m of document.querySelectorAll("meter")) {
        m.value = slider.value;
        m.textContent = `${slider.value}%`;
      }
      document.querySelector("#shown").textContent = slider.value;
    });
  </script>
</body>
</html>

Drag the slider to 10. Battery turns red, storage is green and humidity is yellow. Drag it to 95: now battery is green and storage is red. Same number, different meaning, all from three attributes.

Labelling both elements

Both <progress> and <meter> are labelable elements, which means a <label for="..."> connects to them just like an input. Screen readers then read the label with the value, for example "Upload progress, 40 percent".

  • Use a visible <label> when possible.
  • When the visible text is elsewhere, point to it with aria-labelledby.
  • Do not rely on the fallback text between the tags as the label; most modern browsers never show it.

progress or meter?

QuestionElement
How much of a task is finished? (upload, quiz steps, video watched)<progress>
How long will it take? I do not know yet<progress> without value
How full or how good is something? (storage, battery, marks, password strength)<meter>
A single number with no rangePlain text or <output>

A simple test: if the value will move towards 100% and stop, use <progress>. If it can go up and down forever, use <meter>.

Common mistakes

  • Using <meter> as a loading bar. It has no busy state and no idea of "finished".
  • Writing value="" to reset a progress bar. Remove the attribute instead.
  • Setting value above max. The browser clamps it to max, which hides the bug.
  • Forgetting optimum. Without it, the middle is "best", which is wrong for battery or storage.
  • No label. A bar without a label tells screen reader users nothing.

Interview questions

1. What is the difference between <progress> and <meter>?

<progress> shows how much of a task is complete and can be indeterminate when the total is unknown. <meter> shows a scalar measurement inside a known range, such as disk usage, and can colour the value using low, high and optimum. A meter should never be used as a loading indicator.

2. How do you make a progress bar indeterminate?

Leave out the value attribute, or remove it with removeAttribute("value"). The bar then shows a busy animation and matches the :indeterminate pseudo-class. Its position property returns -1.

3. How does the browser decide a meter's colour?

low and high divide the range into three regions, and the region containing optimum is treated as the best. A value in that region is shown as good, an adjacent region as less good, and the far region as worst. When optimum is in the middle, both outer regions count as less good.

4. How do you make these elements accessible?

Give each one a label with <label for> or aria-labelledby, because both are labelable elements. Show the value in text as well, since colour alone is not enough. For long tasks, announce key moments in an aria-live region instead of every update.

Key points

  • <progress> shows how much of a task is done; max defaults to 1.
  • Without value, a progress bar is indeterminate; use removeAttribute("value") to return to that state.
  • <meter> shows a value in a range; low, high and optimum decide which region is good.
  • The region containing optimum is green, its neighbour yellow and the far region red in most browsers.
  • Both elements are labelable: connect a <label> and show the value as text too.
  • Progress moves towards finished; a meter goes up and down.

Exercise

Build a "Mock test" page for a student in Rohtak. Show a <progress> labelled "Questions answered" for 7 of 20 questions, and a <meter> labelled "Score" for 62 marks out of 100, where below 40 is bad, above 75 is good and 100 is the best. Add a button "Answer next question" that increases the progress by one and stops at 20.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Mock test</title>
</head>
<body>
  <p>
    <label for="answered">Questions answered</label>
    <progress id="answered" value="7" max="20">7 of 20</progress>
  </p>
  <p>
    <label for="score">Score</label>
    <meter id="score" min="0" max="100" low="40" high="75" optimum="100" value="62">62 out of 100</meter>
    62 out of 100
  </p>
  <button id="next">Answer next question</button>

  <script>
    const answered = document.querySelector("#answered");
    document.querySelector("#next").addEventListener("click", () => {
      answered.value = Math.min(answered.value + 1, answered.max);
    });
  </script>
</body>
</html>

With optimum="100" in the high region, a score of 62 sits in the middle region and shows as "not ideal" (yellow). Math.min keeps the progress from going past 20.

Quick Quiz

1. What is the default max of a <progress> element?

2. Which progress bar is indeterminate?

3. A battery meter has low="20" high="80" optimum="100". How is the value 10 shown?

4. Which element fits "Storage: 45 GB of 64 GB used"?

Learn HTML with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call