As mentioned in Chapter 4, some attributes of SVG elements control the element's geometry. An example of one such attribute would be the cx (center x) attribute of a <circle>. Other attributes, such as fill, control the element's presentation. Stylesheets provide a way for you to separate the presentation from the geometric structure; this lets you control the visual display of many different SVG elements (and even documents) by changing one stylesheet referenced by all the documents.

Contents

Anatomy of a Style

A style is a specification of a visual property for an element and the value that you would like that property to have. The property name and the value are separated by a colon. For example, to say that you want the stroke color for some element to be blue, the appropriate style specifier would be stroke: blue.

To specify multiple properties in a style, you separate the specifiers with semicolons. The following style specifier sets the stroke color to red, the stroke width to three pixels, and the fill color to a light blue. The last property-value pair is followed by a semicolon. This is not necessary, but is done to give the style a more consistent look.

stroke: red; stroke-width: 3px; fill: #ccccff;

Style Selectors

Once you have determined the visual properties you'd like, you must select the element or elements to which they apply. The simplest way to apply a style specification to a single element is to make that specification the value of a style attribute. So, if you want the preceding specification to apply to a particular <circle> in your document, you write:

Internal Stylesheets

If you want the style specification to apply to all <circle> elements in a single document, add an internal stylesheet. A style sheet consists of selectors (the names of the elements you want to affect) and the style specifications for those selectors. The style specification is enclosed in curly braces. The following applies styles to <circle> and <rect> elements:

When you put a <style> element into an SVG document, you should enclose its contents within <![[CDATA[ and ]]>. This notation tells XML parsers that the contents are pure character data and should not, under any circumstances, be treated as information for XML to parse.

Because this stylesheet is within a document, it applies to that document alone. If you have many documents, all of whose circles and rectangles appear as specified in the preceding example, take the specifiers, without any of the tags, and put them into a separate myStyle.css file. In each SVG document, insert the following processing instruction:

<?xml-stylesheet href="myStyle.css" type="text/css"?>

Then, at a later point, if you decide that all rectangles should be filled with a light green and outlined in dark green, you can simply change the specification in myStyle.css to read:

rect {fill: #ccffcc; stroke: #006600; }

and all your documents, once redisplayed, will have green rectangles instead of gray rectangles.

Style Selector Classes

The preceding stylesheet affects all <rect> and <circle> elements. Let's say, though, that you want only some circles in your documents to be styled. Write your stylesheet with a class specifier as follows, where the dot after circle indicates that the following identifier is a class name:

circle.special {
stroke: red; stroke-width: 3px;
fill: #ccccff;
}

If, in your SVG document, you had the following elements, the first circle would show up as the default (black fill, no stroke), and the second would take on the style attributes as its class name matches the class identifier in the stylesheet.

It is possible to specify a generic class that can apply to any element. Presume that several different graphic objects serve as warning symbols. You would like them to have a yellow fill and a red border. You could write this selector, which consists only of a class name and its style specifier:

.warning { fill: yellow; stroke: red; }

This generic class may now be applied to any SVG element. In the following example, both the rectangle and triangle will have yellow interiors and red outlines.

The class attribute may contain the names of several classes; their combined properties will be applied to the element in question. We will add a generic class named seeThrough for translucency to the previous example, and then apply both classes to the polygon.

Using CSS with SVG

The question then becomes: which attributes in SVG elements can also be specified in a stylesheet? Table B-1 is a list of the properties you may use in a stylesheet, the valid values (with default value shown in boldface where appropriate), and the elements to which they may be applied. It is a modified version of the property index from the SVG specification at http://www.w3.org/TR/SVG/.[1]