6 Styling and CSS


Contents


 

6.1 SVG's Use of Cascading Style Sheets

As much as possible, SVG conforms to and leverages the "Cascading Style Sheets (CSS) level 2" Recommendation [CSS2].

SVG uses CSS properties to describe many of its document parameters. In particular, SVG uses CSS properties for the following:

The following properties from CSS2 [CSS2] are used by SVG:

The following facilities from CSS2 are supported in SVG:

One variation SVG offers to CSS is that unit-less lengths and sizes are allowed in SVG properties. (Refer to the discussion of Units.)

6.2 Referencing External Style Sheets

External style sheets are referenced using the mechanism documented in "Associating Style Sheets with XML documents Version 1.0" [ESS].

6.3 The <style> element

<!ELEMENT style (#PCDATA)* >
<!ATTLIST style type CDATA #FIXED "text/css">

Attribute definitions:

type = content-type
This attribute specifies the style sheet language of the element's contents and overrides the default style sheet language. The style sheet language is specified as a content type (e.g., "text/css"). Authors must supply a value for this attribute; there is no default value for this attribute.

The <style> element allows authors to put style sheet rules embedded within an SVG document. <style> elements are only allowed as children of <defs> elements.

The syntax of style data depends on the style sheet language.

Some style sheet implementations may allow a wider variety of rules in the <style> element than in the style attribute that is available to container elements and graphics elements. For example, with CSS [CSS2], rules may be declared within a <style> element that cannot be declared within a style attribute.

The following is an example of defining and using a text style:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG July 1999//EN" 
  "http://www.w3.org/Graphics/SVG/svg-19990730.dtd">
<svg width="4in" height="3in">
  <defs>
    <style><![CDATA[
      .TitleText { font-size: 16; font-family: Helvetica } ]]>
    </style>
  </defs>
  <text class="TitleText">Here is my title</text>
</svg>

Download this example


6.4 The class attribute

(??? Not yet written)

6.5 The style attribute

(??? Not yet written)

6.6 Cascading and Inheritance of CSS Properties

SVG conforms fully to the cascading style rules of CSS (i.e., the rules by which the SVG user agent decides which property setting applies to a given element). See the CSS2 specification for a discussion of these rules.

The definition of each CSS property indicates whether the property can inherit the value of its parent.

(??? Add discusion of properties that don't inherit syntactically but by usage, such as opacity, filters and clip-path.)

6.7 The Scope/Range of CSS Styles

The following define the scope/range of CSS styles:

Stand-alone SVG graphic
There is one parse tree, and all elements are in the SVG namespace. CSS styles defined anywhere within the SVG graphic (in style elements or style attributes, or in external style sheets linked with the stylesheet PI) apply across the entire SVG graphic.
Stand-alone SVG graphic embedded in an HTML document with the <img> or <object> elements
There are two completely separate parse trees; one for the HTML document, and one for the SVG graphic. CSS styles defined anywhere within the HTML document (in style elements or style attributes, or in external style sheets linked with the stylesheet PI) apply across the entire HTML document. Since inheritance is down a parse tree, these styles do not affect the SVG graphic. CSS styles defined anywhere within the SVG document (in style elements or style attributes, or in external style sheets linked with the stylesheet PI) apply across the entire SVG document. These styles do not affect the containing HTML document. To get the same styling across both HTML document and SVG graphic, link them both to the same stylesheet.
Stand-alone SVG graphic textually included in an XML document
There is a single parse tree, using multiple namespaces; one or more subtrees are in the SVG namespace. CSS styles defined anywhere within the XML document (in style elements or style attributes, or in external style sheets linked with the stylesheet PI) apply across the entire document including those parts of it in the SVG namespace. To get different styling for the SVG part, use the style attribute or <style> element on the <svg> element. Alternatively, put an ID on the <svg> element and use contextual CSS selectors.

6.8 The 'display' property

To support SVG, the CSS2 [CSS2] 'display' property is enhanced as follows:

'display'
Value:  inline | block | list-item |
run-in | compact | marker |
table | inline-table | table-row-group | table-header-group |
table-footer-group | table-row | table-column-group | table-column |
table-cell | table-caption | none |
svg | svg-g | svg-path |
svg-rect | svg-circle | svg-ellipse | svg-line | svg-polyline | svg-polygon |
svg-text | svg-tspan | svg-textpath | svg-use | svg-image |
svg-switch | svg-foreignObject | svg-a |
inherit
Initial:  inline
Applies to:  all elements
Inherited:  no
Percentages:  N/A
Media:  all

The additional value types for the 'display' property are: svg, svg-g, svg-path, svg-rect, svg-circle, svg-ellipse, svg-line, svg-polyline, svg-polygon, svg-text, svg-tspan, svg-textpath, svg-use, svg-image, svg-switch, svg-foreignObject, svg-a.

A value of display: svg indicates that a principal container element should be created into which the SVG user agent will format and draw the given SVG document.

The remaining additional value types (e.g., display: svg-rect) indicate that the given element should be formatted and drawn by the SVG user agent according to the processing rules for the corresponding SVG element type (e.g., a <rect>).

The additional value types such as display: svg-rect are not allowed outside of a properly constructed SVG document. Thus, the following is an error because the element with display:svg-rect directly descends from an element with display:block, which does not correspond to a properly structured SVG document:

<abc width="10px" height="10px" style='display:block'>
  <xyz style='display:svg-rect' x="10" y="10" width="20" height="20" />
</abc>
On the other hand, the following is usage is correct because the element with display:svg-rect directly descends from an element with display:svg, which corresponds (i.e., one-for-one mapping) to a correctly structured SVG document as defined by the SVG DTD:
<abc width="10px" height="10px" style='display:svg'>
   <xyz style='display:svg-rect' x="10" y="10" width="20" height="20" />
</abc>
since the above SVG code fragment is equivalent to the following:
<?xml version="1.0" standalone="yes"?>
<svg width="10px" height="10px" 
     xmlns="http://www.w3.org/Graphics/SVG/svg-19990730.dtd">
  <rect x="10" y="10" width="20" height="20" />
</svg>

The user agent's default style sheet should include the following entries:

svg { display: svg }
g { display: svg-g } 
path { display: svg-path } 
rect { display: svg-rect } 
circle { display: svg-circle } 
ellipse { display: svg-ellipse } 
line { display: svg-line } 
polyline { display: svg-polyline } 
polygon { display: svg-polygon } 
text { display: svg-text } 
tspan { display: svg-tspan } 
textpath { display: svg-textpath } 
use { display: svg-use } 
image { display: svg-image } 
switch { display: svg-switch } 
foreignObject { display: svg-foreignObject } 
a { display: svg-a } 

Refer to the "Cascading Style Sheets (CSS) level 2" specification [CSS2] for more information.