sodcalculator

10 Tech How-To

Cross Browser Progress Bar: Step-by-Step Guide

Guide 10-35387 · Updated · Step by step

This guide walks you through creating an HTML progress bar that displays consistently across all major browsers, including older versions of Internet Explorer. You’ll learn to use native HTML elements with CSS and a touch of JavaScript for a robust, accessible solution. No frameworks or external libraries are required—just a text editor and a web browser.

What You’ll Need

  • A text editor (e.g., Notepad, VS Code, Sublime Text)
  • A modern web browser for testing (Chrome, Firefox, Safari, Edge)
  • Basic knowledge of HTML and CSS

Step-by-Step

  1. Create the HTML structure. Use the native <progress> element for modern browsers. For older browsers that don’t support it, include a fallback with a <div>. Example:
    <progress id="progressBar" max="100" value="50"></progress>
    <div id="fallback" class="progress-fallback">
      <div class="progress-bar" style="width:50%;"></div>
    </div>
  2. Style with CSS. Add CSS to make the progress bar look consistent. Style both the native element and the fallback. Use vendor prefixes for older WebKit and Firefox versions. Example:
    progress {
      width: 100%;
      height: 20px;
      appearance: none;
      -webkit-appearance: none;
      border: none;
      background-color: #eee;
    }
    progress::-webkit-progress-bar {
      background-color: #eee;
    }
    progress::-webkit-progress-value {
      background-color: #4caf50;
    }
    progress::-moz-progress-bar {
      background-color: #4caf50;
    }
    .progress-fallback {
      display: none;
      width: 100%;
      height: 20px;
      background-color: #eee;
    }
    .progress-bar {
      height: 100%;
      background-color: #4caf50;
    }
  3. Add JavaScript for dynamic updates. To update the progress bar, set the value property on the <progress> element. For the fallback, update the width of the inner <div>. Example:
    function updateProgress(value) {
      var progress = document.getElementById('progressBar');
      var fallback = document.getElementById('fallback');
      if (progress) {
        progress.value = value;
      } else {
        fallback.style.display = 'block';
        fallback.querySelector('.progress-bar').style.width = value + '%';
      }
    }
  4. Detect support and show fallback. Use JavaScript to check if the browser supports <progress>. If not, hide the native element and show the fallback. Example:
    if (!('value' in document.createElement('progress'))) {
      document.getElementById('progressBar').style.display = 'none';
      document.getElementById('fallback').style.display = 'block';
    }
  5. Test across browsers. Open your HTML file in Chrome, Firefox, Safari, Edge, and if possible, Internet Explorer 11. Verify that the progress bar displays correctly and updates as expected.
  6. Enhance accessibility. Add role="progressbar" and aria-valuenow, aria-valuemin, aria-valuemax attributes to the fallback for screen readers. The native <progress> already has these built-in.

Common Mistakes

  • Not using vendor prefixes for appearance in older WebKit browsers, causing the bar to look unstyled.
  • Forgetting to hide the fallback in modern browsers, leading to a duplicate bar.
  • Setting width on the wrong element in the fallback—the inner bar must have the width, not the outer container.
  • Not handling the max attribute correctly; if your max is not 100, ensure your width calculations use percentages relative to that max.

Tips

  • Use CSS transitions to animate the width change for a smoother visual effect.
  • Keep the progress bar accessible by always providing text content or an aria-label.
  • For indeterminate progress (e.g., loading), use the indeterminate attribute on <progress> or a CSS animation for the fallback.
  • Consider using a CSS-only approach with a <div> if you don’t need JavaScript updates—it’s simpler and fully cross-browser.

Troubleshooting

  • Progress bar not showing in Firefox: Ensure you have the ::-moz-progress-bar pseudo-element styled. Also, check that the background-color on the <progress> itself is set.
  • Fallback appears in modern browsers: Your detection script may be incorrectly identifying support. Test with document.createElement('progress').max !== undefined—if it’s undefined, the element isn’t supported.
  • Width not updating: Verify that you’re setting the width as a percentage string, e.g., value + '%', not a number.
  • Inconsistent heights: Set an explicit height on both the native and fallback elements to ensure they match.

Frequently Asked Questions

Can I use the <progress> element in all browsers?

No, Internet Explorer 9 and earlier do not support it. That’s why the fallback is necessary.

How do I make an indeterminate progress bar?

For native, add the indeterminate attribute. For the fallback, use a CSS animation that moves a striped bar across the container.

Can I use a progress bar without JavaScript?

Yes, if the value is static, you can just set the value attribute in HTML and let CSS handle the styling. For dynamic updates, you need JavaScript.

Is it better to use a <div> instead of <progress>?

Using <progress> is more semantic and accessible. However, if you need to support very old browsers, a <div> with ARIA roles is a viable fallback.

How do I change the color of the progress bar?

Use the vendor-specific pseudo-elements: ::-webkit-progress-value for WebKit, ::-moz-progress-bar for Firefox, and the background-color on the native element for Edge/IE.

Can I use a gradient or image as the progress fill?

Yes, you can apply a background-image or linear-gradient to the progress value pseudo-elements or the fallback bar.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →