Skip to content

[selectors] Match local links #2010

Closed
Closed
@LeaVerou

Description

@LeaVerou

In navigation menus, tables of contents etc there's often a need to highlight the links that point to the current page or a part thereof.
While this is needed in almost every website, it currently cannot be done without JS, server-side coding, or horrible hacks.
Selectors4 used to define a :local-link pseudo-class to solve this issue, but was removed in the latest ED due to lack of interest. IMO that was a good decision, as it was overcomplicated and didn't even cover all common use cases. For example, it ignored fragment identifiers, so it was useless for intra-page navigation.

What about a :local-link pseudo-class that works like the following:

  1. If the link target does not match the current document URL ignoring any fragment identifiers, the pseudo-class does not match.
  2. If the link target contains a fragment identifier, the fragment identifier must match that of the current document URL, otherwise the pseudo-class does not match. If the link target does not contain a fragment identifier, step 1 is sufficient.

This definition works for both intra-page navigation as well as navigation across pages, and doesn't have all the path segment complexity of :local-link(). Furthermore, authors don't have to think what kind of matching they need — it just works.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions