DEV Community

Rohan Iyer
Rohan Iyer

Posted on

Link to Specific Text on a Webpage: Text Fragment Tutorial

You can send someone a URL that opens a long report, scrolls to one sentence, and highlights it — no anchor, no page edit, and no extension in a current browser. The feature is called a text fragment, and the whole trick lives in the part of the URL after the # sign.

This tutorial shows how to create that link from the browser menu, read and hand-edit the #:~:text= syntax, encode punctuation that would otherwise confuse the parser, and work through the usual reasons a highlight fails. It is written for anyone who cites passages: researchers, editors, support agents, and documentation writers.

If you first need to grab the page address itself — or the URL hiding behind linked text — this guide to copying and pasting a link on Windows, Mac, iPhone, and Android covers the basics before you add a fragment.

Table of Contents

What a Text Fragment Link Is

A text fragment link is an ordinary URL with a browser instruction appended after the hash:

https://example.com/report#:~:text=revenue%20grew%2012%25
Enter fullscreen mode Exit fullscreen mode

When a supporting browser opens that address, it searches the page for the phrase "revenue grew 12%", scrolls the phrase into view, and highlights it. The linked page does not need an id attribute, a named anchor, or any cooperation from its author. That is what makes the feature useful for citing a sentence in someone else's article or a paragraph deep inside vendor documentation.

The trade-off is durability. MDN's reference on text fragments explains that text is less stable than document structure: if the target sentence is edited, the fragment no longer matches, and the browser simply opens the top of the page. Treat these links as precise but perishable, and prefer wording that is unlikely to be rewritten.

Create a Link with Copy Link to Highlight

The fastest way to make one is to let the browser write it for you.

Chrome on the desktop:

  1. Open the page and select the sentence or phrase you want to cite.
  2. Right-click inside the selection.
  3. Choose Copy link to highlight.
  4. Paste the link into an email, document, or chat message.

These are the steps in Google's instructions for Copy link to highlight. On Android, Chrome's help page describes the same idea with touch: touch and hold, drag to select, tap Share, then choose the app to send the highlighted link to.

Microsoft Edge: select the text, right-click, and choose Copy link to highlight. Microsoft's Edge walkthrough shows the same command and notes that it works for anything from a single word to a full paragraph.

Safari: support for opening text fragments arrived in Safari 16.1, but the creation command is newer. WebKit's Safari 18.2 release notes describe selecting text and choosing Copy Link with Highlight from the context menu. Older Safari versions can still open a link you build by hand.

Paste the result somewhere you can inspect it. A generated link should end with something like #:~:text=the%20exact%20words. If your browser does not offer a copy command, you can write that ending yourself.

Support is broad but not universal. caniuse's support table for URL scroll-to-text fragments lists Chrome 81, Edge 83, Safari 16.1, and Firefox 131 as the starting versions. Firefox's implementation is marked as basic support with some open issues, so a link that works in Chrome may behave differently there.

Read and Edit the Text Fragment Syntax

The full pattern is easier to read when you separate the optional parts:

https://example.com/page#:~:text=[prefix-,]textStart[,textEnd][,-suffix]
Enter fullscreen mode Exit fullscreen mode

The URL Fragment Text Directives specification defines this grammar. The :~: sequence is a fragment directive delimiter: it tells the browser that what follows is an instruction for the browser, not a normal page anchor, and the browser strips it from the URL during loading. The text= part introduces the text directive itself.

Part What it does Example
#:~:text= Starts the text directive #:~:text=
textStart The first words to find; required free%20forever
,textEnd The last words of the range to highlight ,no%20card%20required
prefix-, Words immediately before the match, used to pick the right instance Studio%20plan-,
,-suffix Words immediately after the match, used for the same reason ,-billed%20yearly
&text= Adds a second, separate highlight &text=refund%20policy

A bare textStart highlights the first match on the page. Adding ,textEnd turns it into a range: the browser starts at the first occurrence of textStart and stops at the first occurrence of textEnd that follows it. A prefix or suffix narrows the choice when the same phrase appears more than once. Context terms may be separated from the target by whitespace, and they are not included in the highlight.

Here is a worked example. Suppose a pricing page has two lines: "Starter plan free forever" and "Studio plan free forever billed yearly." A bare #:~:text=free%20forever always jumps to the Starter line. This version targets the Studio line instead:

https://example.com/pricing#:~:text=Studio%20plan-,free%20forever,-billed%20yearly
Enter fullscreen mode Exit fullscreen mode

The prefix Studio plan identifies the right line, the target is free forever, and the suffix billed yearly confirms the match. Only "free forever" is highlighted; the context words stay unhighlighted.

For a long quotation, use a start and end phrase instead of encoding the entire paragraph. #:~:text=Annual%20revenue,up%2012%25 highlights everything from the first "Annual revenue" to the next "up 12%".

Percent-Encode Punctuation Correctly

Values in the text directive are percent-encoded in the URL, and the browser decodes them before matching. Spaces are the most common case, but the characters that give the syntax its structure need special care.

Character Encode as Why
Space %20 A literal space is not valid inside the fragment
Hyphen or dash %2D A literal - separates a context term from the target
Comma %2C A literal , separates textStart from textEnd
Ampersand %26 A literal & separates multiple directives
Percent sign %25 Otherwise the browser reads it as the start of another escape

If the phrase you are quoting contains a dash, a comma, or an ampersand, encode it. The specification requires those characters to be percent-encoded inside text parameters so they are not interpreted as part of the directive syntax.

You can let JavaScript do most of the work in a browser console:

const text = "free forever, no card - cancel anytime";
const encoded = encodeURIComponent(text).replace(/-/g, "%2D");
console.log(encoded);
Enter fullscreen mode Exit fullscreen mode

encodeURIComponent handles spaces, commas, and ampersands. The extra replace covers the hyphen, which encodeURIComponent leaves untouched. Do not encode the result a second time, or every % becomes %25 and the browser will search for the wrong string.

Choose Text That Stays Linkable

The phrase you choose determines whether the link survives. A few practical rules make a big difference.

  • Pick something distinctive. "Billed yearly" is a good target. "The" will match the first "the" on the page.
  • Keep it short. A sentence or two is usually enough. Long selections break when any single word changes.
  • Use headings and product names. They tend to change less often than marketing copy.
  • Use start and end for long passages. web.dev's guide to text fragments shows how a few words at each end can highlight a whole section without producing an enormous URL. It also notes that each individual term must sit inside a single block-level element, although a full start-to-end range can cross block boundaries.
  • Avoid hidden or late-loading text. A sentence inside a closed accordion, an inactive tab, or a section that only appears after scrolling may not be found when the page first loads.
  • Remember the frame limit. Text fragments apply to the main page, not to content inside an iframe.

Matching is case-insensitive, so Revenue and revenue are equivalent. That helps with headings, but it also means a common phrase in a different case can still win the first match. When in doubt, add context terms.

Debug a Missing Highlight

The page opens, but nothing is highlighted and you are at the top. Work through these five checks in order.

  1. The words changed. The page was edited and the exact phrase no longer exists. Open the page, search for the wording, and rebuild the link with text that is currently present. If the sentence is gone for good, no edit to the URL will recover it.
  2. The phrase appears more than once. The browser highlights the first match, which may be far above or below where you expected. Add a prefix or suffix, or choose a longer, more unusual phrase. A highlight can be present but offscreen, so scroll before assuming it failed.
  3. The text is hidden or loads late. Check whether the quote sits in a closed accordion, a tab, a modal, or a lazily loaded section. The browser cannot match text that is not in the page when the fragment is processed. Choose a passage that is visible on normal load.
  4. The browser does not support text fragments. Older browsers ignore the fragment and open the page at the top. There is nothing broken in the URL; the recipient simply needs a current version of Chrome, Edge, Safari, or Firefox.
  5. A redirect or the site rewrote the link. Link shorteners, login flows, and client-side redirects can lose the instruction. WICG's note on text fragments and client-side redirects explains that server-side redirects preserve the user activation needed to apply a fragment, while a script that sets window.location may not. Paste the final destination URL into a new tab and press Enter. If the highlight works there, the redirect is the culprit.

Before blaming the page, do one quick syntax check: confirm that literal commas and dashes inside your quoted words are encoded, that the #:~:text= prefix is intact, and that you did not encode the same text twice.

Conclusion

A text fragment turns an ordinary URL into a precise citation. Use Copy link to highlight in Chrome or Edge, or Copy Link with Highlight in Safari 18.2 and later, then inspect the generated #:~:text= ending. Hand-edit it when you need a prefix, suffix, or a start-and-end range, and percent-encode spaces, commas, dashes, and ampersands so the parser does not mistake them for syntax.

Choose short, distinctive, visible phrases that are unlikely to be rewritten. If a highlight fails, check for changed text, duplicate matches, hidden content, an old browser, and redirects. The link always degrades to a normal page address, so even a broken fragment still gets your reader to the right document.

Top comments (0)