CSS Route Matching

Editor’s Draft,

More details about this document
This version:
https://drafts.csswg.org/css-navigation-1/
Issue Tracking:
CSSWG Issues Repository
Inline In Spec
w3c/csswg-drafts#12594
Editors:
L. David Baron (Google)
Noam Rosenthal (Google)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

This module contains conditional CSS rules for styling conditioned on the current URL or conditioned on the status of navigating between particular URLs.

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-navigation” in the title, like this: “[css-navigation] …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 18 August 2025 W3C Process Document.

1. Conditional rules for navigation queries

1.1. Navigation queries: the @navigation rule

The @navigation rule is a conditional group rule whose condition tests characteristics of the current URL or of the state of navigation between two URLs. These queries are called navigation queries.

Authors can use it to:

The syntax of the condition in the @navigation rule is similar to that defined for <supports-condition> in [CSS-CONDITIONAL-3]. Negation, conjunction, and disjunction are all needed so that authors can specify the interaction of multiple styles in ways that are most intuitive and require the simplest code.

The syntax of the @navigation rule is:

@navigation <navigation-condition> {
  <rule-list>
}

with <navigation-condition> defined as:

<navigation-condition> = not <navigation-in-parens>
                     | <navigation-in-parens> [ and <navigation-in-parens> ]*
                     | <navigation-in-parens> [ or <navigation-in-parens> ]*
<navigation-in-parens> = ( <navigation-condition> ) | ( <navigation-test> ) | <general-enclosed>
<navigation-test> = <navigation-location> | <navigation-keyword> : <navigation-location>
<navigation-keyword> = at | from | to
<navigation-location> = <urlpattern()>

The above grammar is purposely very loose for forwards-compatibility reasons, since the <general-enclosed> production allows for substantial future extensibility. Any @navigation rule that does not parse according to the grammar above (that is, a rule that does not match this loose grammar which includes the <general-enclosed> production) is invalid. Style sheets must not use such a rule and processors must ignore such a rule (including all of its contents).

Many of these grammar terms are associated with a boolean result, as follows:

<navigation-condition>
not <navigation-in-parens>

The result is the negation of the <navigation-in-parens> term.

<navigation-in-parens> [ and <navigation-in-parens> ]*

The result is true if all of the <navigation-in-parens> child terms are true, and false otherwise.

<navigation-in-parens> [ or <navigation-in-parens> ]*

The result is false if all of the <navigation-in-parens> child terms are false, and true otherwise.

<navigation-in-parens>

The result is the result of the child subexpression.

<navigation-test>
<navigation-location>
at: <navigation-location>

The result is true if the document’s URL matches the navigation location URL pattern of <navigation-location>.

from: <navigation-location>

The result is true if the document’s navigation API of the document is non-null, and either:

to: <navigation-location>

The result is true if the document’s navigation API of the document is non-null, and either:

TODO: Update this definition to handle redirects better. In particular, we’d like the to: URL to change at only a single time, from the original URL of the navigation to the final one. There is already a well-defined definition and processing model that we can depend on.

The above definitions of from and to apparently don’t work right if you start a same-document navigation (e.g., with pushState) in the middle of a cross-document navigation.

Improve integration with has been revealed rather than monkeypatching it.

<general-enclosed>

The result is false.

Authors must not use <general-enclosed> in their stylesheets. It exists only for future-compatibility, so that new syntax additions do not invalidate too much of a <navigation-condition> in older user agents.

The navigation location URL pattern of a <navigation-location> depends on the type of <navigation-location>:

<urlpattern()>

The URL pattern represented by the function; see create a URL pattern for urlpattern().

There is currently only one type of <navigation-location> but we should consider adding another, which would be just a name that references a name defined in a routemap. See the route matching explainer for details.

A document’s navigation API is the result of the following steps on document:

  1. Let window be the Window whose associated Document is document, or null if there is no such Window.

  2. If window is null, return null.

  3. Return window’s navigation API.

The condition of the @navigation rule is the result of the <navigation-condition> in its prelude.

This specification defines an additional function for the @when rule:

navigation() = navigation( <navigation-condition> )

The navigation() function is associated with the boolean result that its contained condition is associated with.

This specification defines an additional function for the if() function’s <if-test> production:

navigation() = navigation( <navigation-condition> )

This should probably have a more formal definition of the function, but I can’t find the formal definitions of the existing if() functions to model it after.

TODO: To be written.

3. The urlpattern() function

The urlpattern() function represents a URL pattern, which can be used to match URLs.

<urlpattern()> = urlpattern( <string> )

This function represents a URL pattern that can be created using the steps of the create a URL pattern for urlpattern() algorithm:

  1. Let arg be the <string> argument to the urlpattern() function.

  2. Let baseURL be the style resource base URL of the rule or declaration block containing the urlpattern() function.

    Do we want this to be the base URL all the time? For use of urlpattern() in @navigation, it’s likely more useful for the base URL to be the document URL rather than the style sheet URL. However, it would be very awkward for urlpattern() to be inconsistent with url().

    Should we allow the base URL of urlpattern() to be defined by the consumer? Should we introduce document-urlpattern()? Should we do something similar to CSS Images 3 § 2.1.1 Ambiguous Reference-or-Image URLs (see w3c/csswg-drafts issue #383)?

    Also see other proposed uses of URLPattern in CSS in w3c/csswg-drafts issue #10975, for :local-link.

  3. Return the result of create a URL pattern given arg, baseURL, and an empty map.

NOTE: This function requires that its argument is quoted. This differs from the url() function, which allows its argument to be quoted or unquoted.

To serialize a urlpattern() function f, serialize a function f, using serialize a string on the single argument to serialize f’s contents.

NOTE: This is defined this way because URLPattern intentionally does not provide a serialization.

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-CONDITIONAL-3]
Chris Lilley; David Baron; Elika Etemad. CSS Conditional Rules Module Level 3. URL: https://drafts.csswg.org/css-conditional-3/
[CSS-CONDITIONAL-5]
Chris Lilley; et al. CSS Conditional Rules Module Level 5. URL: https://drafts.csswg.org/css-conditional-5/
[CSS-IMAGES-3]
Tab Atkins Jr.; Elika Etemad; Lea Verou. CSS Images Module Level 3. URL: https://drafts.csswg.org/css-images-3/
[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-VALUES-5]
Tab Atkins Jr.; Elika Etemad; Miriam Suzanne. CSS Values and Units Module Level 5. URL: https://drafts.csswg.org/css-values-5/
[CSSOM-1]
Daniel Glazman; Emilio Cobos Álvarez. CSS Object Model (CSSOM). URL: https://drafts.csswg.org/cssom/
[DOM]
Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/
[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[INFRA]
Anne van Kesteren; Domenic Denicola. Infra Standard. Living Standard. URL: https://infra.spec.whatwg.org/
[MEDIAQUERIES-5]
Dean Jackson; et al. Media Queries Level 5. URL: https://drafts.csswg.org/mediaqueries-5/
[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-5]
Selectors Level 5. Editor's Draft. URL: https://drafts.csswg.org/selectors-5/
[URLPATTERN]
Ben Kelly; Jeremy Roman; 宍戸俊哉 (Shunya Shishido). URL Pattern Standard. Living Standard. URL: https://urlpattern.spec.whatwg.org/

Issues Index

This assumes that the ongoing navigate event and the transition have the same lifetime, but this isn’t really true if the event is intercepted. After whatwg/html#11690 / whatwg/html#11692. we could probably define this more like "from" above. But which lifetime is the one we want?
The above definitions of from and to apparently don’t work right if you start a same-document navigation (e.g., with pushState) in the middle of a cross-document navigation.
Improve integration with has been revealed rather than monkeypatching it.
There is currently only one type of <navigation-location> but we should consider adding another, which would be just a name that references a name defined in a routemap. See the route matching explainer for details.
This should probably have a more formal definition of the function, but I can’t find the formal definitions of the existing if() functions to model it after.
Do we want this to be the base URL all the time? For use of urlpattern() in @navigation, it’s likely more useful for the base URL to be the document URL rather than the style sheet URL. However, it would be very awkward for urlpattern() to be inconsistent with url().

Should we allow the base URL of urlpattern() to be defined by the consumer? Should we introduce document-urlpattern()? Should we do something similar to CSS Images 3 § 2.1.1 Ambiguous Reference-or-Image URLs (see w3c/csswg-drafts issue #383)?

Also see other proposed uses of URLPattern in CSS in w3c/csswg-drafts issue #10975, for :local-link.