javascript – SVG element width and height set in html but appearing as 0 0 in inspector-ThrowExceptions

Exception or error:

I have an svg rect like this:

<svg class="legend-square">
  <defs>
    <pattern id="pattern1" width="3"
     height="3" patternunits="userSpaceOnUse" patterntransform="rotate(-45)">
      <rect width="2" height="3" transform="translate(0,0)" fill="purple"></rect>
    </pattern>
  </defs>
  <rect width="12" height="12" fill="url(#pattern1)"></rect>
</svg>

When I inspect the second rect with Chrome it has no width and height. enter image description here There are no CSS rules applying to it. Why doesn’t it get affected by width and height?

How to solve:

It works fine.

If the snippet works individually but size of div containing it in you code appears 0x0 then look into : Why is my div’s height zero

Its usually caused when float is set.

<div>
  <svg class="legend-square">
    <defs>
      <pattern id="pattern1" width="3" height="3" patternunits="userSpaceOnUse" patterntransform="rotate(-45)">
        <rect width="2" height="3" transform="translate(0,0)" fill="purple"></rect>
      </pattern>
    </defs>
    <rect width="12" height="12" fill="url(#pattern1)"></rect>
  </svg>
</div>

###

You really haven’t provided enough information. For instance, what is the parent element of your SVG?

Just because your <rect> has a width and hight, it doesn’t mean your SVG does. SVG is not like HTML, where elements expand to fit their children. SVG is like the <canvas> element. You have to make sure it either explicitly (or implicitly) has a size.

You have not specified width or height attributes for your <svg> element, so they are both defaulting to “100%”. What they are 100% of depends on what size the SVG’s parent element is. Hence my first question above.

###

For an ‘inline’ SVG element you have to apply the styles, inside the svg element itself.
inside the <defs></defs> tags. Since it has its Own DOM[Document Object Model], Css Apllied to it from Outside, will have no effect.

You have Several different options for embeding an SVG, You can use it inline as in your example where you declare the <svg></svg> inside of your html/php document or you can use one of the many other methods listed below;

embed an: ……………………………………….<img src="../pathtoyourSvg"></img>
embed as an img with a fallback option: <img src="logo.png" srcset="logo.svg" alt="My logo">

use an: …………………………………………….<object type="image/svg+xml" data="image.svg">
<!-- Your fall back here -->
<img src="image.svg" />
</object>

use: …………………………………………………<embed type="image/svg+xml" src="image.svg" />
use an: …………………………………………….<iframe></iframe>

embed the Svg inside of a canvas element using Javascript:

var img = new Image();
img.onload = function() {
    ctx.drawImage(img, 0, 0);
}
img.src = "path2your.svg";

You may Also Embed the Svg using Css Background image,
ie:

.mySvgContainer{
   background-image:url('PathToMySvg');
}

But from the comments & Questions I have read, this need to be either Url or Base64 encoded, which seems a bit Hacky and not very convenient.

each of them have their own advantages and disadvantages.

Because of security reasons, some SVG embedding methods will block access to external resources including CSS, fonts and javascript. Especially when we have multiple images, ideally our embedding method should be able to refer to a single CSS, font or javascript file (to save resources) and be able to manipulate our embedded SVG.

Also worth mentioning that if you have display:flex; attached to an elemnet or its parent then the width and height values will have no effect on the flex items. So It may appear as 0, 0, in the console.

Some Useful Information & related questions:
Svg Coords & Units w3.org
Svg Width – Height
Applying Styles to an embedded Svg

###

You can wrap your svg element inside the html5 object element.

<style type="text/css">
object{
width:300px;
height:200px;
}
object svg{
width:100%;
height:100%;
}
</style>

<object>
<svg class="legend-square">
  <defs>
    <pattern id="pattern1" width="3"
     height="3" patternunits="userSpaceOnUse" patterntransform="rotate(-45)">
      <rect width="2" height="3" transform="translate(0,0)" fill="purple"></rect>
    </pattern>
  </defs>
  <rect width="12" height="12" fill="url(#pattern1)"></rect>
</svg>
</object>



You can set height and width on the object element just link any other html element while you can set height width to 100% for SVG element. It will work. But first, test your SVG by opening the file directly in chrome.

Leave a Reply

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