CSS Namespaces Module Level 3

Editor’s Draft,

More details about this document
This version:
https://drafts.csswg.org/css-namespaces/
Latest published version:
https://www.w3.org/TR/css3-namespace/
Previous Versions:
Test Suite:
http://test.csswg.org/suites/css3-namespace/20090210/
Feedback:
CSSWG Issues Repository
Editor:
Elika J. Etemad / fantasai (Apple)
Former Editors:
Anne van Kesteren (Opera Software ASA)
Peter Linss (Netscape Communications)
Chris Lilley (W3C)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

This CSS Namespaces module defines the syntax for using namespaces in CSS. It defines the @namespace rule for declaring the default namespace and binding namespaces to namespace prefixes, and it also defines a syntax that other specifications can adopt for using those prefixes in namespace-qualified names.

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-namespaces” in the title, like this: “[css-namespaces] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list [email protected].

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

1. Introduction

This section is non-normative.

This CSS Namespaces module defines syntax for using namespaces in CSS. It defines the @namespace rule for declaring a default namespace and for binding namespaces to namespace prefixes. It also defines a syntax for using those prefixes to represent namespace-qualified names. It does not define where such names are valid or what they mean: that depends on their context and is defined by a host language, such as Selectors ([SELECT]), that references the syntax defined in the CSS Namespaces module.

Note that a CSS client that does not support this module will (if it properly conforms to CSS’s forward-compatible parsing rules) ignore all @namespace rules, as well as all style rules that make use of namespace qualified names. The syntax of delimiting namespace prefixes in CSS was deliberately chosen so that these CSS clients would ignore the style rules rather than possibly match them incorrectly.

1.1. Terminology

Besides terms introduced by this specification, CSS Namespaces uses the terminology defined in Namespaces in XML 1.0. [XML-NAMES] However, the syntax defined here is not restricted to representing XML element and attribute names and may represent other kinds of namespaces as defined by the host language.

In CSS Namespaces a namespace name consisting of the empty string is taken to represent the null namespace or lack of a namespace.

For example, given the namespace declarations:
@namespace empty "";
@namespace "";

The type selectors elem, |elem, and empty|elem are equivalent.

2. Declaring namespaces: the @namespace rule

The @namespace at-rule declares a namespace prefix and associates it with a given namespace name (a string). This namespace prefix can then be used in namespace-qualified names such as the CSS qualified names defined below.

@namespace "http://www.w3.org/1999/xhtml";
@namespace svg "http://www.w3.org/2000/svg";

The first rule declares a default namespace http://www.w3.org/1999/xhtml to be applied to names that have no explicit namespace component.

The second rule declares a namespace prefix svg that is used to apply the namespace http://www.w3.org/2000/svg where the svg namespace prefix is used.

In CSS Namespaces, as in Namespaces in XML 1.0, the prefix is merely a syntactic construct; it is the expanded name (the tuple of local name and namespace name) that is significant. Thus the actual prefixes used in a CSS style sheet, and whether they are defaulted or not, are independent of the namespace prefixes used in the markup and whether these are defaulted or not.

For example, given the following XML document:
<qml:elem xmlns:qml="http://example.com/q-markup"></qml:elem>

and the following @namespace declarations at the beginning of a CSS file:

@namespace Q "http://example.com/q-markup";
@namespace lq "http://example.com/q-markup";

The selectors Q|elem and lq|elem in that CSS file would both match the element <qml:elem>.

(The selector qml|elem would be invalid, because CSS namespaces only recognize prefixes declared in CSS, not those declared by the document language.)

2.1. Syntax

The syntax for the @namespace rule is:

@namespace <namespace-prefix>? [ <string> | <url> ] ;

<namespace-prefix> = <ident>

Any @namespace rules must follow all @charset and @import rules and precede all other non-ignored at-rules and style rules in a style sheet. For CSS syntax this adds [ namespace [SCDOCDC]* ]* immediately after [ import [SCDOCDC]* ]* in the stylesheet grammar.

A syntactically invalid @namespace rule (whether malformed or misplaced) must be ignored. A CSS style sheet containing an invalid @namespace rule is not a valid style sheet.

A URI string parsed from the URI syntax must be treated as a literal string: as with the STRING syntax, no URI-specific normalization is applied.

All strings—​including the empty string and strings representing invalid URIs—​are valid namespace names in @namespace declarations.

2.2. Scope

The namespace prefix is declared only within the style sheet in which its