1.17. SVG gradient-Linear

发布时间 : 2025-10-25 13:33:32 UTC      

Page Views: 9 views

1.17.1. SVG gradient

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

There are two main types of SVG gradients:

  • Linear

  • Radial

1.17.2. SVG linear gradient-

element is used to define a linear gradient.

labels must be nested in the on the inside. the label is definitions , that defines special elements such as gradients

A linear gradient can be defined as a horizontal, vertical, or angular gradient:

  • When y1 and y2 are equal, but x1 and x2 are different, a horizontal gradientcan be created.

  • When x1 and x2 are equal, but y1 and y2 are different, a vertical gradient can be created.

  • When x1 and x2 are different, and y1 and y2 are different, an angular gradient can be created.

1.17.3. Example 1

Define the oval shape of the horizontal linear gradient from yellow to red:

Here is the SVG code:

Example

<svgxmlns="http://www.w3.org/2000/svg"version="1.1"><defs><linearGradientid="grad1"x1="0%"y1="0%"x2="100%"y2="0%"><stopoffset="0%"style="stop-color:rgb(255,255,0);stop-opacity:1"/><stopoffset="100%"style="stop-color:rgb(255,0,0);stop-opacity:1"/>linearGradient>defs><ellipsecx="200"cy="70"rx="85"ry="55"fill="url(#grad1)"/>svg>    

Code parsing: