Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computing font-relative units in font-* properties #11629

Open
danburzo opened this issue Jan 31, 2025 · 1 comment
Open

Computing font-relative units in font-* properties #11629

danburzo opened this issue Jan 31, 2025 · 1 comment

Comments

@danburzo
Copy link

danburzo commented Jan 31, 2025

A bit of an odd case resulting from my attempt at implementing @Lorp’s font-optical-sizing: <number> proposal with current CSS capabilities:

:root {
  --ratio: 1
}

* {
  font-optical-sizing: 'opsz' calc(tan(atan2(1em * var(--ratio), 1px))); 
}

A Chrome developer pointed in a bug report that, technically, the spec says:

"When used in the value of any font-* property on the element they refer to, the font-relative lengths resolve against the computed metrics of the parent element—​or against the computed metrics corresponding to the initial values of the font and line-height properties, if the element has no parent."

Currently, as far as I can tell, it’s only the font-size and font shorthand that accept <length>s, so would it make sense to narrow down on font-size and font instead of font-* in this paragraph? With CSS gaining the ability to produce <number>s from <length>s, it may be beneficial to allow 1em to refer to the current font size in most cases, including some font-* properties.

@danburzo
Copy link
Author

danburzo commented Jan 31, 2025

I now understand that I’m very late to the party, and the change is in response to:

In any case, tan(atan2()) calculation doesn’t work as-is in current browsers. Instead, it requires an intermediary registered <length> property:

@property --1em {
	syntax: '<length>';
	initial-value: 0px;
	inherits: false;
}

:root {
	font-family: Newsreader;
	--opsz-ratio: 0.75;
}

* {
	font-variation-settings: 'opsz' 
		tan(atan2(var(--1em) * var(--opsz-ratio), 1px));
	--1em: 1em;
	padding-block: var(--1em);
}

As seen in the live demo, there is an interop issue:

  • Firefox (stable/nightly macOS) and Safari (stable/TP macOS) use the current element’s font-size to compute 1em.
  • In Chrome (stable/canary macOS), --1em = 16px if the font-variation-settings is present (and reflects on padding as well). If the font-variation-settings declaration is removed, --1em becomes the element’s font-size. [Chromium#392317688]

I am unsure what the spec-mandated behavior is here, I believe the tracking issues for that are these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant