I'm trying to use SVG elements in a fluid context, using %
units to set the size proportional to the container.
If you include an SVG design like an image it works just like an image:
<img class="img-svg" src="box.svg" />
You can use CSS to easily scale it proportionally with stuff like this:
.img-svg { width:100%; height: auto; }
If you use inline SVG, everything changes...
<svg class="inline-svg" preserveAspectRatio="xMidYMid meet" viewBox="...">
...
</svg>
This WON'T work:
.inline-svg { display:block; width:100%; height: auto; }
If you do this it will expand the SVG's box to 100% but all the elements inside it wills stay the same size. Here's a jsfiddle: http://jsfiddle.net/s_d_p/jA62R/
What's the right way to do this so that the contents inside the SVG scale up/down uniformly?
Note1: I'm looking for a CSS/SVG solution. Not a javascript hack.
NOte2: I found this but it relies on fixed px
units.
Just set the viewBox on your <svg> , and set one of height or width to auto . The browser will adjust it so that the overall aspect ratio matches the viewBox .
❓ How can I resize a SVG image? First, you need to add a SVG image file: drag & drop your SVG image file or click inside the white area to choose a file. Then adjust resize settings, and click the "Resize" button. After the process completes, you can download your result file.
We use viewBox to make the SVG image scalable. viewBox = “0 0 100 100”: defines a coordinate system having x=0, y=0, width=100 units and height=100 units. Thus, the SVG containing a rectangle having width=50px and height=50px will fill up the height and width of the SVG image, and all the dimensions get scaled equally.
An SVG image with fixed dimensions will be treated just like a raster image of the same size. Note: If you are trying to stretch your SVG to a different aspect ratio with CSS—for example in order to stretch it over the page background—make sure your SVG includes preserveAspectRatio="none" .
I'm still getting my head around this, but here's the best I've come up with so far...
.svg {
display: block;
width:100%;
height: auto;
}
<svg preserveAspectRatio="xMidYMid meet" viewBox="...">
...
</svg>
The CSS needs to set the display to block because the default for SVG is inline
(not sure why). Then you can scale it's boundary like a div.
The way you get the contained svg nodes to play nicely also is by using the preserveAspectRatio
attribute. As Robert pointed out in a comment above, the preserveAspectRatio attribute doesn't work if you don't have a viewBox
attribute as well.
It's worth reading more about preserveAspectRatio here because there a lot if different values which will determine how the scaled elements interact with the surrounding space if the dimensions are not the same. For example, if your SVG is a short wide rectangle and your container is a tall skinny rectangle, what should the browser do with the extra space to make it fit?
[Be careful! The browser defaults for preserveAspectRatio
differ a lot!]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With