brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the meter element shows a level like disk usage, and turns red when it is too high

Web-Development
~/tips/tip-meter-element-disk-usage

low="0.7" high="0.9" optimum="0.2">92%</meter>

A built-in gauge that changes color based on ranges you define.

People reach for progress to draw a bar, but progress is for a task that is moving toward done. For a measurement within a known range, like a full disk or a battery level, meter is the element.

<label for="disk">Disk usage</label>
<meter id="disk" value="0.92"
       low="0.7" high="0.9" optimum="0.2">92%</meter>

min and max default to 0 and 1, so a fraction works directly. low and high split the range into three parts, and optimum says which part is good. Because the optimum is low here, Chrome shows green below 70 percent, yellow between 70 and 90, and red above 90. Flip the optimum to a high number and a battery meter works the other way around.

I like that the color logic is in the markup and not in a script that adds classes. The text between the tags is a fallback for browsers that do not draw the gauge.

All browsers support it, but styling is the weak spot. The bar and its colors live in browser specific pseudo-elements with prefixes, so matching a design in every browser takes effort. If you need full control over the look, a styled div with role="meter" and the right ARIA values is the alternative, at the cost of writing those yourself.

Further reading: MDN: The meter element

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link