Sometimes, we want to get text element width of an SVG with JavaScript.
In this article, we’ll look at how to get text element width of an SVG with JavaScript.
How to get text element width of an SVG with JavaScript?
To get text element width of an SVG with JavaScript, we can use the getBBox
method.
For instance, we write
const bbox = textElement.getBBox();
const width = bbox.width;
const height = bbox.height;
to call textElement.getBBox
to get the bbox
object from the text
element in the SVG.
Then we get the dimensions from the width
and height
properties of bbox
.
Conclusion
To get text element width of an SVG with JavaScript, we can use the getBBox
method.