2

How to calculate the dimensions of a SVG element in a consistent way, cross browser ?

var svg = document.querySelector('svg');
var gBCR = svg.getBoundingClientRect().width;
var bBox = svg.getBBox().width;
var gCS = window.getComputedStyle(svg, null).width;
console.log('gBCR: ' + Math.round(gBCR) + ' - gCS: ' + gCS + ' - bBox: ' + Math.round(bBox));

// What I got:
// Firefox: gBCR: 142 - gCS: 400px - bBox: 363
// Chrome:  gBCR: 400 - gCS: 400px - bBox: 363 

http://jsfiddle.net/23x8S/

My svg element:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" height="200px">
    <title>HTML5 Logo</title>
    <polygon fill="#E44D26" points="107.644,470.877 74.633,100.62 437.367,100.62 404.321,470.819 255.778,512" />
    <polygon fill="#F16529" points="256,480.523 376.03,447.246 404.27,130.894 256,130.894             " />
    <polygon fill="#EBEBEB" points="256,268.217 195.91,268.217 191.76,221.716 256,221.716 256,176.305 255.843,176.305 142.132,176.305 143.219,188.488 154.38,313.627 256,313.627" />
    <polygon fill="#EBEBEB" points="256,386.153 255.801,386.206 205.227,372.55 201.994,336.333 177.419,336.333 156.409,336.333 162.771,407.634 255.791,433.457 256,433.399" />
    <path d="M108.382,0h23.077v22.8h21.11V0h23.078v69.044H152.57v-23.12h-21.11v23.12h-23.077V0z" />
    <path d="M205.994,22.896h-20.316V0h63.72v22.896h-20.325v46.148h-23.078V22.896z" />
    <path d="M259.511,0h24.063l14.802,24.26L313.163,0h24.072v69.044h-22.982V34.822l-15.877,24.549h-0.397l-15.888-24.549v34.222h-22.58V0z" />
    <path d="M348.72,0h23.084v46.222h32.453v22.822H348.72V0z" />
    <polygon fill="#FFFFFF" points="255.843,268.217 255.843,313.627 311.761,313.627 306.49,372.521 255.843,386.191 255.843,433.435 348.937,407.634 349.62,399.962 360.291,280.411 361.399,268.217 349.162,268.217" />
    <polygon fill="#FFFFFF" points="255.843,176.305 255.843,204.509 255.843,221.605 255.843,221.716 365.385,221.716 365.385,221.716 365.531,221.716 366.442,211.509 368.511,188.488 369.597,176.305" />
<svg>
Sergio
  • 28,539
  • 11
  • 85
  • 132

1 Answers1

3

Your approach seems right, It gives the right width in both cases, the problem is that the SVG component is rendered different in Firefox and Chrome, in Chrome use the 100% of available width and in Firefox use the minimum width to display the content.

This tracker explains why Firefox are not using "100%" as default width: https://bugzilla.mozilla.org/show_bug.cgi?id=611099

Roberto
  • 8,586
  • 3
  • 42
  • 53
  • Roberto, thank you for checking this out. What would be the right way to get the same number out of this? If I force the width of the SVG element to be `width="400px"` the problem still exists, now in a proporcional way to the first one: http://jsfiddle.net/2zn32/1/ – Sergio May 19 '14 at 06:43
  • 1
    Not only 100% width, but 100% height as well. Fixing both width and height (aspect ratio matching the viewport dimensions): http://jsfiddle.net/abhitalks/WDQ6X/2/ – Abhitalks May 19 '14 at 06:46
  • @Sergio, If you can use jQuery, using `$('svg').width()` returns 400 in Firefox and Chrome – Roberto May 19 '14 at 07:01
  • @Roberto, that was a nice pointer. jQuery uses `.getComputedStyle`. Updated my question width that info. Now just have to find a way to get consistent results in older browsers. Btw, just tested IE8 and jQuery returns `0` there... – Sergio May 19 '14 at 07:21
  • Does IE8 support SVG ? http://stackoverflow.com/questions/6097179/cant-display-svg-charts-in-internet-explorer-8/6097246#6097246, do you get to display correctly the logo ?, unfortunately I have not any IE8 browser to test it on. – Roberto May 19 '14 at 09:24