r/HTML • u/VanillaWaffle_ • Aug 15 '26
Proposal: A native HTML <unit> element for automatic unit conversion
Browsers could automatically convert units to the user agent preferred unit. Just like the existing language declaration and browser can suggest auto translation.
<unit value="10" unit-base="m^2" unit-name="mi2" convert="auto">
10 square miles
</unit>
The attributes mean:
- value : numeric value
- unit-name : standardized source unit (mi2, kg, mph, kW, etc.)
- unit-base : standardized unit dimension (m2, kgm^2s-3)
- convert : auto/on/off
The browser knows that mi2 is an area and can display the same quantity as, for example, 25.9 km² if that is the user's preferred unit.
The unit name and base are standardized and language neutral.
The important part is that the HTML stores the original value and unit. The UA only changes the presentation. And it's useful for weather app, cooking recipe website all the way to commercial airliner jet documentation.
25
u/morete Aug 15 '26
The comments thinking this is beyond the scope of html are gonna be horrified when they find out about elements like <usermedia> and <geolocation>
7
u/u8589869056 Aug 15 '26
Need a precision, or browsers will be free to turn 10m into 393.707874 in.
8
u/JaydonLT Aug 15 '26
Love this
3
u/jaapsch2 Aug 16 '26
This will be tricky because there can be different conversions for the same units depending on context. The most common problematic case is temperatures versus temperature differences.
For example a temperature of 0 degrees celsius is 32 degrees fahrenheit, but if two temperatures differ by 0 degrees celsius, they also differ by 0 fahrenheit.
2
u/Jasedesu Aug 16 '26
You might think this is a good idea on the surface, but it doesn't take much digging to find the proposal has multiple flaws.
For starters, your semantics are wrong, because your
<unit>element is actually a value with units. You should call it<dimensionedValue>or something, or maybe split it into two elements that have one job each:<value id="v1">10</value><unit for="v1">km</unit>. However, you don't need to invent new mark-up to do this, as HTML5 incorporates MathML, so you can already write<math><mn>10</mn><mi>km</mi></math>. There are already advantages of doing it this way for improved semantics, accessibility and typography, but it's not without its own problems. For example, there's currently no way to mark something as a unit, although MathML 4 is looking to address this with theintent=":unit"attribute. I'd also like a better way to explicitly link a unit to a value than just proximity, as you don't always have the unit next to the number (e.g. tables and charts). Of course, there's also the whole verbosity of MathML to consider.As for unit conversions, I think you vastly underestimate the complexity of the task. Just think of the range of units you'd need to support for a start. There's all the SI stuff, some of which can get quite complex. Imperial is a nightmare - take fluid volume, such as pint and gallon, that are different in the US and the UK - do we need to tie this in with the
langattribute? Historical units - fathoms, chains, cubits, etc. - are you supporting those? Fictional units - there are quite a lot of them, some with real world uses. Of course a lot of the necessary work has already been done - Google calculator exists and there are unit conversion libraries out there, but I think getting it done well enough and completely enough will be a challenge. It'd end up as just another part of the internet that's incomplete, such as natural language designations.If it were to be a built in browser feature, such as translation, then we'd also need a
noconvertattribute to block conversion in those cases where authors do not want their content to be messed with.If someone were to build a really good implementation with the tools currently available and it started to gain traction, then you'd have a better case.
1
u/abdul_Ss Aug 16 '26
I'm making a hiking route planner and MY GOD would this be amazing. It would remove a lot of code that I'm doing right now on the JS/TS side for distance unit settings.
1
u/Important_Yam_71 Aug 16 '26
Not that hard to do by using the Intl#NumberFormat API https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat. You can even create a custom element to do that
1
u/fortyeightD Aug 16 '26
You can submit your proposal here: https://github.com/whatwg/html/issues
1
u/hazily Aug 17 '26
Let’s bring back <marquee> and <blink> while we are at it
1
u/mossy2100 Aug 18 '26
Good idea. I built a PHP package to do this (oceanmoon/quantities).
1
u/NADZ-Labs Aug 21 '26
Yep, I feel you. I've actually been experimenting with solving this at the browser-extension layer - detecting currencies and units like miles, °F, lbs, etc. and automatically displaying the user's preferred conversion alongside the original value.
It works surprisingly well, but the tricky part is exactly what a semantic element like this would solve: reliably understanding what a value represents from arbitrary page content. Having that information explicitly defined in the HTML would make the browser-side implementation much cleaner.
1
u/jalensailin Aug 15 '26
Good idea! If you have the time, make a custom component and report back!
1
u/qwerty_asd Aug 15 '26
Really good idea
-4
u/Awarnae Aug 15 '26
For what? HTML for markup, not for logic.
U need no load data from a backend... So u have any logic yet ... There's vue, react, abgular etc...
-5
u/Mark__78L Aug 15 '26
It's not the duty of html
2
u/[deleted] Aug 15 '26
[removed] — view removed comment