Access path properties of svg via css - html

I have saved my svg image with illustrator and the content looks like this:
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="10.3 126 503.7 272" style="enable-background:new 10.3 126 503.7 272;" xml:space="preserve">
<style type="text/css">
.st0{fill:#B7B7B7;}
.st1{fill:#33BFC1;}
.st2{fill:#B23939;}
</style>
<path fill="#B7B7B7" class="st0" d="M20.4,126h113c4.3,0,8"/>
<path class="st1" d="M504,126H390c-4.3,0-8,2.7-9.4,6"/>
<path class="st2" d="M420.9,384.6l-92.2-252"/>
</svg>
In my html I have :
<a><img class="logo" src="logo.svg"></a>
Since there are 3 path within svg, I want to be able to style each using css. I know I can edit directly svg file, but the idea is to change it via css.
I have tried something like
.st0 {
fill:#fff
}
.st0 path {
fill:#fff
}
but none of it works

This should work, only if it placed after the previous .st0 declaration.
.st0 {
fill:#fff
}
This is the proper way to declare the second example.
path.st0 {
fill:#fff
}
But both of these have to be AFTER the svg code block.
What I think is happening is that you have the SVG in your body, and you are declaring the css in the head. So your fill:#fff is getting overwritten by the CSS inside the SVG element.

Related

How to reference different SVGs inside the same SVG file in CSS background?

I am creating a website and a very important requirement of this website is to have a minimal number of files. One place where I can reduce the number of files are the SVG files of the project. Currently this project is using 10+ SVG files and I hope to combine them all into one SVG sprite sheet. Since these SVG files were being used as background images in CSS:
.class-name {
background-image: url(/path/to/file.svg)
}
I now have an svg file, where each one of the previous SVG files is a symbol, so the svg file looks like this:
<svg>
<symbol id="id1">....</svg>
<symbol id="id2">....</svg>
<symbol id="id3">....</svg>
...
</svg>
I wish to use these symbols in my CSS as follows:
.class-name {
background-image: url(/path/to/combined-file.svg#id1)
}
How can I use the SVG symbols in my background image.
I have already tried the following:
.class-name {
background-image: url(/path/to/combined-file.svg#id1)
}
and also
.class-name {
background-image: url("data:image/svg+xml;base64,<svg><use xlink:href="path/to/combined-svg#id1/></svg>"");
}
I have also tried converting all the <symbol> tags to <g> tags but then all the images start to overlap each other and therefore it becomes pointless.
I am hoping for a solution where all my SVG are in one file and i can reference them individually in my CSS background image.
This is an example where I'm using svg sprites as background:
div{width:300px;height:300px; border:1px solid;
background-image:url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/cat.svg#redcat);
background-size:200px;
background-repeat: no-repeat;
background-position: center;
}
<div></div>
You can also use the #blackcat
The combined file looks like this:
(Please observe the style element where this rule svg > svg:not(:target) {display: none;} is hiding all the nested svg elements unless they are the target)
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100px" height="100px" viewBox="0 0 225 225">
<style type="text/css">
<![CDATA[
/* this is hiding all the nested svg elements unless they are the target*/
svg > svg:not(:target) {
display: none;
}
]]>
</style>
<desc>
<g id="cat">
<path id="body" fill-rule="evenodd" clip-rule="evenodd" d="M121.506,108.953.....108.953z"/>
<path id="head" fill-rule="evenodd" clip-rule="evenodd" d="M129.747,18.651......81.453z"/>
</g>
</desc>
<svg version="1.1" id="blackcat" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 225 225">
<use xlink:href ="#cat" fill="black" />
</svg>
<svg version="1.1" id="redcat" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 225 225">
<use xlink:href ="#cat" fill="red" />
</svg>
</svg>
I hope this is what you need.
You will find this explained in detail in this book: Using SVG with CSS3 and HTML5: Vector Graphics for Web Design

Can't get clip-path to work with external svg

I'm trying to get an image clipped by a external svg file.
I can get it to work applying eg. a polygon directly to the css, but when I try to link it to an external svg, it's not working.
This is my image that needs to be clipped:
<img src="{{ asset('/images/hero.jpg') }}" class="hero-image">
This is my svg
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Laag_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1366 706" style="enable-background:new 0 0 1366 706;" xml:space="preserve">
<defs>
<clipPath id="bg-clipping">
<path d="M-0.6-15.6l76.8,612.4c0,0,6.6,104.8,108.4,107.2C243.8,705.3,1363,571.9,1363,571.9L1364.3,3L-0.6-15.6z"/>
</clipPath>
</defs>
</svg>
and here is my css
.hero-image {
#apply absolute w-full h-full;
clip-path: url("#bg-clipping");
}
Please save me, heroes of Stackoverflow. I've been struggling for a loooooong time now.
If the SVG is on a different URL, you're probably out of luck. Firefox is currently the only browser that supports this (CanIUse chart footnote 2).
If you're OK with it only working in Firefox, you can define the clip-path like so.
.hero-image {
clip-path: url("path/to/svg.svg#bg-clipping");
}
Interesting caveat: while it does work with relative URL's (as demonstrated here), it doesn't work with absolute URL's to a different domain (demo here). I can't find an explanation for this in the spec, it appears to be a bug.
When the SVG is on the same page as the image, it should work fine. Browser support is better.
body {
margin: 0;
}
.hero-image {
min-height: 706px;
clip-path: url("#bg-clipping");
}
<img src="https://picsum.photos/id/1015/1366/706" class="hero-image">
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 1366 706">
<defs>
<clipPath id="bg-clipping">
<path d="M-0.6-15.6l76.8,612.4c0,0,6.6,104.8,108.4,107.2C243.8,705.3,1363,571.9,1363,571.9L1364.3,3L-0.6-15.6z"/>
</clipPath>
</defs>
</svg>

How to select the color fill for a svg image using css? (Not inline svg)

How can you select the color "fill" for a svg image using css? I have the following img tag:
<img src="assets/images/folder.svg" class="svg" >
My .svg file is locking like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" viewBox="0 0 25 19" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
<g transform="matrix(1,0,0,1,-87.34,-350.89)">
<g transform="matrix(1,0,0,0.947605,-375.66,-124.574)">
<g id="Mail-Icon" serif:id="Mail Icon" transform="matrix(1,0,0,1.05529,-47.0001,-2366.53)">
<path d="M532.5,2737L511.5,2737C510.672,2737 510,2736.33 510,2735.5L510,2719.5C510,2718.67 510.672,2718 511.5,2718L518.065,2718L518.065,2720L532.5,2720C533.329,2720 534,2720.67 534,2721.5L534,2735.5C534,2736.33 533.329,2737 532.5,2737ZM531,2724L531,2722.43L513,2722.44L513,2724L531,2724Z" style="fill:rgb(131,147,167);"/>
</g>
</g>
</g>
</svg>
I have tried this, but it does not work:
.svg {
fill:rgb(18, 136, 222);
}
I have seen solutions where you add the svg code inside your HTML. Though I am looking for a solution where adding the svg inline is not required.
The problem with both <img> and background-image, Is that you don't get to control the innards of the SVG with CSS like you can with using SVG as inline or Using SVG as an <object>.
svg path {
fill: red;
}
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600"><title>Untitled-1</title><g id="Mail-Icon"><path d="M552,467.87H48a36,36,0,0,1-36-36v-384a36,36,0,0,1,36-36H205.56v48H552a36,36,0,0,1,36,36v336A36,36,0,0,1,552,467.87Zm-36-312V118.19l-432,.24v37.44Z"/></g></svg>
You can read more about it here
Similar question answered here: img src SVG changing the fill color
As you cannot modify an external ressource with a CSS file, and that using the <img> tag imports an external ressource, you cannot achieve what you want without putting the .svg content into you .html page.
Another option is to modify directly the svg code by adding the fill="rgb(18, 136, 222);" as an attribute of the <path> tag :
<path fill="red" d="M532.5 …

Inline CSS styles being applied to SVG with specific file name

I have a simple svg
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="100px" height="100px" viewBox="0 0 100 100" style="enable-background:new 0 0 100 100;" xml:space="preserve">
<style type="text/css">
<![CDATA[
.st0{fill:#A0A0A0;}
]]>
</style>
<path class="st0" d="M92.6,0H7.4C3.3,0,0,3.2,0,7.2v85.6c0,4,3.3,7.2,7.4,7.2h85.2c4.1,0,7.4-3.2,7.4-7.2V7.2C100,3.2,96.7,0,92.6,0
z M29.7,85.2H14.8V37.5h14.8V85.2z M22.2,31c-4.8,0-8.6-3.9-8.6-8.6c0-4.7,3.8-8.6,8.6-8.6c4.7,0,8.6,3.9,8.6,8.6
C30.8,27.1,27,31,22.2,31z M85.2,85.2H70.4V62c0-5.5-0.1-12.7-7.7-12.7c-7.7,0-8.9,6-8.9,12.3v23.6H39V37.5h14.2V44h0.2
c2-3.8,6.8-7.7,14-7.7c15,0,17.8,9.9,17.8,22.7V85.2z"/>
</svg>
However, when I place it in my html file like this:
<img src="images/icon_linkedin.svg" alt="LinkedIn">
It doesn't appear. Inspecting the HTML reveals that image could not be loaded and the following inline style has been added to the HTML img element.
style="display: none !important;"
Changing the file name (of the actual file and in the HTML) fixes this but I'm wondering what's going on here? It only seems to occur in Firefox (57.0.1).
are you using adblocking software? It should work jsfiddle.net/honsa/5tao1sog

Applying CSS from a file to a SVG file through HTML

Good Evening,
I have multiple SVG files and need to dynamically change their fill color. Preferably using CSS.
HTML:
<div>
<svg width="100%" height="100%" viewbox="0 0 64 64" preserveAspectRatio="xMinYMin meet">
<image xlink:href="http://imgh.us/export_1.svg" width="64" height="64" />
</svg>
</div>
CSS:
div {
width: 64px;
height: 64px;
}
svg, svg * {
fill: #000000 !important;
}
SVG:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 15.0.2, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="72.121px" height="59.209px" viewBox="0 0 72.121 59.209" enable-background="new 0 0 72.121 59.209" xml:space="preserve">
<g>
<path fill="#ff0000" d="M71.53,36.664L57.56,22.705c-0.57-0.57-1.43-0.74-2.18-0.43c-0.74,0.31-1.23,1.04-1.23,1.84v5.59h-4.32
v-12.6c0-0.53-0.22-1.05-0.609-1.43L33.6,0.564c-0.22-0.21-0.47-0.36-0.75-0.45c-0.1-0.04-0.2-0.06-0.31-0.08
c-0.06-0.01-0.12-0.02-0.18-0.02h-0.08c-0.06-0.01-0.13-0.01-0.19-0.01H2c-1.1,0-2,0.89-2,2v55.2c0,1.11,0.9,2,2,2h45.83
c1.101,0,2-0.89,2-2v-10.75h4.32v5.591c0,0.81,0.49,1.54,1.23,1.85c0.25,0.1,0.51,0.15,0.77,0.15c0.521,0,1.03-0.2,1.41-0.58
l13.97-13.971c0.38-0.38,0.59-0.88,0.59-1.409C72.12,37.545,71.91,37.045,71.53,36.664z M45.83,55.204H4v-51.2h26.21v13.1
c0,1.11,0.89,2,2,2h13.62v10.6H34.91c-1.1,0-2,0.9-2,2v12.75c0,1.11,0.9,2,2,2H45.83V55.204z"/>
<path fill="#ff0000" d="M32.28,0.015c-0.06-0.01-0.13-0.01-0.19-0.01h0.12C32.23,0.004,32.26,0.015,32.28,0.015z"/>
</g>
<g>
<path fill="#ff0000" d="M32.21,0.004h-0.12c0.06,0,0.13,0,0.19,0.01C32.26,0.015,32.23,0.004,32.21,0.004z"/>
</g>
</svg>
Here's a fiddle.
My problem is simple. Instead of the fill color in the CSS, the SVG-files fill color is used. Which makes sense, since it's a different document.
For various reasons I can not edit the SVG files.
Is there a solution and what would it look like?
If you want to use the SVG and Image tag alone you'll need to use javascript to manipulate/recreate the image to use the filters/colors/css:
How to change color of SVG image using CSS (jQuery SVG image replacement)?
If you want to use pure CSS you need to embed the SVG directly in the page.