Skip to content

[css-values] Percentages and unit arithmetic #14300

Description

@emilio

So for context, this discussion comes from this Firefox commit.

Consider this:

z-index: calc(10% / 10%);
line-height: calc(10% / 10%);
opacity: calc(10% / 10%);

It seems to me these should all behave identically, either computing to 1 (z-index: 1 / line-height: 1 / opacity: 1), or being rejected?

That doesn't match browser behavior tho. There's some informal notes in https://drafts.csswg.org/css-values-4/#calc-simplification about how percentages generally might block simplification vs. not:

However, "raw" percentages—​ones which do not resolve against another value, such as in opacity—​might not block simplification.

However browsers behave differently here, and it's unclear from the spec what the right behavior is.

Similar weird example would be this test, effectively line-height: calc(10% / 1px), expecting it to compute to a number:

<!doctype html>
<style>
 body {
   font-size: 10px;
 }
 #target {
   line-height: calc(10% / 1px);
 }
</style>
<div id=target>What's my line height?</div>
<pre><script>
  document.writeln(getComputedStyle(target).lineHeight);
  document.writeln(target.computedStyleMap().get("line-height").toString());
</script></pre>

I think making it compute to a number and making the percentage disappear (what Chromium does) is wrong. Because percentage line-heights generally inherit differently...

cc @kbhomes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions