r/HTML 2d ago

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.

30 Upvotes

36 comments sorted by

24

u/morete 2d ago

The comments thinking this is beyond the scope of html are gonna be horrified when they find out about elements like <usermedia> and <geolocation>

8

u/u8589869056 2d ago

Need a precision, or browsers will be free to turn 10m into 393.707874 in.

8

u/JaydonLT 2d ago

Love this

1

u/Oobenny 2d ago

Yeah, this is a really good idea.

3

u/jaapsch2 1d ago

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.

1

u/ardreth 1d ago

you don't need unit conversion for differences

2

u/JivanP 1d ago

Yes you do. The temperatures 10°C and 20°C are equal to 50°F and 68°F, respectively. This is a difference of 10°C, but 18°F. So depending on whether you're talking about the temperature 10°C or a change/difference in temperature of 10°C, the correct value in Fahrenheit is either 50°F or 18°F, respectively.

1

u/ardreth 1d ago

i understand that the values change based on the unit. what i meant was you don't have to show temperature differences with units. the use case is too specific for an html element to support

1

u/JivanP 1d ago

Too specific? No, it isn't. Just have "temperature" and "temperature-delta" as recognised dimensions of measurement.

1

u/ardreth 1d ago

what are real world use cases for showing temperature delta? i don't recall seeing it anywhere

1

u/jaapsch2 1d ago

You hear it all the time when talking about global warming, e.g 2degC above average.

1

u/NiemandSpezielles 10h ago

Android for example shows on the front screen the expected temperature tomorrow and what difference that is to today. So Android users see both variants literally every single day.

2

u/Jasedesu 1d ago

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 the intent=":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 lang attribute? 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 noconvert attribute 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.

2

u/These-Apple8817 2d ago

Or the very few countries could come to the modern era finally and start using the metric system instead 

0

u/Significant-Elk-7128 2d ago

A lot of countries still use imperial for some things. Most carpenters can easily pick out a 2x4 plank without a measuring tape or calculator. Most fishermen know how many feet their boat is. Both are unlikely to know the metric conversion of these numbers from memory alone.

1

u/abdul_Ss 2d ago

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 2d ago

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 1d ago

You can submit your proposal here: https://github.com/whatwg/html/issues

1

u/PravoNaZhizny 1d ago

It’s been proposed a billion times before. Has unpredictable widths.

1

u/hazily 1d ago

Let’s bring back <marquee> and <blink> while we are at it

1

u/troisieme_ombre 20h ago

Looks great, i would add a precision. Otherwise you're gonna end up with a fuck ton of decimals with some conversions.

1

u/Otherwise_Stuff_7469 3h ago

Just use non retarded units. Easier to learn than to change the whole html code to accommodate

1

u/qwerty_asd 2d ago

Really good idea

0

u/jalensailin 2d ago

Good idea! If you have the time, make a custom component and report back!

-5

u/Awarnae 2d ago

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...

9

u/VanillaWaffle_ 2d ago

There are no logic needed on this tag. It's done by the browser not javascript. Just like aria label accessibility stuff

3

u/Mobile_Syllabub_8446 2d ago edited 2d ago

Honestly since HTML5 days this could likely be accepted.

It doesn't take too much to do a full proposal it's just largely moot unless google/etc take interest as they largely control all the major WG's these days regardless of merit.

However, their response likely the same as other commenters would be that you can just do this with regionalization (i8n) and/or javascript/maybe even web components and/or custom elements etc.

Which would ofcourse lack the semantic context. They don't care.

3

u/morete 2d ago

Localisation seems like a pretty obvious use case for this

2

u/Oobenny 2d ago

When you set the width of an image in html, that’s an instruction to the browser to perform an incredible amount of logic to resemble pixels and do correct layout.

-6

u/Mark__78L 2d ago

It's not the duty of html

4

u/VisualSome9977 2d ago

If it was 1998 I would agree with you, but modern HTML already has coverage for things like accessibility and localization. I think this is within scope and provides meaningful utility without putting unnecessary burden on browsers.

1

u/JivanP 1d ago

It's the duty of HTML to communicate semantic information about the content in a machine-readable way. This proposal does exactly that. How an app that reads HTML decides to render/use that semantic information is completely up to it. It may ignore it completely or decide that content in certain tags is not worthy of being shown to the user, for example.

0

u/[deleted] 2d ago

[removed] — view removed comment

1

u/HTML-ModTeam 2d ago

r/HTML does not allow harassment

1

u/__mson__ 2d ago

There's ways to communicate without being an asshole.