Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG Path Fill Colour with CSS

Tags:

css

svg

fill

I've seen lots of people posting this, and they all seem pretty happy with their answers.

However I cant get my path fill to actually work,

http://jsfiddle.net/OwenMelbz/LvgmV/

The fiddle is above, the svg is generated from Illustrator

HTML

<img src="http://owenmelbourne.com/arrow.svg">

CSS

img {width: 100px}
path {
    fill: blue;
}

Yet I always get enter image description here

The SVG Code looks like

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, 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="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="9.334px" height="11.881px" viewBox="0 0 9.334 11.881" enable-background="new 0 0 9.334 11.881" xml:space="preserve">
<path fill="#999999" d="M4.159,5.942L0.202,1.884c0,0-0.609-1.032,0.288-1.641s1.471,0.118,1.471,0.118L6.29,4.877
    c0,0,1.15,0.947,0.254,1.894c-0.896,0.947-3.94,4.143-3.94,4.143L2.08,11.438c0,0-0.861,0.996-1.759,0
    c-0.93-1.031,0.863-2.418,0.863-2.418L4.159,5.942z"/>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>

If anybody could shed some light on the matter it would be much appreciated.

Thanks

like image 445
owenmelbz Avatar asked Jan 01 '26 04:01

owenmelbz


1 Answers

CSS fills, and other SVG-specific rules, won't apply to an SVG rendered with an <img> tag:

// CSS fills won't apply to this
<img src="image.svg">

You need the SVG to be rendered inline in order for CSS fills to work.

// CSS fills will apply to this
<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="9.334px" height="11.881px" viewBox="0 0 9.334 11.881" enable- 
background="new 0 0 9.334 11.881" xml:space="preserve">
<path fill="" d="M4.159,5.942L0.202,1.884c0,0-0.609-1.032,0.288-1.641s1.471,0.118,1.471,0.118L6.29,4.877
c0,0,1.15,0.947,0.254,1.894c-0.896,0.947-3.94,4.143-3.94,4.143L2.08,11.438c0,0-0.861,0.996-1.759,0
c-0.93-1.031,0.863-2.418,0.863-2.418L4.159,5.942z"/>
</svg>

CSS:

#Layer_1 {
    fill: blue;
}

Also, you could also use Javascript to automatically convert SVG's into inline elements. However if you're dealing with a few static SVG's for an interface (e.g. your theme), it's best to just copy-paste them inline.

like image 131
MarsAndBack Avatar answered Jan 04 '26 18:01

MarsAndBack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!