Get the text width (width of the inline box) of the HTMLElement
Demo
Examples
<script type="module">
import HtmlElementText from './dist/HtmlElementText.js';
for (const textElement of document.querySelectorAll('.js-text-width')) {
const htmlElementText = new HtmlElementText(textElement);
console.debug(`${htmlElementText.getWidth()}px`);
}
</script>
<span class="js-text-width">Hello world!</span>
Constructor
new HtmlElementText(
thisElement: HTMLElement
)
Parameters
-
thisElement
[required] - Target element
Methods
getWidth(text = <string>this.#thisElement.textContent): number
- Get the text width (the width of that inline box)