Expected Behavior:
Icon to fit only the area of the 24 x 24 viewbox.
Actual Behavior:
The svg tag takes up the correct area of 24 x 24 but the path element is not contained inside of the 24 x 24 area.
How would I be able to have the path be constrained to the 24 x 24 area and not go over? Currently this is what my svg tag looks like.
<a href="#">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M15.99 2a13.99 13.99 0 0 0-5.1 27.03c-.13-1.11-.23-2.81.05-4.02l1.64-6.96s-.41-.84-.41-2.07c0-1.95 1.13-3.4 2.53-3.4 1.2 0 1.77.9 1.77 1.97 0 1.2-.76 2.99-1.16 4.66-.33 1.39.7 2.53 2.07 2.53 2.49 0 4.4-2.63 4.4-6.4 0-3.35-2.41-5.69-5.85-5.69a6.05 6.05 0 0 0-6.32 6.07c0 1.2.46 2.49 1.04 3.19.12.14.13.26.09.4l-.39 1.59c-.06.25-.21.31-.47.18-1.73-.83-2.81-3.39-2.81-5.44 0-4.41 3.2-8.47 9.25-8.47 4.85 0 8.63 3.46 8.63 8.09 0 4.83-3.04 8.71-7.26 8.71-1.42 0-2.75-.74-3.2-1.61l-.88 3.33a15 15 0 0 1-1.74 3.67A13.97 13.97 0 0 0 30 16.01 14.02 14.02 0 0 0 15.99 2z">
</path>
</svg>
</a>
Remember, the viewBox doesn't have to match your dimensions, so adjust your viewBox - that's what it's there for! The viewBox settings below seems to work. (I scaled up the SVG size and added a 1px border red to show the effect).
svg {
border: 1px solid red;
}
<svg xmlns="http://www.w3.org/2000/svg" width="240px" height="240px" viewBox="1.6 2 28.73 27.99">
<path d="M15.99 2a13.99 13.99 0 0 0-5.1 27.03c-.13-1.11-.23-2.81.05-4.02l1.64-6.96s-.41-.84-.41-2.07c0-1.95 1.13-3.4 2.53-3.4 1.2 0 1.77.9 1.77 1.97 0 1.2-.76 2.99-1.16 4.66-.33 1.39.7 2.53 2.07 2.53 2.49 0 4.4-2.63 4.4-6.4 0-3.35-2.41-5.69-5.85-5.69a6.05 6.05 0 0 0-6.32 6.07c0 1.2.46 2.49 1.04 3.19.12.14.13.26.09.4l-.39 1.59c-.06.25-.21.31-.47.18-1.73-.83-2.81-3.39-2.81-5.44 0-4.41 3.2-8.47 9.25-8.47 4.85 0 8.63 3.46 8.63 8.09 0 4.83-3.04 8.71-7.26 8.71-1.42 0-2.75-.74-3.2-1.61l-.88 3.33a15 15 0 0 1-1.74 3.67A13.97 13.97 0 0 0 30 16.01 14.02 14.02 0 0 0 15.99 2z">
</path>
</svg>
The problem is with your SVG path. It is bleeding outside the view box. This is what it looks like in Illustrator:
This is after editing the path in illustrator to contain it to the view box:
a {
display: inline-block;
}
<a href="#">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M12 0C5.4 0 0 5.3 0 11.9c0 5 3 9.4 7.6 11.2-.1-1-.2-2.4 0-3.4l1.4-6s-.4-.7-.4-1.8c0-1.7 1-2.9 2.2-2.9 1 0 1.5.8 1.5 1.7 0 1-.7 2.6-1 4-.3 1.2.6 2.2 1.8 2.2 2.1 0 3.8-2.3 3.8-5.5 0-2.9-2.1-4.9-5-4.9-2.9-.1-5.3 2.1-5.4 4.9v.3c0 1 .4 2.1.9 2.7.1.1.1.2.1.3l-.3 1.4c-.1.2-.2.3-.4.2-1.5-.7-2.4-2.9-2.4-4.7 0-3.8 2.7-7.3 7.9-7.3 4.2 0 7.4 3 7.4 6.9 0 4.1-2.6 7.5-6.2 7.5-1.2 0-2.4-.6-2.7-1.4l-.8 3c-.4 1.1-.9 2.2-1.5 3.1 6.3 2 13-1.6 15-7.9.4-1.2.5-2.4.5-3.6C24 5.4 18.6 0 12 0z"/>
</path>
</svg>
</a>
Also, adding display: inline-block to your anchor tag allows the SVG to take up it's space.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 days ago.
Improve this question
Currently, I have this:
const regex = /<svg.*?width="(.*?)".*?height="(.*?)".*?>(.*)<\/svg>/m;
const fileContent = await fs.readFile(svgFile, 'utf8');
const m = regex.exec(fileContent);
m gives me this:
[
'<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12"><path fill="currentColor" d="M1.5 7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3ZM10.5 7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3ZM6 7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z"/></svg>',
'12',
'12',
'<path fill="currentColor" d="M1.5 7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3ZM10.5 7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3ZM6 7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z"/>',
index: 0,
input: '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12"><path fill="currentColor" d="M1.5 7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3ZM10.5 7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3ZM6
7.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z"/></svg>',
groups: undefined
]
What I want to get instead is this:
m[0] = '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12"></svg>' (only the svg without the child-elements, or only the svg attributes, all of them, which could vary depending on the svg icon. Some may have fill attribute, some not.)
m[1] = the path element like in m[3].
How do I do that?
How can I use stroke-offset to animating path in this SVG? I very appreciate it if anyone can help me.
<svg xmlns="http://www.w3.org/2000/svg" id="logo" width="200" viewBox="0 0 132.64 140.91">
<path id="path1" d="M123.26 15.4C115.58 5 103.78 0 87.17 0H0v6.73h87.17c15.08 0 25.63 4.35 32.26 13.3 7.25 9.78 7.93 18.38 7.93 38.71v23.32c0 20.4-.68 29-7.93 38.83-8.65 11.67-20.62 13.29-32.26 13.29H5.27V33.26H0v107.65h87.17c12.21 0 26-1.83 36.08-15.39 8.95-12.08 9.39-23.79 9.39-43.46V58.74c0-19.6-.44-31.28-9.38-43.34z" fill="#f96464"/>
<path id="path2" d="M109.31 25.48c7.84 9.08 7.76 19.83 7.76 32v25.89c0 12.13 0 22.91-7.83 32-6.79 7.85-15 8.16-25.69 8.16H15.8V33.26h-7v96.42h78.37c11.74 0 22.23-1.79 29.7-11.88 6.11-8.24 7-15 7-35.74V58.74c0-20.72-.87-27.39-7-35.63-6.87-9.22-17.76-11.89-29.7-11.89H0v6.19h83.55c10.67 0 18.97.23 25.76 8.07z" fill="#00aad8"/>
<path id="path3" d="M103.41 33.8c4.67 5.41 4.88 11.38 4.87 23.29v26.32c0 12.09-.2 18.14-4.94 23.62-4.09 4.73-9.42 5.25-19.79 5.25h-59v-79h-5.24V119h64.24c10.75 0 17.69-.46 23.33-7 6.68-7.72 6.68-16.94 6.67-28.62V57.09c0-11.52 0-20.62-6.6-28.28-5.7-6.58-12.61-6.91-23.4-6.91H0v6.73h83.55c10.69 0 15.8.49 19.86 5.17z" fill="'#b7c406"/>
</svg>
This path is drawn with fill. So, I can not use 'stroke-dashoffset'.
I'm wrangling some JavaScript code for a navbar menu on my website:
menuOptions : {
'Home' : '/',
'About' : '/about',
'Projects' : '/projects',
'Contact' : '/contact',
'<svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4zm2 6a1 1 0 011-1h6a1 1 0 110 2H7a1 1 0 01-1-1zm1 3a1 1 0 100 2h6a1 1 0 100-2H7z" clip-rule="evenodd"></path></svg>' : 'https://Link01.com',
'<svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M2 6a2 2 0 012-2h6a2 2 0 012 2v8a2 2 0 01-2 2H4a2 2 0 01-2-2V6zM14.553 7.106A1 1 0 0014 8v4a1 1 0 00.553.894l2 1A1 1 0 0018 13V7a1 1 0 00-1.447-.894l-2 1z"></path></svg>' : 'https://Link02.com',
'<svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path stroke-width="0" fill-rule="evenodd" d="M0 1.146C0 .513.526 0 1.175 0h13.65C15.474 0 16 .513 16 1.146v13.708c0 .633-.526 1.146-1.175 1.146H1.175C.526 16 0 15.487 0 14.854V1.146zm4.943 12.248V6.169H2.542v7.225h2.401zm-1.2-8.212c.837 0 1.358-.554 1.358-1.248-.015-.709-.52-1.248-1.342-1.248-.822 0-1.359.54-1.359 1.248 0 .694.521 1.248 1.327 1.248h.016zm4.908 8.212V9.359c0-.216.016-.432.08-.586.173-.431.568-.878 1.232-.878.869 0 1.216.662 1.216 1.634v3.865h2.401V9.25c0-2.22-1.184-3.252-2.764-3.252-1.274 0-1.845.7-2.165 1.193v.025h-.016a5.54 5.54 0 0 1 .016-.025V6.169h-2.4c.03.678 0 7.225 0 7.225h2.4z" clip-rule="evenodd"></path></svg>' : 'https://Link03.com',
},
I’ve tried the <a href> call below…
menuOptions : {
'Home' : '/',
'About' : '/about',
'Projects' : '/projects',
'Contact' : '/contact',
'<svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4zm2 6a1 1 0 011-1h6a1 1 0 110 2H7a1 1 0 01-1-1zm1 3a1 1 0 100 2h6a1 1 0 100-2H7z" clip-rule="evenodd"></path></svg>' : '#',
'<svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M2 6a2 2 0 012-2h6a2 2 0 012 2v8a2 2 0 01-2 2H4a2 2 0 01-2-2V6zM14.553 7.106A1 1 0 0014 8v4a1 1 0 00.553.894l2 1A1 1 0 0018 13V7a1 1 0 00-1.447-.894l-2 1z"></path></svg>' : '#',
'<svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path stroke-width="0" fill-rule="evenodd" d="M0 1.146C0 .513.526 0 1.175 0h13.65C15.474 0 16 .513 16 1.146v13.708c0 .633-.526 1.146-1.175 1.146H1.175C.526 16 0 15.487 0 14.854V1.146zm4.943 12.248V6.169H2.542v7.225h2.401zm-1.2-8.212c.837 0 1.358-.554 1.358-1.248-.015-.709-.52-1.248-1.342-1.248-.822 0-1.359.54-1.359 1.248 0 .694.521 1.248 1.327 1.248h.016zm4.908 8.212V9.359c0-.216.016-.432.08-.586.173-.431.568-.878 1.232-.878.869 0 1.216.662 1.216 1.634v3.865h2.401V9.25c0-2.22-1.184-3.252-2.764-3.252-1.274 0-1.845.7-2.165 1.193v.025h-.016a5.54 5.54 0 0 1 .016-.025V6.169h-2.4c.03.678 0 7.225 0 7.225h2.4z" clip-rule="evenodd"></path></svg>' : '#',
},
The above works for me... however, this method gets in the way of some backend hover stylings.
Please pardon my coding lingo which I'm sure is rough around the edges. Is there another way to perhaps call a backend function to open the SVG links in new browser tabs, without sacrificing my fade hover stylings?
Thanks in advance!
I have a SVG object.
<svg version="1.1" viewBox="0 0 200 172.29" xml:space="preserve" xmlns="http://www.w3.org/2000/svg">
<g transform="matrix(2.2495 0 0 2.2495 -12.814 -35.835)" stroke="#333" stroke-miterlimit="10">
<path d="m78.259 50.602c13.319 2.133 14.25-10.228 14.241-13.55" fill="none" stroke-linecap="round" stroke-width="4.208"/>
<path d="m18.922 56.574h-8.679a2.743 2.743 0 0 1-2.743-2.743v-7.138a2.743 2.743 0 0 1 2.743-2.743h8.679z" fill="#f47e60" stroke-width="3.607"/>
</g>
<g id="pig" transform="matrix(2.2495 0 0 2.2495 -12.814 -35.835)">
<g stroke="#333" stroke-miterlimit="10">
<path d="m28.203 82.098-4.803-1.462a3.1 3.1 0 0 1-2.063-3.868l4.713-15.483 10.734 3.268-4.713 15.483a3.1 3.1 0 0 1-3.868 2.062z" fill="#f47e60" stroke-width="4.097"/>
<path d="m66.242 82.072 5.22-1.589a3.19 3.19 0 0 0 2.123-3.982l-4.687-15.396-11.325 3.447 4.687 15.396a3.191 3.191 0 0 0 3.982 2.124z" fill="#f47e60" stroke-width="4.208"/>
<path d="m47.707 22.636c-4.499 0-8.788 0.727-12.712 2.035a11.28 11.28 0 0 0-0.945-1.545c-1.313-1.822-3.247-3.34-5.477-4.281-2.24-0.929-4.738-1.33-7.275-0.916 1.534 1.694 3.452 6.355 3.452 6.355s1.017 2.311 2 4.334c-7.785 5.062-12.784 12.872-12.784 21.644 0 15.257 15.106 27.626 33.74 27.626s33.74-12.369 33.74-27.626c1e-3 -15.258-15.105-27.626-33.739-27.626z" fill="#f8b26a" stroke-linejoin="round" stroke-width="3.673"/>
</g>
</g>
<g transform="matrix(2.2495 0 0 2.2495 -12.814 -35.835)">
<path d="m57.851 31.201a35.536 35.536 0 0 0-10.145-1.457c-2.78 0-5.468 0.315-8.019 0.902" fill="#f8b26a" stroke="#333" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="3.673"/>
<circle cx="27.608" cy="42.441" r="3.574" fill="#333"/>
</g>
<path id="pigFill" d="m77.485 10.578a2.6994 3.5305 0 0 0-2.2803 1.5975l-2.1748 4.3372a2.6994 3.5305 0 0 0 0.08348 4.0307h-4.4682l-0.03515-0.61297a2.7039 3.5362 0 0 0-2.9041-3.2552l-3.9542 0.39936a2.7039 3.5362 0 0 0-2.4956 3.4689h-29.179v116.56h129.39v-116.56h-12.342l0.65024-1.8947c0.49715-1.4534-3.1e-4 -3.1577-1.1116-3.808l-4.5341-2.6515a2.2045 2.8831 0 0 0-2.9129 1.4535l-2.3593 6.9005h-1.8673a2.6994 3.5305 0 0 0 0.0396-1.8621l-0.94903-5.0384a2.7017 3.5333 0 0 0-3.2688-2.5866l-14.912 4.7923-1.4323-3.0928a2.7039 3.5362 0 0 0-3.7126-1.1888l-3.3918 2.6841a2.7039 3.5362 0 0 0-0.99296 4.6622l-1.2522 0.404a2.7017 3.5333 0 0 0-1.4631 1.2259h-0.61511l-3.4928-5.8884a2.7039 3.5362 0 0 0-3.7916-0.6269l-3.1327 3.1762a2.7039 3.5362 0 0 0-1.0105 3.3389h-1.7355l-10.936-9.3851a2.6994 3.5305 0 0 0-1.4631-0.58047z" fill="#66503a" stroke-width="1.4711"/>
</svg>
with a masked part declared using CSS
#pigFill {
clip-path: ellipse(64px 54px at 64px -36px);
transform: translate(0px,100px);
}
I wrote a piece of JS to move the mask and it's content
$("#pigFill").css("clip-path", "ellipse(64px 54px at 64px "+clippathVal+"px)");
$("#pigFill").css("transform", "translate(0px,"+translateVal+"px)");
Here's the codepen POC https://codepen.io/Chagam/pen/wvozYPr
It's working ok, except with chrome based browsers on android such as Brave or Chrome. the animated part has wrong position and size. The transform: translate(0px,100px); seems to break everything. But it works as expected on firefox and safari.
Is there an other way to get the same result working on all browsers ?
Found a way to have it work on every browsers !!
Heres is the full fix : https://codepen.io/Chagam/pen/jOVyRYO
Basically changed 'translate' to 'translateY' as I only translate vertically.
#pigFill {
clip-path: ellipse(50% 43% at 50% -43%);
transform: translateY(70%);
}
$("#pigFill").css("clip-path", "ellipse(50% 43% at 50% "+clippathVal+"%)");
$("#pigFill").css("transform", "translateY("+translateVal+"%)");
I also changed translate and clip path values from px to %
The following SVG does not display, what is the problem? It should display with size 40x40px with a color.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20px" height="20px" viewBox="20 20 40 40" fill="#14cc9e">
<path d="M1096 301.5a5.5 5.5 0 1 0 10.96-.66c-.09-.7.16-1.42.66-1.93l4.58-4.58a2.7 2.7 0 0 1 2.12-.79 5.26 5.26 0 0 0 5.46-6.76l-3.17 3.17c-1.6.35-3.9-1.98-3.56-3.56l3.17-3.17a5.36 5.36 0 0 0-5.23 1.32 5.29 5.29 0 0 0-1.52 4.32c.05.67-.2 1.33-.67 1.81l-4.69 4.7c-.5.51-1.22.76-1.94.67a5.5 5.5 0 0 0-6.17 5.46zm3 .58l.7-2.63 2.63-.7 1.92 1.92-.7 2.63-2.63.7z"></path>
</svg>
You can use the command transform = 'translate (-1090 -275)
The fill command works in the svg file header, but it's more correct to move it to the patch
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="40px" height="40px" viewBox="0 0 40 40" style="border:1px solid red;">
<g transform="translate(-1090 -275)">
<path fill="#14cc9e" d="M1096 301.5 a5.5 5.5 0 1 0 10.96-.66c-.09-.7.16-1.42.66-1.93l4.58-4.58a2.7 2.7 0 0 1 2.12-.79 5.26 5.26 0 0 0 5.46-6.76l-3.17 3.17c-1.6.35-3.9-1.98-3.56-3.56l3.17-3.17a5.36 5.36 0 0 0-5.23 1.32 5.29 5.29 0 0 0-1.52 4.32c.05.67-.2 1.33-.67 1.81l-4.69 4.7c-.5.51-1.22.76-1.94.67a5.5 5.5 0 0 0-6.17 5.46zm3 .58l.7-2.63 2.63-.7 1.92 1.92-.7 2.63-2.63.7z">
</path>
</g>
</svg>
To find the image, it is convenient to use a frame that shows the boundaries of svg in the browser window.
style="border:1px solid red;"
After debugging, the frame can be removed.
The second option
You can also shift the image using parameters min-x, min-y viewBox="1090 275 40 40"
<svg width="40px" height="40px" viewBox="1090 275 40 40" >
<path fill="#14cc9e" d="M1096 301.5 a5.5 5.5 0 1 0 10.96-.66c-.09-.7.16-1.42.66-1.93l4.58-4.58a2.7 2.7 0 0 1 2.12-.79 5.26 5.26 0 0 0 5.46-6.76l-3.17 3.17c-1.6.35-3.9-1.98-3.56-3.56l3.17-3.17a5.36 5.36 0 0 0-5.23 1.32 5.29 5.29 0 0 0-1.52 4.32c.05.67-.2 1.33-.67 1.81l-4.69 4.7c-.5.51-1.22.76-1.94.67a5.5 5.5 0 0 0-6.17 5.46zm3 .58l.7-2.63 2.63-.7 1.92 1.92-.7 2.63-2.63.7z">
</path>
</svg>
You're setting the svg background color with fill. As far as I know this doesn't work. Try using property background-color instead.