Categories
JavaScript Answers

How to get text element width of an SVG with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *