XML technologies / SVG / Filters, transformations and gradients / Gradient and pattern settings / Applying and using gradients
(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)
The previous figure (see Radial gradients) illustrates gradients’ design possibilities. A small example shows how simple changes make a graphic more interesting, using the graphic from Creating simple SVG graphics as its basis:
Figure: Starting graphic for the following explanations.
Adding a linear gradient to horizontal navigation
A linear gradient suits the gray horizontal navigation bar, giving a metallic 3D effect when configured correctly. Only a few code changes are needed. Insert a <defs> tag and define a dark-gray-to-white gradient with the attribute spreadMethod set to reflect. This ensures a smooth gradient filling the bar even after resizing. Name it “verlaufgrau” and use it instead of the previous fill color in the <rect> tag.
<?xml version="1.0" standalone="no"?>
...
<svg width="800" height="600">
<defs>
<linearGradient id="verlaufgrau" x1="50%" y1="30%" x2="50%" y2="60%" spreadMethod="reflect">
<stop offset="0%" style="stop-color:darkgray;stop-opacity:1"/>
<stop offset="50%" style="stop-color:lightgray;stop-opacity:1"/>
<stop offset="100%" style="stop-color:white;stop-opacity:1"/>
</linearGradient>
</defs>
<rect id="hnavigationsleiste" x="0" y="80" width="720" height="40" rx="10" ry="10" style="fill:url(#verlaufgrau);stroke:rgb(0,0,0);stroke-width:1"/>
</svg>
The code produces this result:
Figure: Gradient result for the horizontal navigation bar.
Note:
Also apply this gradient to the text “SVG” by replacing its fill color with url(#verlaufgrau).
Formatting the circle in the left corner
The left circle still looks plain, but a gradient can improve it too. Lighting and shadows can be added later.
For a similar 3D effect, define a radial gradient starting with white and light gray and darkening only near the outside. Add a light stripe before the edge to obtain the following gradient.
<radialGradient id="verlaufgraukreis" spreadMethod="reflect">
<stop offset="0%" style="stop-color:white;stop-opacity:1"/>
<stop offset="70%" style="stop-color:rgb(200,200,200);stop-opacity:1"/>
<stop offset="92%" style="stop-color:rgb(200,200,200);stop-opacity:1"/>
<stop offset="95%" style="stop-color:white;stop-opacity:1"/>
<stop offset="97%" style="stop-color:rgb(180,180,180);stop-opacity:1"/>
<stop offset="99%" style="stop-color:rgb(122,122,122);stop-opacity:1"/>
<stop offset="100%" style="stop-color:darkgray;stop-opacity:1"/>
</radialGradient>
Figure: Gradient created for the circle.
Copyright © verlag moderne industrie GmbH 2001
You may print the online version for your private use.
Otherwise, this chapter from “Das Einsteigerseminar SVG — Webgrafiken mit XML” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
verlag moderne industrie GmbH, Justus-von-Liebig-Straße 1, D-86899 Landsberg/Lech, fon ++49 (0) 81 91 / 125 – 0, leserservice(at)mi-verlag.de