Selectors Level 5

Editor’s Draft,

More details about this document
This version:
https://drafts.csswg.org/selectors-5/
Latest published version:
https://www.w3.org/TR/selectors-5/
Feedback:
CSSWG Issues Repository
Inline In Spec
Editors:
Elika J. Etemad / fantasai (Apple)
Tab Atkins Jr. (Google)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

Selectors are patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in a document. Selectors have been optimized for use with HTML and XML, and are designed to be usable in performance-critical code. They are a core component of CSS (Cascading Style Sheets), which uses Selectors to bind style properties to elements in the document. Selectors Level 5 describes the selectors that already exist in [selectors-4], and further introduces new selectors for CSS and other languages that may need them.

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 “selectors” in the title, like this: “[selectors] …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

This is a diff spec against Selectors Level 4.

1.1. Module Interactions

This module extends the set of selectors defined for CSS in [selectors-4].

2. Location Pseudo-classes

2.1. The local link pseudo-class :local-link

The :local-link pseudo-class allows authors to style hyperlinks based on the users current location within a site and to differentiate site-internal versus site-external links.

The (non-functional) :local-link pseudo-class represents an element that is the source anchor of a hyperlink whose target’s absolute URL matches the element’s own document URL. Any fragment identifiers are stripped before matching the document’s URL against the link’s URL; otherwise all portions of the URL are considered.

For example, the following rule prevents links targeting the current page from being underlined when they are part of the navigation list:
nav :local-link { text-decoration: none; } 

As a functional pseudo-class, :local-link() can also accept a non-negative integer as its sole argument, which, if the document’s URL belongs to a hierarchical scheme, indicates the number of path levels to match:

The following example styles all site-external links with a dashed underline.
:not(:local-link(0)) { text-decoration-style: dashed; } 

Path segments are portions of the URL’s path that are separated by forward slashes (/). If a segment is missing from the document’s URL, a pseudo-class requiring that segment to match does not match anything.

So, given the links:
  1. <a href="http://www.example.com">Home</a>
  2. <a href="http://www.example.com/2011">2011</a>
  3. <a href="http://www.example.com/2011/03">March</a>
  4. <a href="http://www.example.com/2011/03/">March</a>
  5. <a href="http://www.example.com/2011/03/21">21 March</a>
  6. <a href="https://www.example.com/2011/03/">March</a>
  7. <a href="http://example.com/2011/03/">March</a>

and the styles:

  1. a:local-link {...}
  2. a:local-link(0) {...}
  3. a:local-link(1) {...}
  4. a:local-link(2) {...}
  5. a:local-link(3) {...}
  6. a:local-link(4) {...}

If the document’s URL is http://www.example.com/2011/03/:

  1. Link 1 would receive Style B
  2. Link 2 would receive Styles B and C
  3. Link 3 would receive Styles B, C, and D
  4. Link 4 would also receive Styles A, B, C, D, and E
  5. Link 5 would receive Styles B, C, and D
  6. Link 6 would remain unstyled
  7. Link 7 would remain unstyled
  8. Style F would not be applied to anything

The "origin" of the URL is defined by RFC 6454, Section 4. The username, password, query string, and fragment portions of the URL are not considered when matching against :local-link(n). If the document’s URL does not belong to a hierarchical scheme, the functional pseudo-class matches nothing.

It’s clear that, if the document URL has at least N segments, then :local-link(N) only matches links whose URL has at least N segments. (This lets you assign consistent semantics to :local-link so that, for example, :local-link(2) means a "within-repo" link on GitHub.) What about if the document url has less than N segments, and the link is same-page? Should "null segments" count as matching, or not?

3. Exposing custom state: the :state() pseudo-class

The :state() pseudo-class takes a case-sensitive argument and matches custom elements whose exposed custom states include the argument.

The grammar of the :state() pseudo-class is:

:state(  )

The exact matching behavior of :state() pseudo-class is defined by the host language. See HTML’s definition.

4. Combinators

4.1. Reference combinators /ref/

The reference combinator consists of two slashes with an intervening CSS qualified name, and separates two compound selectors, e.g. A /attr/ B. The element represented by the first compound selector explicitly references the element represented by the second compound selector. Unless the host language defines a different syntax for expressing this relationship, this relationship is considered to exist if the value of the specified attribute on the first element is an IDREF or an ID selector referencing the second element.

Attribute matching for reference combinators follow the same rules as for attribute selectors.

The following example highlights an input element when its <label> is focused or hovered-over:
label:is(:hover, :focus) /for/ input,       /* association by "for" attribute */
label:is(:hover, :focus):not([for]) input { /* association by containment */
  box-shadow: yellow 0 0 10px;
}

5. Changes

5.1. Changes Since Level 4

Additions since Level 4:

6. Acknowledgements

The CSS working group would like to thank everyone who contributed to the previous Selectors specifications over the years, as those specifications formed the basis for this one. In particular, the working group would like to extend special thanks to the following for their specific contributions to Selectors Level 5: Joey Arhar.

Privacy Considerations

Should be copied from Level 4 when appropriate.

Security Considerations

Should be copied from Level 4 when appropriate.

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

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

Informative References

[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/

Issues Index

This is a diff spec against Selectors Level 4.
It’s clear that, if the document URL has at least N segments, then :local-link(N) only matches links whose URL has at least N segments. (This lets you assign consistent semantics to :local-link so that, for example, :local-link(2) means a "within-repo" link on GitHub.) What about if the document url has less than N segments, and the link is same-page? Should "null segments" count as matching, or not?