Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define color in SVG

Tags:

How do I define a Color in a SVG file?

<?xml version="1.0"?>
<svg width="704" height="702" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <!-- define mycolor=red -->
    </defs> 

    <!-- use mycolor as fill -->
    <rect x="0" y="0" width="704" height="702" fill=mycolor/>
</svg>
like image 980
multiholle Avatar asked Dec 17 '10 12:12

multiholle


2 Answers

You can define a color via a gradient (<linearGradient> or <radialGradient>).

In both cases you use them the same way:

<rect x="0" y="0" width="704" height="702" fill="url(#yourcolor)"/>

The gradients should have only one <stop> element child, with the color you want.

A sample "gradient" color definition:

<linearGradient id="yourcolor">
  <stop stop-color="#991132"/>
</linearGradient>

The <solidColor> element is obsoleted nowadays and should be avoided.

like image 151
Erik Dahlström Avatar answered Sep 27 '22 02:09

Erik Dahlström


You can use a style sheet with svg and the fill property is also inhered from its parent element assuming you do not specify it. In this change to your code the color is red. If it were not given that color by the .myfill class it would be inherit blue from the "G" group tag.

<?xml version="1.0"?>
<svg width="704" height="702" xmlns="http://www.w3.org/2000/svg">

<style>
.myfill { fill:red }
</style>

    <g fill="blue">
    <rect x="0" y="0" width="704" height="702" class="myfill" />
    </g>

</svg>
like image 39
Wayne Avatar answered Sep 30 '22 02:09

Wayne