Skip to content

Instantly share code, notes, and snippets.

@maskingtape
Created September 27, 2012 15:18
Show Gist options
  • Save maskingtape/3794574 to your computer and use it in GitHub Desktop.
Save maskingtape/3794574 to your computer and use it in GitHub Desktop.
Finds the width in ems of any string
// Finds the width in ems of any string
// Useful for finding out how wide elements set on the page in ems will be in pixels
function getEmWidth(chars){
var p = document.createElement("span");
p.innerHTML = chars;
document.body.appendChild(p);
var width = p.getBoundingClientRect().width;
document.body.removeChild(p);
return width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment