HTML5 Progress and Meter Elements

My primary role at Atomic Object is an embedded developer. Most of my business hours are spent reading and writing C code. However, over the past couple months, I have been dabbling in web application development in my free time.

The other day I needed to display some time-based information on a webpage, and I thought a progress bar would be an ideal fit. I assumed making a progress bar on a webpage would require a fair amount of CSS and Javascript magic, so I began what I thought was going to be a long and arduous process by googling “html progress bar.” I was quite surprised to find that HTML5 includes a new tag called progress.

HTML5 Progress Element

The progress tag is a tool intended to visually display the progress of a task. Using it is as simple as setting the min and max attributes and then adjusting the value attribute to display the current progress. Here’s an example:

<progress min="0" max="1" value=".25"></progress>
<progress min="0" max="1" value=".5"></progress>
<progress min="0" max="1" value=".75"></progress>

Screen Shot 2013-05-27 at 7.14.35 PM

The progress tag turned out to be exactly what I was looking for. But I also had some temperature information that I wanted to display on my webpage. While I was reading through some of the documentation for the progress tag, I came across another similar tag that I found very interesting: the meter tag.

HTML5 Meter Element

The meter tag is useful for displaying a value within a given range and applying a style to the display to indicate how close the current value is to the optimum range. Like the progress element, meter includes minmax, and value attributes. But it also includes lowhigh, and optimum attributes, which can be used to define what ranges of values are high, medium or low.

The optimum attribuite defines which section of the range is desirable. If the high range is optimal, values in the high range are represented with a green bar, values in the medium range are yellow, and values in the low range are red. If the low range is optimal, the colors are reversed. If the medium range is optimal, then values between high and low are green and values in the high and low ranges are displayed as yellow.

<meter style="width: 200px" max="100" low="25" high="75" optimum="65" value="15"></meter>
<meter style="width: 200px" max="100" low="25" high="75" optimum="76" value="5"></meter>
<meter style="width: 200px" max="100" low="25" high="75" optimum="50" value="35"></meter>

Screen Shot 2013-05-27 at 8.55.49 PM

The meter element could be perfect for showing the output of something like a thermostat or temperature controller. The target temperature could be marked as the optimum value. Then, the current temperature could be assigned to value and the control would provide colorful feedback of how well the temperature controller is operating.

Customizing Progress & Meter

Adjusting the Style

The engineer side of me loves the progress and the meter tags: they are very basic and incredibly simple to use. However, the artistic side me of is a bit disappointed. After reading through the documentation, I found that styling them is not a simple task. If you’re not satisfied with the default look and feel of these items, you have to delve pretty deep into browser-specific CSS and potentially some Javascript in order to change them.

Check out this page for more information on styling progress bars if your interested.

Adding a Scale

Unfortunately, neither the progress tag nor the meter tag have a built-in way of displaying a scale along with the graph. However, with a little extra HTML and CSS, you can make one that automatically sizes to fill a parent container.

Screen Shot 2013-05-28 at 7.51.10 AM

<style>
#meter_item 
{
    width: 100%; 
}
 
#scale {
    display: table;
    width: 100%;
    padding: 0px 0px 0px 0px;
    margin: 2px 0px 10px 0px;
    border: 0px 0px 0px 0px;
    text-align: center;
}
 
#scale li {
    width: 10%;
    display: table-cell;
    white-space: nowrap;
}​
</style>
 
<div style="width: 300px">
    <meter id="meter_item" min="0" value="30" max="100" ></meter>
    <ul id="scale">
    <li style="width: 5%"><span></span></li>
    <li><span id="scale">10</span></li>
    <li><span id="scale">20</span></li>
    <li><span id="scale">30</span></li>
    <li><span id="scale">40</span></li>
    <li><span id="scale">50</span></li>
    <li><span id="scale">60</span></li>
    <li><span id="scale">70</span></li>
    <li><span id="scale">80</span></li>
    <li><span id="scale">90</span></li>
    <li style="width: 5%"><span id="scale"></span></li>
    </ul>
</div>