Text fragments enable users to link directly to a specific portion of text within a web document without requiring the document’s author to annotate the text with an ID. This feature leverages a unique syntax in the URL fragment to identify and highlight the target text.
This enables you to create links that highlight a specific portion of text on a webpage. When someone clicks your link, the browser navigates to the webpage and automatically scrolls to and highlights the desired text.
How to Use Text Fragments
One of the web’s most fundamental features is the ability to link between different documents. There are two primary ways to create links:
Linking to the Top of a Document by providing the document's URL
Linking to a Specific Section of a Document by appending a document fragment (ID) to the URL
Create and use Text Fragments in Firefox
- Identify the Text to Highlight. Choose the exact text you want users to see on the target webpage.
- Create the URL. Use the following syntax to add a Text Fragment to the URL:
text= | A text directive. This provides a text fragment to the browser, defining what text is to be linked to in the linked document. |
textStart | A text string specifying the start of the linked text. When used without textEnd it will contain the whole range that must be highlighted. |
textEnd | A text string specifying the end of the linked text. When textStart and textEnd are both present, they serve as boundary points of a range. |
prefix- | A text string followed by a hyphen specifying what text should immediately precede the linked text, only allowing for whitespace in between. This helps the browser to select the correct linked text, in cases where there are multiple matches. |
-suffix | A hyphen followed by a text string specifying what text should immediately follow the linked text, only allowing for whitespace in between. This helps the browser to select the correct linked text, in cases where there are multiple matches. |
- Note: The text strings used for the following parameters must be percent-encoded: textStart, textEnd, prefix, suffix-.
- For example, this highlights the phrase "Firefox Text Fragments" on the page:
Test Your Link: Open the link in Firefox to ensure it highlights the intended text. If the webpage content changes or multiple matches are present, adjust the fragment using a prefix or suffix for greater accuracy.
Practical Examples
- Highlight a Single Word:
- This highlights the word "example."
- Highlight a Range of Text:
- This highlights the text "Text Fragments are useful."
- Add Context for Ambiguous Matches:
- This ensures the specific phrase is highlighted even if similar text exists on the page.
Common Questions
What happens if the target text changes?
The Text Fragment might fail if the target text is removed or altered significantly. Use prefixes and suffixes to improve resilience. If matching fails, the page behaves as if there is no Text Fragment. If an additional document fragment is present, it is scrolled to, otherwise the page scrolls to the top.
Can I use Text Fragments with non-text elements?
No. Text Fragments only work with selectable text, not images, videos, or dynamic elements.
Do all users see the highlight?
Yes, as long as their browser supports Text Fragments.
What are the limitations of linking to document fragments?
Linking to document fragments requires the page author to define an anchor, such as an id attribute, in the HTML.
If the ID is modified or removed, the link breaks and the user is redirected to the top of the page. While this behavior is an example of graceful degradation, it limits the control of the link author. The ability to link precisely to specific content is at the mercy of the page author’s decisions.