Gradiente lineal de SVG
- Página anterior Sombreado de SVG
- Página siguiente Gradiente radial de SVG
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 gradients in SVG:
- Linear gradient
- Radial gradient
SVG linear gradient - <linearGradient>
<linearGradient>
The element is used to define linear gradients.
<linearGradient>
The element must be nested in <defs>
Inside the tag.<defs>
The element is an abbreviation for definitions, which includes definitions of special elements (such as filters).
Linear gradients can be defined as horizontal, vertical, or angular gradients:
- Create a horizontal gradient when y1 and y2 are equal and x1 and x2 are not equal
- Create a vertical gradient when x1 and x2 are equal and y1 and y2 are not equal
- Create an angular gradient when x1 and x2 are different and y1 and y2 are not equal
Example 1
Define an ellipse with a horizontal linear gradient from yellow to red:
Este es el código SVG:
<svg height="150" width="400"> <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>
Explicación del código:
- La propiedad id de la etiqueta <linearGradient> define el nombre único del degradado
- Las propiedades x1, x2, y1, y2 de la etiqueta <linearGradient> definen el inicio y el final de la transición
- El rango de colores del degradado puede consistir en dos o más colores. Cada color se especifica con la etiqueta <stop>
- La propiedad offset se utiliza para definir el inicio y el final de la transición del degradado
- La propiedad fill enlaza el elemento elíptico con el degradado
Ejemplo 2
Define un elíptico con una transición lineal vertical desde amarillo a rojo:
Este es el código SVG:
<svg height="150" width="400"> <defs> <linearGradient id="grad2" x1="0%" y1="0%" x2="0%" y2="100%"> <stop offset="0%" style="stop-color:rgb(255,0,0);stop-opacity:1" /> <stop offset="100%" style="stop-color:rgb(255,255,0);stop-opacity:1" /> </linearGradient> </defs> <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad2)" /> </svg>
Ejemplo 3
Define un elíptico y agrega texto dentro de él, que tiene una transición horizontal lineal desde amarillo a rojo:
Este es el código SVG:
<svg height="150" width="400"> <defs> <linearGradient id="grad3" 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(#grad3)" /> <text fill="#ffffff" font-size="45" font-family="Verdana" x="150" y="86"> SVG</text> </svg>
Explicación del código:
- El elemento <text> se utiliza para agregar texto
- Página anterior Sombreado de SVG
- Página siguiente Gradiente radial de SVG