Last active
March 31, 2024 18:48
-
-
Save pointofpresence/e80046fab19a2d864810996793baa738 to your computer and use it in GitHub Desktop.
Получение ширины строки текста
This file contains 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
function getTextWidth() { | |
text = document.createElement("span"); | |
document.body.appendChild(text); | |
text.style.font = "times new roman"; | |
text.style.fontSize = 16 + "px"; | |
text.style.height = 'auto'; | |
text.style.width = 'auto'; | |
text.style.position = 'absolute'; | |
text.style.whiteSpace = 'no-wrap'; | |
text.innerHTML = 'Hello World'; | |
width = Math.ceil(text.clientWidth); | |
formattedWidth = width + "px"; | |
document.querySelector('.output').textContent | |
= formattedWidth; | |
document.body.removeChild(text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment