SVG Parameters

A Collection of Interesting Ideas,

This version:
https://tabatkins.github.io/specs/svg-parameters/
Issue Tracking:
GitHub
Editor:
Tab Atkins-Bittner (Google)

Abstract

This introduces "parameters" to SVG, which are a method of setting CSS custom properties in an external SVG document via the fragment on the referencing URL. This allows easy reuse of "templated" SVG images, which can be adapted to a site’s theme color, etc. easily, without having to modify the source SVG.

Table of Contents

1. Introduction

SVG is stylable with CSS, and when used inline in HTML, this capability can be very useful. For example, an SVG icon can take on a different color based on whether the user is hovering it or not, just by appling a :hover rule to it that changes the fill property.

When the SVG is referenced in a way that doesn’t allow selectors or CSS inheritance from the outer page to apply to it (such as embedding it via img or iframe in HTML), though, this functionality is lost. The only way to change the display of such "external" SVG images is to produce several of them, and change which image you’re referencing. This incurs delay on the page as a new resource is downloaded, and disallows dynamic effects like CSS Transitions.

SVG Parameters are a way to set CSS custom properties on an "external" SVG image, by passing them through a special fragment scheme on the URL. This gives a limited, but powerful, subset of the customizability that "inline" SVG images have to "external" SVG images.

2. Setting an SVG Parameter

To "set" an SVG Parameter, a special "fragment identifier" must be used in the fragment of the URL used to reference. Several examples of existing "fragment identifiers" for SVG documents can be found in the SVG 1.1 specification.

The syntax of an SVG parameter fragment identifier is:

param( <custom-property-name> <declaration-value> )

(using the CSS value definition syntax).

For example, to set the "--text-color" custom property of an SVG image to blue, one can reference the image with a url like “http://example.com/image.svg#param(--text-color%20blue)”.

If passing multiple parameters to an image, additional param() functions must be appended to the URL. If multiple param() functions specify the same <custom-property-name>, the custom property is set to the value of the last one.

For example, if the image from the previous example also used a "--bg-color" custom property, it could be referenced with a url like “http://example.com/image.svg#param(--text-color%20blue)param(--bg-color%20white)”.

Note: Spaces, and some other characters that might be valid in CSS syntax, are not technically valid in URLs. In some contexts, you might need to escape those characters to form a valid URL. In most cases, though, such as HTML’s a element or CSS’s url() function, spaces are accepted and do not need to be escaped.

When a valid param() function is encountered in the fragment referencing an SVG document, it sets the initial value of the given custom property to the given <declaration-value>. (By default, the initial value of a custom property is an invalid value.)

2.1. Setting via the CSS url() Function

When referencing an SVG image via CSS, the param() function can be used in the url() function. But a common use-case is passing in values of the page’s own custom properties; for example, a page might use a --primary-color custom property, and want to make an SVG image match. There’s no way, however, to integrate the value of a custom property in CSS into the URL passed to the url() function.

To accomodate this, this specification defines a new <url-modifier>, the param() function:

param() = param( <custom-property-name> <declaration-value> )

Note: You may recognize this as identical to the syntax of the param() fragment identifer.

Similarly to the param() fragment identifier, the param() <url-modifier> sets the initial value of the referenced custom property to the passed <declaration-value>. Also similarly to the fragment identifier, if multiple param() functions reference the same custom property, the last one wins.

For example, if the site is using a -primary-color custom property to theme its elements with, and wanted an SVG background using a --color custom property to reflect it, it could write:
.foo {
  background-image: url("http://example.com/image.svg" param(--color var(--primary-color)));
}

3. Using SVG Parameters

Within an SVG image, any SVG parameters set the initial value of the corresponding custom properties. They can then be used directly in any CSS property via the CSS var() function.

For example, if an SVG image wanted to expose a --color parameter, it could use it like:
<svg>
  <g style="fill: var(--color);">
    <path d="..." />
  </g>
</svg>

Can it be used in presentation attributes?

It’s usually a good idea to make your SVG image usable even if no parameters are given, by providing "default values" for each of the custom properties. There are several ways to do this.
  1. On each var() function, provide a fallback value, like fill: var(--color, blue).

  2. If the custom property is going to be used a lot, such that providing a fallback for each individual var() is troublesome, store the custom property in a different name while invoking the default, like:

    :root {
      --color2: var(--color, blue);
    }

    In this example, if --color is provided via an SVG parameter, --color2 will receive its value. If not, it will recieve the default blue value. In either case, --color2 can be used in the SVG image’s stylesheet unconditionally, secure in the knowledge that it will always have a value.

  3. In a future level of the Custom Properties specification [CSS-VARIABLES], some "parent’s value" functionality will be available to make the previous suggestion more usable:

    :root {
      --color: var(parent --color, blue);
    }

    (This is an example syntax, and is not yet final.)

    By invoking the value of the --color property on the parent (which, on the root element, refers to the initial value), an author can avoid self-reference loops while retaining the same custom property name.

Conformance

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[HTML]
Ian Hickson. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[SVG2]
Nikos Andronikos; et al. Scalable Vector Graphics (SVG) 2. 9 July 2015. WD. URL: http://www.w3.org/TR/SVG2/
[CSS-CASCADE-4]
Elika Etemad; Tab Atkins Jr.. CSS Cascading and Inheritance Level 4. 21 April 2015. WD. URL: http://www.w3.org/TR/css-cascade-4/
[CSS-COLOR-4]
CSS Color Module Level 4 URL: https://drafts.csswg.org/css-color-4/
[CSS-SYNTAX-3]
Tab Atkins Jr.; Simon Sapin. CSS Syntax Module Level 3. 20 February 2014. CR. URL: http://www.w3.org/TR/css-syntax-3/
[CSS-VALUES]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 11 June 2015. CR. URL: http://www.w3.org/TR/css-values/
[CSS-VARIABLES-1]
Tab Atkins Jr.. CSS Custom Properties for Cascading Variables Module Level 1. 6 May 2014. LCWD. URL: http://www.w3.org/TR/css-variables-1/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119
[SELECTORS-4]
Selectors Level 4 URL: http://www.w3.org/TR/selectors4/
[URL]
Anne van Kesteren; Sam Ruby. URL. 9 December 2014. WD. URL: http://www.w3.org/TR/url-1/

Informative References

[CSS-VARIABLES]
Tab Atkins Jr.. CSS Custom Properties for Cascading Variables Module Level 1. 6 May 2014. LCWD. URL: http://www.w3.org/TR/css-variables-1/

Issues Index

Can it be used in presentation attributes?