Get width/height of SVG element Get width/height of SVG element javascript javascript

Get width/height of SVG element


Use getBBox function

http://jsfiddle.net/Xkv3X/1/

var bBox = svg1.getBBox();console.log('XxY', bBox.x + 'x' + bBox.y);console.log('size', bBox.width + 'x' + bBox.height);


FireFox have problemes for getBBox(), i need to do this in vanillaJS.

I've a better Way and is the same result as real svg.getBBox() function !

With this good post : Get the real size of a SVG/G element

var el   = document.getElementById("yourElement"); // or other selector like querySelector()var rect = el.getBoundingClientRect(); // get the bounding rectangleconsole.log( rect.width );console.log( rect.height);


I'm using Firefox, and my working solution is very close to obysky. The only difference is that the method you call in an svg element will return multiple rects and you need to select the first one.

var chart = document.getElementsByClassName("chart")[0];var width = chart.getClientRects()[0].width;var height = chart.getClientRects()[0].height;