Skip to content

Instantly share code, notes, and snippets.

@pointofpresence
Last active March 31, 2024 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pointofpresence/e80046fab19a2d864810996793baa738 to your computer and use it in GitHub Desktop.
Save pointofpresence/e80046fab19a2d864810996793baa738 to your computer and use it in GitHub Desktop.
Получение ширины строки текста
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