18

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?

Kaiido
  • 123,334
  • 13
  • 219
  • 285
user3162553
  • 2,699
  • 3
  • 37
  • 61

5 Answers5

8

One of the reasons why SVG file is rendered on front-end with zero height and width is missing <svg> tag attributes "height" and "width".

Incorrect:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 49 30">

Correct:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 49 30" width="49" height="30">
1

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>
Community
  • 1
  • 1
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
0

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.

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • The parent element is a div and it does have a height and width of 12px which currently show when inspected. – user3162553 Apr 30 '16 at 19:45
  • 1
    Create an example that shows the problem then. At the moment your snippet works in all browsers. So it is nearly impossible to help you solve your problem. – Paul LeBeau May 01 '16 at 01:48
0

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

Ryan Stone
  • 345
  • 4
  • 19
-3

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.

Pranay kumar
  • 1,983
  • 4
  • 22
  • 51
  • 1
    You've stated that you can wrap the svg in an Object element, but then just coied the code provided without giving an example of what you mean? – Ryan Stone Feb 23 '20 at 04:50
  • 1
    That's not how you use the element... This element loads a resource you would point to using its `data` attribute. – Kaiido Feb 24 '20 at 06:51