AI answers link straight to the sentence they quote. Your browser can make these links too. Add one button so your readers can share them.
Web Dev Weekly November 28, 2026
AI answers link to the exact sentence. Your site can too
AI answers link straight to the sentence they quote. Your browser can make these links too. Add one button so your readers can share them.
Built something and want to sell it?
CREEM costs nothing to start: no monthly fees, no setup fees, and you only pay when you earn. It also handles sales tax and VAT in 190+ countries, so you don't have to.
Someone wants to send a colleague one sentence from your 3,000 word article. They copy the URL, paste it, and add “it’s somewhere in the middle, search for ‘cache'”. The colleague opens the page at the top and gives up.
Search engines and AI answers already solved this. Their source links often end in something like #:~:text=only%20the%20first%20request, and the browser opens the page, scrolls to that sentence and highlights it. This is called a text fragment, and every major browser supports it.
Your browser can even make these links for you. In Chrome or Edge, select any text, right-click and pick “Copy link to highlight”. Almost nobody knows it’s there, so your readers will never find it. Put a visible button next to your content instead.
The fix #
Add a button to your article template:
<button class="quote-link" type="button">Copy link to selection</button>
Then turn the selected text into a link and copy it:
const encode = (text) => encodeURIComponent(text).replace(/-/g, '%2D');
function linkToSelection() {
const lines = getSelection().toString().split('\n').map((l) => l.trim()).filter(Boolean);
if (!lines.length) return null;
const first = lines[0].split(/\s+/);
const last = lines.at(-1).split(/\s+/);
const text = lines.length === 1 && first.length <= 8
? encode(lines[0])
: `${encode(first.slice(0, 4).join(' '))},${encode(last.slice(-4).join(' '))}`;
return `${location.origin}${location.pathname}#:~:text=${text}`;
}
const button = document.querySelector('.quote-link');
button.addEventListener('mousedown', (event) => event.preventDefault());
button.addEventListener('click', async () => {
const url = linkToSelection();
if (!url) return;
await navigator.clipboard.writeText(url);
button.textContent = 'Link copied';
});
The mousedown line stops the click from clearing the reader’s selection before you read it.
Caching is the cheapest performance win most sites never take. A response that is reused from the browser cache costs zero network requests, zero server time and zero bytes.
The catch is invalidation. Put a version number or a content hash in every asset's file name, and you can cache it for a year without ever serving a stale file.
HTML is the exception. Keep it short-lived, so a new deploy reaches readers on their next visit.
Select a sentence above (or across two paragraphs), then click the button.
Select a sentence in the demo, copy the link, then open it in a new tab.
How the link is built #
#:~:text=start
#:~:text=start,end
#:~:text=prefix-,start,-suffix
text=start: highlight the first match of this text.text=start,end: highlight everything fromstarttoend. The fix uses this for long selections, so a whole paragraph becomes a short URL of eight words.prefix-and-suffix: text right before or after the match, for when the same phrase appears more than once. Most quotes don’t need them.&text=: repeat it to highlight several passages in one link.
Matching ignores case. Each piece of text has to sit inside one block, like one paragraph or one list item, which is why the fix takes the start from the first selected line and the end from the last.
The encoding rule that breaks most links #
Everything goes through encodeURIComponent, which handles spaces, commas and &. But it leaves - alone, and a dash in your quote would be read as a prefix or suffix marker. That is the .replace(/-/g, '%2D'). Skip it and any quote with “built-in” or “real-time” opens at the top of the page.
Style the highlight #
Browsers use their own highlight color by default. Match your brand instead:
::target-text {
background: #fde68a;
color: inherit;
}
The rough edges #
- Whole words only: a match has to start and end on a word boundary. If someone selects half a word, the link opens at the top of the page instead.
- Edits break old links: if you rewrite the sentence, the link stops matching and the page opens at the top. Nothing breaks, it just loses the scroll.
- Older browsers do the same: no highlight, just the normal page. The link still works.
- Not inside iframes: the browser only searches the main page, so quoted text must not live in an embedded frame.
Support: Chrome and Edge since 2020, Safari 16.1+, Firefox 131+. ::target-text styling is a bit newer: Chrome 89+, Firefox 131+, Safari 18.2+.
Where this comes up #
- Long articles and docs: “see this exact paragraph” instead of “scroll down a bit”.
- Terms, policies and changelogs: support teams can link the exact clause a customer asked about.
- Help centers: answers that point to the one step that matters.
- Your own sharing: combine it witha native share button and readers can send the quote straight to a chat app on mobile.
Why it matters #
- No IDs needed: unlike
#headinganchors, any sentence on any page is linkable, with zero changes to your markup. - Readers become citers: every quote they share sends someone to the exact part of your page that convinced them.
- It’s just a URL: no backend, no storage. Likequery params , the state lives in the link itself.
Add the button to your article template, select your favorite sentence, and send the link to a friend.
Happy coding!
Marko