Skip to content

Notes caveats that make :target difficult to use with dynamic content #11928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

stephband
Copy link
Contributor

@stephband stephband commented Jan 11, 2022

Summary

These caveats are not well documented anywhere else on the web, and only become evident reading the spec – or in my case, after hours of head scratching, testing and refactoring.

Motivation

I would like to allow others to avoid the head scratching, testing and refactoring.

Supporting details

Test case for dynamic :target styles and explanation of the problem is here: https://stephen.band/target/

Related issues

Changes to :targets behaviour are being discussed in the App History API (WICG/navigation-api#162) but the consensus is it can't be changed.

Metadata

  • Adds a new document
  • Rewrites (or significantly expands) a document
  • Fixes a typo, bug, or other error

…namic content

These caveats are not well documented anywhere else on the web, and only become evident reading the spec – or in my case, after hours of head scratching, testing and refactoring. I would like to allow others to avoid a similar pain.
@stephband stephband requested a review from a team as a code owner January 11, 2022 18:44
@stephband stephband requested review from rachelandrew and removed request for a team January 11, 2022 18:44
@github-actions github-actions bot added the Content:CSS Cascading Style Sheets docs label Jan 11, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jan 11, 2022

Preview URLs

Flaws

None! 🎉

External URLs

URL: /en-US/docs/Web/CSS/:target
Title: :target
on GitHub

No new external URLs

(this comment was updated 2022-01-11 18:58:32.208351)

@estelle
Copy link
Member

estelle commented Jan 13, 2022

I am not seeing this caveat.
Example: https://codepen.io/estelle/pen/LYzMjRK.
The caveat makes it sound like the above shouldn't work, when it is just a change in the URL that doesn't work. The change in the URL, if it was working (as in changing the URL) would scroll... oddly, not scrolling but then not changing state either...

The example, when I click on the history.pushState(#one) doesn't give focus to #one, the target element, either. If I put https://stephen.band/target/#four in the browser, sometimes it works for both (goes to and hilites four) and sometimes it doesn't (neither scrolls to four nor matches the selector). I am thinking the caveat should be in the pushState API rather than the selector, with a caveat linking to pushState, but need to do a bit more research.

@stephband
Copy link
Contributor Author

stephband commented Jan 13, 2022 via email

@stephband
Copy link
Contributor Author

stephband commented Jan 13, 2022 via email

@estelle
Copy link
Member

estelle commented Jan 13, 2022

Thanks. Digging deeper.

@@ -12,7 +12,7 @@ browser-compat: css.selectors.target
---
{{CSSRef}}

The **`:target`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) represents a unique element (the _target element_) with an {{htmlattrxref("id")}} matching the URL's fragment.
The **`:target`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) represents a unique element (the _target element_) with an {{htmlattrxref("id")}} matching the URL's fragment at the time the page loaded.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not just at page load. when the page scrolls AND the fragment is changed, the selector matches, even if the id was added well after page load (my test was 10s) ... the URL fragment wasn't there at page load.


There are two caveats that make the `:target` selector unsuitable for use with dynamic content.

1. The target element is determined at the time of `DOMContentLoad`, upon navigations triggered by clicking on links, and when setting `window.location` or `window.location.hash`. It does NOT update when changing the location hash via `window.history.pushState()` or `window.history.replaceState()`. Pages relying on `history` to navigate cannot rely on `:target` to highlight content.
Copy link
Member

@estelle estelle Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this accurate, or is it only when history.pushState() doesn't trigger a scroll to the fragment?

@estelle estelle closed this Jun 14, 2022
@stephband
Copy link
Contributor Author

I know I have been lax in replying here, but is there a good reason for closing this? The description of :target at https://developer.mozilla.org/en-US/docs/web/css/_colon_target remains misleading. The very first sentence on that page,

"The :target pseudo-class represents a unique element (the target element) with an id matching the URL's fragment."

just isn't true. It's only true under certain circumstances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:CSS Cascading Style Sheets docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants