Skip to content

Instantly share code, notes, and snippets.

@aminnj
Created June 9, 2021 02:34
Show Gist options
  • Save aminnj/5ca372aa2def72fb017b531c894afdca to your computer and use it in GitHub Desktop.
Save aminnj/5ca372aa2def72fb017b531c894afdca to your computer and use it in GitHub Desktop.
arial character widths
var d1 = document.createElement("div");
d1.id = "test3";
d1.innerHTML = "test";
d1.style = "width: fit-content; padding: 0px; margin: 0px; font-family: Arial, sans-serif;";
document.documentElement.appendChild(d1);
var d2 = document.createElement("div");
d2.id = "test3";
document.documentElement.appendChild(d2);
d2.innerHTML = "";
var N = 100;
for (var i = 32; i < 127; i++) {
var s = String.fromCharCode(i).replace(" ","&nbsp;");
d1.innerHTML = s.repeat(N);
var width = d1.getBoundingClientRect().width;
d2.innerHTML += `"${s}": ${width/N},<br>`;
}
@aminnj
Copy link
Author

aminnj commented Oct 30, 2024

Awesome, thanks!

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