CSS Linked Parameters

Editor’s Draft,

More details about this document
This version:
https://drafts.csswg.org/css-link-params/
Issue Tracking:
CSSWG Issues Repository
Inline In Spec
Editors:
Tab Atkins-Bittner (Google)
Daniel Holbert (Mozilla)
Jonathan Watt (Mozilla)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

This spec introduces a way to pass CSS values into linked resources, such as SVG images, so that they can be used as CSS custom property values in the destination resource. 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.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-link-params” in the title, like this: “[css-link-params] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.

This document is governed by the 03 November 2023 W3C Process Document.

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 applying 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.

CSS link parameters are a way to set CSS custom properties on an "external" resource, either by a CSS property or thru 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 a Link Parameter

An external resource can be accompanied by a map of link parameters, each entry composed of a custom property name as a key, and an <any-value> as the value.

There are three ways to specify a link parameter:

If specified in multiple of these ways, all of the link parameters are combined. If the same key appears in multiple inputs, the latest source in the above list wins (that is, URL fragment beats link-parameters, and url("..." param()) beats URL fragment).

The influence of link parameters on the linked resource is defined in the next section.

Name: link-parameters
Value: none | <link-param>+
Initial: none
Applies to: all elements and pseudo-elements
Inherited: no
Percentages: n/a
Computed value: as specified
Canonical order: per grammar
Animation type: discrete

The link-parameters property is one way to set link parameters on the element itself (if it is an element representing an external resource, such as an HTML img or iframe), and on all external CSS resources specified on the element (such as background images, etc). Its values are:

none

No link parameters specified.

<link-param>+

A list of one or more link parameters. If two link parameters with the same name are specified with the same <custom-property-name>, the last one wins.

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

A <link-param> represents a link parameter, with a key of the <custom-property-name>. If the <declaration-value> is specified, that’s the value of the link parameter. If omitted, the value of the custom property of the same name on the element is the value of the link parameter.

2.2. In The URL

A special "fragment identifier" can be used in the fragment of a URL used to reference an external resource. 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; TODO define an actual parser for it).

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.

2.3. Setting via the CSS url() Function

When referencing an external resource 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 accommodate this, a <link-param> is a valid <url-modifier>.

As in the other methods of specifying link parameters, if the same <custom-property-name> is specified in several <link-param>s, 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 Link Parameters

When an external resource link has one or more link parameters specified, if the linked resource understands CSS (such as an SVG or HTML document), then the initial value of custom properties with names equal to the keys of the link parameters map is set to the corresponding values of the map.

If an @property rule is specified for one of the custom property names in the link parameters, the link parameter value is used for the initial value, rather than the @property-specified initial value.

If the linked resource does not understand CSS (such as PNG images), then link parameters have no effect.

Define a way for the linked resource to specify what link parameters they allow. For cross-origin iframes/etc, this will default to nothing; for same-origin (or cross-origin "SVG as image"), it defaults to "everything". If not allowed, the link parameter is ignored.

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>
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

Document conventions

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.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Tests

Tests relating to the content of this specification may be documented in “Tests” blocks like this one. Any such block is non-normative.


Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Partial implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Non-experimental implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-PROPERTIES-VALUES-API-1]
Tab Atkins Jr.; et al. CSS Properties and Values API Level 1. URL: https://drafts.css-houdini.org/css-properties-values-api-1/
[CSS-SYNTAX-3]
Tab Atkins Jr.; Simon Sapin. CSS Syntax Module Level 3. URL: https://drafts.csswg.org/css-syntax/
[CSS-VALUES-4]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. URL: https://drafts.csswg.org/css-values-4/
[CSS-VARIABLES-2]
CSS Custom Properties for Cascading Variables Module Level 2. Editor's Draft. URL: https://drafts.csswg.org/css-variables-2/
[FILL-STROKE-3]
Elika Etemad; Tab Atkins Jr.. CSS Fill and Stroke Module Level 3. URL: https://drafts.fxtf.org/fill-stroke/
[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://datatracker.ietf.org/doc/html/rfc2119
[SELECTORS-4]
Elika Etemad; Tab Atkins Jr.. Selectors Level 4. URL: https://drafts.csswg.org/selectors/
[URL]
Anne van Kesteren. URL Standard. Living Standard. URL: https://url.spec.whatwg.org/

Informative References

[CSS-COLOR-4]
Chris Lilley; Tab Atkins Jr.; Lea Verou. CSS Color Module Level 4. URL: https://drafts.csswg.org/css-color-4/
[CSS-VARIABLES]
Tab Atkins Jr.. CSS Custom Properties for Cascading Variables Module Level 1. URL: https://drafts.csswg.org/css-variables/

Property Index

Name Value Initial Applies to Inh. %ages Anim­ation type Canonical order Com­puted value
link-parameters none | <link-param>+ none all elements and pseudo-elements no n/a discrete per grammar as specified

Issues Index

Define a way for the linked resource to specify what link parameters they allow. For cross-origin iframes/etc, this will default to nothing; for same-origin (or cross-origin "SVG as image"), it defaults to "everything". If not allowed, the link parameter is ignored.