How to resize a svg icon (use href) - svg

I have all my icons added as symbols in the html file, eg:
<symbol id="flag-bg"><g fill-rule="evenodd" stroke-width="1pt"><path fill="#d62612" d="M0 319.997h640V480H0z"/><path fill="#fff" d="M0 0h640v160.003H0z"/><path fill="#00966e" d="M0 160.003h640v160.003H0z"/></g></symbol>
And I want to use it to display an icon of 32 by 24
<svg width="32" height="24" viewbox="0 0 32 24" id="#lg-bg"><use href="#flag-bg"><title></title></use></svg>
But it doesn't resize the flag to fill the size. What's the magic properties to make it happen?

You have to set a viewBox attribute on the <symbol> element that matches the size of its grafical content. To find out what size that is, you can use the .getBBox() function. Temporarily exchange <symbol> for <svg>, and don't mind if the grafical output makes no sense. You only need the numbers on the console:
console.log(document.querySelector('#flag-bg').getBBox())
<svg>
<svg id="flag-bg">
<g fill-rule="evenodd" stroke-width="1pt">
<path fill="#d62612" d="M0 319.997h640V480H0z"/>
<path fill="#fff" d="M0 0h640v160.003H0z"/>
<path fill="#00966e" d="M0 160.003h640v160.003H0z"/>
</g>
</svg>
</svg>
Then, use these values for your viewBox. There is no need to set a size on the <use> element, it defaults to 100% (of the surrounding <svg>) element.
<svg width="0" height="0">
<symbol id="flag-bg" viewbox="0 0 640 480">
<g fill-rule="evenodd" stroke-width="1pt">
<path fill="#d62612" d="M0 319.997h640V480H0z"/>
<path fill="#fff" d="M0 0h640v160.003H0z"/>
<path fill="#00966e" d="M0 160.003h640v160.003H0z"/>
</g>
</symbol>
</svg>
<svg width="32" height="24" viewbox="0 0 32 24" id="#lg-bg">
<use href="#flag-bg"><title></title></use>
</svg>

you can use viewbox property to scale your svg icon
check this code and reference link in below
<h1>Auto-Scaling an SVG Image with a <code>viewBox</code>
<div>—watch out for IE and table layouts</div></h1>
<figure>
<figcaption><code>img</code>, 100xauto height</figcaption>
<div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/91525/potofgold.svg"
style="width:100px">
</div>
</figure>
<figure>
<figcaption><code>img</code>, auto widthx50</figcaption>
<div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/91525/potofgold.svg"
style="height:50px">
</div>
</figure>
<figure>
<figcaption><code>img</code>, auto size</figcaption>
<div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/91525/potofgold.svg">
</div>
</figure>
you can use viewbox property and scale your svg icons in it ..
reference link clickhere

Related

The .svg in my header.php will not change size

I inherited this site and am trying to change the Logo.
I managed to paste new .svg in the place of the old one but the new one will not resize at all.
<body <?php body_class(); ?>>
<svg viewBox="0 0 100 100" width="100%" height="auto" style="display: none">
<symbol id="vestoj-logo">
<title><?php bloginfo( 'name' ); ?></title>
<path d="M0,.19H3.29l1.21,4.08c.1,.34,.17,.85,.2,1.16h.08c.01-.35,.08-.79,.18-1.16L6.14,.19h3.29l-3.35,8.67H3.37L0,.19Z"/>
<path d="M9.58,8.87V.19h5.85V2.57h-2.7v.81h2.46v2.28h-2.46v.83h2.78v2.38h-5.93Z"/>
<path d="M21.6,2.96c-.38-.26-1.07-.65-1.77-.65-.17,0-.43,.03-.6,.16-.12,.09-.16,.18-.16,.31,0,.18,.03,.35,.51,.45l1,.22c.77,.17,1.22,.48,1.48,.74,.55,.53,.66,1.2,.66,1.72,0,.74-.2,1.43-.78,2.04-.49,.51-1.43,1.1-3.05,1.1-1.33,0-2.21-.38-2.95-.68l-.29-.12,.95-2.44,.34,.26c.33,.25,.99,.64,1.75,.64,.43,0,.61-.1,.68-.16,.14-.12,.17-.26,.17-.39,0-.08-.01-.21-.1-.3-.1-.1-.27-.14-.6-.21l-.77-.14c-.49-.09-1.03-.25-1.51-.74-.44-.46-.62-.98-.62-1.6,0-.81,.27-1.5,.84-2.07,.66-.68,1.64-1.12,3.02-1.12,.69,0,1.59,.12,2.64,.75l-.84,2.21Z"/>
<path d="M24.36,8.87V2.94l-1.77,.08V.19h6.63V3.02l-1.77-.08v5.93h-3.09Z"/>
<path d="M37.56,7.67c-.74,.75-1.95,1.39-3.6,1.39s-2.86-.64-3.6-1.39c-.68-.69-1.21-1.75-1.21-3.15s.53-2.44,1.21-3.13c.74-.75,1.95-1.39,3.6-1.39s2.86,.64,3.6,1.39c.68,.69,1.21,1.75,1.21,3.13s-.53,2.46-1.21,3.15Zm-4.73-4.43c-.31,.33-.49,.83-.49,1.3,0,.49,.18,.98,.49,1.29,.26,.26,.65,.44,1.13,.44s.87-.18,1.13-.44c.29-.29,.49-.74,.49-1.29s-.18-1-.49-1.3c-.23-.23-.61-.44-1.13-.44-.56,0-.92,.22-1.13,.44Z"/>
<path d="M40.3,.19h3.09V6.24c0,.95-.44,1.59-.78,1.94-.57,.6-1.37,.88-2.17,.88-.6,0-1.43-.13-2.24-.7l-.35-.25,1.14-2.68,.06,.22c.05,.18,.17,.43,.29,.53,.09,.09,.22,.16,.4,.16,.16,0,.27-.05,.36-.16,.17-.19,.18-.51,.18-.64V.19Z"/>
</symbol>
</svg>
I tried changing the viewBox and the width= to other values but nothing changes.

Scale more than one dynamically loaded SVG images in a single parent container -Angular7

I have a requirement where I need to add on SVG images one after another(stack vertically) and it should scale accordingly without any scrollbar for parent container in Angular7. The images are added dynamically. My problem is I don't know how to scale these images with respect to the parent container (There is only one single parent container for all the SVG's) and there should not be any scrollbar to view the svgs. The SVG images should scale accordingly.
My HTMl is like this:
<div id="stackedSVGParentContainer">
<svg width="16" height="4.5" viewBox="0 0 16 5" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0.738516 4.5L2.33852 0.5H13.6615L15.2615 4.5H2.33852Z" fill="#E0E0E0" stroke="#212121"/>
<line x1="1" y1="1.75" x2="15" y2="1.75" stroke="#262121" stroke-width="0.5"/>
<line x1="2" y1="3.25" x2="14" y2="3.25" stroke="#262121" stroke-width="0.5"/>
</svg>
<svg width="16" height="135" viewBox="0 0 16 150" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect _ngcontent-ng-cli-universal-c326="" x="2.5" y="25.5" width="11" height="99" fill="#E0E0E0" stroke="#262121"></rect>
</svg>
<svg width="16" height="135" viewBox="0 0 16 150" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect _ngcontent-ng-cli-universal-c326="" x="2.5" y="25.5" width="11" height="99" fill="#E0E0E0" stroke="#212621"></rect>
</svg>
</div>

SVG feImage filter clips stroke - how to stop that?

I'm trying to create a filter for only the stroke of path in SVG, but the feImage keeps getting clipped to what I assume is that bounding box (see green rectangle below in code as bounding box). I've tried setting the filter's x/y and width/height to all sorts of positions/sizes, as seems to be the prevailing advice, but nothing works. x/y just offset the feImage and width/height of greater than 100% has no effect.
I won't know in advance if the stroke or fill is solid or something else (like linearGradient.
The below demonstrates what I'm looking to do - just get the stroke of a shape (regardless of size or fill) and apply a filter to it for all modern browsers.
Notes: FF doesn't even display the left-hand feImage. Chrome clips the left and top. Edge clips all 4 sides.
<html>
<body>
<svg width="960" height="540" >
<rect width="960" height="540" stroke="#385D8A" fill="white" stroke-width="3"/>
<svg name="BoundingBox1" class="rect" x="100" y="100" overflow="visible" fill="none" stroke="#00ff00" stroke-width="1">
<path d="M0,0L121.68,0L121.68,121.68L0,121.68Z" />
</svg>
<!-- BELOW IS MODIFIED "SPEECH" SHAPE. NEED A FILTER ON THE STROKE ONLY. -->
<svg name="Speech-strokeonly" x="100" y="100" overflow="visible" fill="blue" stroke="orange" stroke-width="12" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<path d="M35.541,137.084L30.985,113.988A60.926,60.926 0 1 1 53.043,121.34Z" id="strokeOnly" fill="none"/>
<filter id="effs0sp9" color-interpolation-filters="sRGB" x="0" y="0">
<feImage xlink:href="#strokeOnly" />
</filter>
</defs>
<path d="M35.541,137.084L30.985,113.988A60.926,60.926 0 1 1 53.043,121.34Z" id="effs0sp9" filter="url(#effs0sp9)" overflow="visible" />
<text y="160" stroke="black" stroke-width="0.2">this is the one that has undesirable clipping of</text>
<text y="178" stroke="black" stroke-width="0.2">of stroke in Chrome/Edge. Doesn't appear at all in FF</text>
</svg>
<!-- BELOW IS ORIGINAL WITH BOTH FILL AND STROKE -->
<svg name="BoundingBox2" x="400" y="100" overflow="visible" fill="none" stroke="green" stroke-width="1">
<path d="M0,0L121.68,0L121.68,121.68L0,121.68Z" />
</svg>
<svg name="Speech-original" x="400" y="100" overflow="visible" fill="blue" stroke="orange" stroke-width="12">
<path d="M35.541,137.084L30.985,113.988A60.926,60.926 0 1 1 53.043,121.34Z" id="effs0sp9" />
<text y="-22" stroke="black" stroke-width="0.2">this is the original one</text>
<text y="-6" stroke="black" stroke-width="0.2">that I just want the stroke as feImage from</text>
</svg>
</svg>
</body>
</html>
Is there a way to grab the whole stroke of a shape only and use in a filter?

How inherit stroke color in SVG? (Not fill, but stroke color)

I have an .SVG where I call with <img src="/image/arrow.svg" alt="Arrow">. Everything is good, but I would like to dynamically set a different stroke color (Not fill color...) for my SVG like <img ... style="color:red">. I read that I could use fill="currentColor" on my path,but how could I do for my stroke color?
My SVG file:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height="100" width="100">
<path d="M20 10 H90 V80" fill="transparent" stroke="currentColor" stroke-width="20" stroke-linecap="round"></path>
<path d="M10 90 L100 0" fill="transparent" stroke="currentColor" stroke-width="20" stroke-linecap="round"></path>
</svg>
My html:
<img src="/image/arrow.svg" alt="Arrow" style="color:red">
As Robert Lognson correctly stated, and previously discussed on StackOverflow, svg used as an image element has a new image context, thus it does not use the document's styles, whereas an inline svg element does use them.
So the following example works:
svg.red {
color: red;
}
<svg class="red" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height="100" width="100">
<path d="M20 10 H90 V80" fill="transparent" stroke="currentColor" stroke-width="20" stroke-linecap="round"></path>
<path d="M10 90 L100 0" fill="transparent" stroke="currentColor" stroke-width="20" stroke-linecap="round"></path>
</svg>

Adding svg to page from svg sprite

I'm using grunt-svgstore in my grunt set up to create a svg sprite file from svg I add to my project.
The output is something like this.
<svg xmlns="http://www.w3.org/2000/svg"><symbol viewBox="0 0 32 32" id="shape-times"><title>times</title><desc>Created with Sketch.</desc> <!-- Generator: Sketch 3.4.4 (17249) - http://www.bohemiancoding.com/sketch --> <g id="times-Icons" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage"> <g id="times-times" sketch:type="MSArtboardGroup" fill="#B7C0C7"> <path d="M17.4142136,16 L23.7764339,9.63777963 C24.1745374,9.23967616 24.1686989,8.6123497 23.7781746,8.22182541 C23.3849276,7.82857845 22.753706,7.83208044 22.3622204,8.22356607 L16,14.5857864 L9.63777963,8.22356607 C9.23967616,7.8254626 8.6123497,7.83130112 8.22182541,8.22182541 C7.82857845,8.61507236 7.83208044,9.246294 8.22356607,9.63777963 L14.5857864,16 L8.22356607,22.3622204 C7.8254626,22.7603238 7.83130112,23.3876503 8.22182541,23.7781746 C8.61507236,24.1714215 9.246294,24.1679196 9.63777963,23.7764339 L16,17.4142136 L22.3622204,23.7764339 C22.7603238,24.1745374 23.3876503,24.1686989 23.7781746,23.7781746 C24.1714215,23.3849276 24.1679196,22.753706 23.7764339,22.3622204 L17.4142136,16 L17.4142136,16 Z" sketch:type="MSShapeGroup"/> </g> </g> </symbol></svg>
At the top of my document I'm adding a reference to the svg file.
<?php include_once("images/svg/processed/svg-defs.svg"); ?>
Whats the best way or how do I show the svg on the page. I'm trying things like this.
<object type="image/svg+xml" width="100" height="100" data="#times-Icons"></object>
To can reference a <symbol> element with a <use> element.
You should include the SVG with the symbols at the top of your document. Actually it could go anywhere, but I believe Safari has a bug which requires the definitions to be before the references to them.
You will need to hide the symbol definition SVG so it is not visible. To do that can either use display="none" or set width="0" height="0".
Then to reference the symbol, create another SVG of the desired size with a <use> element.
<svg width="40px" height="40px">
<use xlink:href="#shape-times"/>
</svg>
You can set the size explicitly as above, or use CSS as in the following example.
.large {
width: 40px;
height: 40px;
}
.small {
width: 24px;
height: 24px;
}
<svg xmlns="http://www.w3.org/2000/svg" display="none"><symbol viewBox="0 0 32 32" id="shape-times"><title>times</title><desc>Created with Sketch.</desc> <!-- Generator: Sketch 3.4.4 (17249) - http://www.bohemiancoding.com/sketch --> <g id="times-Icons" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage"> <g id="times-times" sketch:type="MSArtboardGroup" fill="#B7C0C7"> <path d="M17.4142136,16 L23.7764339,9.63777963 C24.1745374,9.23967616 24.1686989,8.6123497 23.7781746,8.22182541 C23.3849276,7.82857845 22.753706,7.83208044 22.3622204,8.22356607 L16,14.5857864 L9.63777963,8.22356607 C9.23967616,7.8254626 8.6123497,7.83130112 8.22182541,8.22182541 C7.82857845,8.61507236 7.83208044,9.246294 8.22356607,9.63777963 L14.5857864,16 L8.22356607,22.3622204 C7.8254626,22.7603238 7.83130112,23.3876503 8.22182541,23.7781746 C8.61507236,24.1714215 9.246294,24.1679196 9.63777963,23.7764339 L16,17.4142136 L22.3622204,23.7764339 C22.7603238,24.1745374 23.3876503,24.1686989 23.7781746,23.7781746 C24.1714215,23.3849276 24.1679196,22.753706 23.7764339,22.3622204 L17.4142136,16 L17.4142136,16 Z" sketch:type="MSShapeGroup"/> </g> </g> </symbol></svg>
<svg width="40px" height="40px">
<use xlink:href="#shape-times"/>
</svg>
<svg class="large">
<use xlink:href="#shape-times"/>
</svg>
<svg class="small">
<use xlink:href="#shape-times"/>
</svg>

Resources