"If it's stupid but it works, it's not stupid."
I want to draw some vertical bar charts. I don't want to use a 3rd party library, or bundle someone else's CSS, or learn how to build SVGs.
HTML contains a <meter>
element. It is used like this:
HTML
<meter min="0" max="4000" value="1234">1234</meter>
Which looks like this:
There isn't much you can do to style it. Browser manufacturers seem to have forgotten it exists and the CSS standard kind of ignores it.
It is possible to use CSS to rotate it using:
CSS
meter { transform: rotate(-90deg); }
But then you have to mess about with origins and the box model gets a bit confused.
See what
You can hack your way around that with <div>
s and bludgeoning your layout into submission.
But that is a bit tedious.
Luckily, there's another way. As suggested by Marius Gundersen, it's possible to set the writing direction of the element to be vertical.
That means you can have them "written" vertically, while having them laid out horizontally. Giving a nice(ish) bar-chart effect.
As well as the normal sort of CSS spacing, there is basic colour support for values which are inside a specific range:
The background colour can also be set.
I dare say they're slightly more accessible than a raster image - even with good alt text. They can be targetted with JS, if you want to do fancy things with them.
Or, if you just want a quick and dirty bar-chart, they're basically fine.
5 thoughts on “Quick and dirty bar-charts using HTML's meter element”
@Edent Alack alas, and as is tradition, mobile Safari disappoints.
Reply to original comment on hachyderm.io
|@Edent sadly it looks like the iPadOS browser doesn’t support changing the colours on the meter element, so maybe don’t rely on colouring to convey information if wider compatibility is needed?
Reply to original comment on mastodon.richardloxley.com
|Gotta love old HTML tags and how macOS Safari, Chrome and Firefox have very different ideas on how to display them! 😀
Reply to original comment on bsky.app
|mike
Quick and dirty with significantly inconsistent results in different browsers. Some screenshots:
Chrome on Android. Edge on Windows 11 looks the same. https://ibb.co/CKZQC3R9
Firefox on Linux https://ibb.co/ymsPpN3w
Safari on iPadOS https://ibb.co/23MSdGvq
@blog Quicker and dirtier bar charts: an (inline?) IMG of a coloured pixel, and use different height/widths. Works everywhere an image does - unlike these METER examples!
Reply to original comment on bne.social
|More comments on Mastodon.