HTML - tutorial - 27 - SVG - gradient elements

revision:


Content

<linearGradient> <radialGradient> <stop>


<linearGradient>

top

lets authors define linear gradients to apply to other SVG elements.

the <linearGradient> element must be nested within a <defs> tag, which is short for "definitions" and contains definition of special elements (such as gradients).

Linear gradients can be defined as horizontal, vertical or angular gradients:1/ horizontal gradients are created when y1 and y2 are equal and x1 and x2 differ; 2/ vertical gradients are created when x1 and x2 are equal and y1 and y2 differ; 3/ angular gradients are created when x1 and x2 differ and y1 and y2 differ

attributes: gradientUnits, gradientTransform, href, spreadMethod, x1, x2, xlink:href, y1, y2

example

codes:

                    <svg viewBox="0 0 10 6">
                        <defs>
                            <linearGradient id="myGradient" gradientTransform="rotate(90)"> 
                                <stop offset="5%"  stop-color="blue" />
                                <stop offset="95%" stop-color="red" />
                            </linearGradient>
                        </defs>
                        <!-- using my linear gradient -->
                        <circle cx="5" cy="3" r="3" fill="url('#myGradient')" />
                    </svg>
                    
                

<radialGradient>

top

lets authors define radial gradients that can be applied to fill or stroke of graphical elements.

the <radialGradient> element must be nested within a <defs> tag, which is short for "definitions" and contains definition of special elements (such as gradients).

attributes: cx, cy, fr, fx, fy, gradientUnits, gradientTransform, href, r, spreadMethod, xlink:href

example

codes:

                    <svg viewBox="0 0 10 7">
                        <defs>
                            <radialGradient id="myGradient_a">
                                <stop offset="10%" stop-color="gold" />
                                <stop offset="95%" stop-color="red" />
                            </radialGradient>
                        </defs>
                        <!-- using my radial gradient -->
                        <circle cx="5" cy="3" r="3" fill="url('#myGradient_a')" />
                    </svg>
                

<stop>

top

defines a color and its position to use on a gradient. This element is always a child of a <linearGradient> or <radialGradient> element.

attributes: offset, stop-color, stop-opacity

example

codes:

                    <svg viewBox="0 0 10 7">
                        <defs>
                            <radialGradient id="myGradient_b">
                                <stop offset="10%" stop-color="green" />
                                <stop offset="95%" stop-color="blue" />
                            </radialGradient>
                        </defs>
                        <!-- using my radial gradient -->
                        <circle cx="5" cy="3" r="3" fill="url('#myGradient_b')" />
                    </svg>