Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active February 10, 2025 15:32
Show Gist options
  • Save daliborgogic/411365e0e17710922bb45c7164a356f3 to your computer and use it in GitHub Desktop.
Save daliborgogic/411365e0e17710922bb45c7164a356f3 to your computer and use it in GitHub Desktop.
Dynamic Metrics Inter font postcss
/* css variables does't work in math... */
/* using postcss-simple-vars */
$a: -0.0223;
$b: 0.185;
$c: -0.1745;
$lineHeight: 1.4;
$fontSize: 16;
@import url('https://rsms.me/inter/inter.css');
html {
font-family: 'Inter', sans-serif;
/*
InterDynamicLineHeight produces the line height
for the given font size
function InterDynamicLineHeight(fontSize) {
const l = 1.4
return Math.round(fontSize * l)
}
*/
font-size: $(fontSize)px;
line-height: resolve(round($(fontSize) * $(lineHeight)))px;
/*
InterDynamicTracking takes the font size in points
or pixels and returns the compensating tracking in EM.
function InterDynamicTracking(fontSize) {
const a = -0.0223
const b = 0.185
const c = -0.1745;
// tracking = a + b * e ^ (c * fontSize)
return a + b * Math.pow(Math.E, c * fontSize)
}
*/
letter-spacing: resolve(round(resolve($(a) + $(b) * pow(E, resolve($(c) * $(fontSize)))), 3))em;
}
@supports (font-variation-settings: normal) {
html { font-family: 'Inter var', sans-serif; }
}
@jerriclynsjohn
Copy link

Why did you pick a dynamic way to mention line height and tracking? Can you refer me to a place where I can read more about these equations and the reasons to use these over others?

@daliborgogic
Copy link
Author

@NikolayFrantsev
Copy link

I've created small postcss plugin to insert dynamic metrics, see https://www.npmjs.com/package/postcss-dynamic-metrics

@daliborgogic
Copy link
Author

@NikolayFrantsev please credit @rsms in npm package.

@NikolayFrantsev
Copy link

@daliborgogic I do believe what links to original author and idea are already there in source code and documentation, do you want something more?

@daliborgogic
Copy link
Author

daliborgogic commented Feb 10, 2025

@NikolayFrantsev it's ok from my side 👍🏻. I think @rsms will agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment