docs.rodeo

MDN Web Docs mirror

Text fragments

Text fragments link directly to specific text in a web page, without requiring the page author to add an ID. They use a special syntax in the URL fragment. This feature lets you create deep links to content that you don’t control and may not have IDs associated. It also makes sharing links more useful by directly pointing others to specific words. Browsers may differ in how they draw attention to the linked text—usually, the text is scrolled into view and highlighted with color.

Concepts and usage

Historically, one of the web’s key features has always been its ability to provide links between different documents — it is what makes the web, a web:

The issue with linking to specific document fragments is that the author of the linked page needs to put an anchor in place to actually link to. The second example above links to an {{htmlelement("Heading_Elements", "h2")}}  element with an ID of browser_compatibility:

<h2 id="browser_compatibility">
  <a href="#browser_compatibility">Browser compatibility</a>
</h2>

Not all documents have such anchors, and even if they do, linking to a heading might be much less obvious than linking directly to the specific text you are citing. This is where text fragments help: they allow the link author to have full control over what text to link to, without requiring any special markup in the target document. For example, a search engine may refer to a specific sentence in its search results, and clicking the link will take you directly to that sentence.

However, text fragments also have a limitation: text in a document is less stable than document structure. If the text in the linked document is updated, the fragment no longer matches, and the browser navigates to the top of the page. This is fine for transient links such as those in search results, but if you intend for the link to work over time, document fragments may be more reliable.

Syntax

https://example.com#:~:text=[prefix-,]textStart[,textEnd][,-suffix]

Text fragments are a kind of URL fragment, and is written after the #. The key parts to understand are as follows:

Supporting browsers will scroll to and highlight the first text fragment in the linked document that matches the specified directive. Note that it is possible to specify multiple text fragments to highlight in the same URL by separating them with ampersand (&) characters.

Usage notes

[!NOTE] If the provided text fragment does not match any text in the linked document, or if the browser does not support text fragments, the whole text fragment is ignored and the top of the document is linked.

Examples

Text fragment with textStart

textStart and textEnd

Examples with prefix- and/or -suffix

URLs with multiple text fragments

You can specify multiple text fragments to highlight in the same URL by separating them with ampersand (&) characters. Let’s look at a couple of examples:

If you don’t see one or more of your text fragments highlighted and you are sure you’ve got the syntax correct, you might just be highlighting a different instance than the one you expected. It might be highlighted, but offscreen.

Styling matched text fragments

Browsers are free to style the highlighted text in whatever default way they choose. The CSS Pseudo-Elements Module Level 4 defines a pseudo-element, {{cssxref("::target-text")}} , which allows you to specifying custom styling.

For example, in our scroll-to-text demo we have the following CSS:

::target-text {
  background-color: rebeccapurple;
  color: white;
}

Try following the above link in a supporting browser to see the effect this has.

Feature Detectability

The {{domxref("FragmentDirective")}}  object, which is accessed via the {{domxref("Document.fragmentDirective")}}  property, can be used to test whether or not text fragments are supported in a browser.

Try running the following in a supporting browser’s devtools, in a tab with one or more matched text fragments:

document.fragmentDirective;
// returns an empty FragmentDirective object, if supported
// undefined otherwise

This functionality is mainly intended for feature detection at present. In the future, the FragmentDirective object could include additional information.

Reference

API

CSS

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN