Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SVG Gradient – Linear

SVG Gradient - Linear


SVG Gradient

A gradient is a smooth transition from one color to another. In addition, transitions of multiple colors can be applied to the same element.

There are mainly two types of SVG gradients:


SVG linear gradient -

In SVG, the The element is used to create a linear gradient; it transitions from one color to another along a straight line, creating a smooth gradient effect.

Linear gradients can be used to fill or stroke SVG graphic elements, giving them rich color variations.

Basic Syntax



  

  

  



Parameter Description:

Linear gradients can be defined as horizontal, vertical, or angular gradients:

The following code defines a horizontal linear gradient from red to blue, then applies it to a filled rectangle, giving the rectangle a gradient fill effect.

Example

 

 

   

   

 

 

 

 

Example 1

Define a horizontal linear gradient from yellow to red ellipse:

Below is the SVG code:

Example

<

svg

xmlns

=

"

http://www.w3.org/2000/svg

"

version

=

"

1.1

"

<

defs

<

linearGradient

id

=

"

grad1

"

x1

=

"

0%

"

y1

=

"

0%

"

x2

=

"

100%

"

y2

=

"

0%

"

<

stop

offset

=

"

0%

"

style

=

"

stop-color:rgb(255,255,0);stop-opacity:1

"

/>

<

stop

offset

=

"

100%

"

style

=

"

stop-color:rgb(255,0,0);stop-opacity:1

"

/>

linearGradient

defs

<

ellipse

cx

=

"

200

"

cy

=

"

70

"

rx

=

"

85

"

ry

=

"

55

"

fill

=

"

url(#grad1)

"

/>

svg

Try it »

Click to view:View SVG File.

Code analysis:

  • The id attribute of the tag can define a unique name for the gradient
  • The X1, X2, Y1, Y2 attributes of the tag define the gradient start and end positions
  • The color range of a gradient can consist of two or more colors. Each color is specified by atag. The offset attribute is used to define the start and end positions of the gradient.
  • The fill attribute links the ellipse element to this gradient

Example 2

Define a vertical linear gradient from yellow to red ellipse:

Below is the SVG code:

Example

<

svg

xmlns

=

"

http://www.w3.org/2000/svg

"

version

=

"

1.1

"

<

defs

<

linearGradient

id

=

"

grad1

"

x1

=

"

0%

"

y1

=

"

0%

"

x2

=

"

0%

"

y2

=

"

100%

"

<

stop

offset

=

"

0%

"

style

=

"

stop-color:rgb(255,255,0);stop-opacity:1

"

/>

<

stop

offset

=

"

100%

"

style

=

"

stop-color:rgb(255,0,0);stop-opacity:1

"

/>

linearGradient

defs

<

ellipse

cx

=

"

200

"

cy

=

"

70

"

rx

=

"

85

"

ry

=

"

55

"

fill

=

"

url(#grad1)

"

/>

svg

Try it »

Click to view:View SVG File.

Example 3

Define an ellipse, with a horizontal linear gradient from yellow to red and add text inside the ellipse:

Below is the SVG code:

Example

<

svg

xmlns

=

"

http://www.w3.org/2000/svg

"

version

=

"

1.1

"

<

defs

<

linearGradient

id

=

"

grad1

"

x1

=

"

0%

"

y1

=

"

0%

"

x2

=

"

100%

"

y2

=

"

0%

"

<

stop

offset

=

"

0%

"

style

=

"

stop-color:rgb(255,255,0);stop-opacity:1

"

/>

<

stop

offset

=

"

100%

"

style

=

"

stop-color:rgb(255,0,0);stop-opacity:1

"

/>

linearGradient

defs

<

ellipse

cx

=

"

200

"

cy

=

"

70

"

rx

=

"

85

"

ry

=

"

55

"

fill

=

"

url(#grad1)

"

/>

<

text

fill

=

"

#ffffff

"

font-size

=

"

45

"

font-family

=

"

Verdana

"

x

=

"

150

"

y

=

"

86

"

SVG

text

svg

Try it »

Click to view:View SVG File.

Code analysis:

  • The element is used to add a text

Notes

  • A gradient can contain multiple elements, each element represents a color and position in the gradient.

  • The start and end points of a gradient can be located anywhere; the gradient interpolates along the line between the defined start and end points.

  • Gradient coordinate values can use percentages or specific length values; percentage values are relative to the size of the graphic element.

  • Linear gradients can be applied to fill or stroke SVG graphic elements.