A gradient is a smooth transition from one color to another.
In addition, several color transitions can be applied to
the same element.
Gradients are defined within a <defs> element
and then referenced using fill or stroke
properties on a given graphics object.
Gradients can have multiple colors and stop points. You
can add multiple <stop> elements to create
a blend that spans several colors. Each <stop>
element will blend towards the color on either side of it.
There are two main types of gradients in SVG - linear gradients
and radial gradients.
The <linearGradient> element is used
to transition colors across a straight path.
The <radialGradient> element is used
to transition colors between an outer circle and an inner
circle.
Code Example
SVG Image
<?xml version="1.0"
standalone="no"?>
Right-click
on the image to open the SVG viewer pop-up menu.
Click 'view SVG' to display the image in a new, full
window to zoom & pan the image.
(SVG Viewer Tips)
<!DOCTYPE svg PUBLIC "-//W3C//DTD
SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
Linear
gradient - is assumed to start at
the left edge of the area to be filled and end at
the right edge. This vector can be changed using the
x1, y1, x2, and y2 attributes. Horizontal gradients
are created when y1 and y2 are equal and x1 and x2
differ. Vertical gradients are created when x1 and
x2 are equal and y1 and y2 differ. Angular gradients
are created when x1 and x2 differ and y1 and y2 differ.
Radial gradient
- is based on a circle, the center and radius of the
outer circle (at which the gradient vector ends) can
be adjusted using the cx, cy and r attributes. The
focal point (at which the gradient vector starts)
can be adjusted using the fx and fy attributes.
Element <stop>
- allows to indicate the colors that
they constitute the ends of the gradient.
Each <stop> element has two important
components: an offset attribute and a stop-color
style rule. The offset attribute determines
the point on the gradient line at which a color is
defined. This value is in relation to the start and
stop points defined previously.
When the gradient goes through the whole object (like
the first example), from 0 to 100%. The offset for
the yellow color is on 0% and for the green color
on 100%.
Often, the offset attribute is listed as
a percentage, although pixel values are also accepted.