Created
September 27, 2012 15:18
-
-
Save maskingtape/3794574 to your computer and use it in GitHub Desktop.
Finds the width in ems of any string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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