---
title: "CSS ::target-text for Text Highlighting"
description: "A look at how browsers can highlight text fragments using CSS ::target-text, making text sharing and navigation more user-friendly"
canonical: "https://www.trevorlasn.com/blog/css-target-text"
author: "Trevor I. Lasn"
published: "2024-12-17"
last-updated: "2024-12-17"
topics: ["CSS"]
---

# CSS ::target-text for Text Highlighting

A look at how browsers can highlight text fragments using CSS ::target-text, making text sharing and navigation more user-friendly

By Trevor I. Lasn · Published 2024-12-17 · https://www.trevorlasn.com/blog/css-target-text

Ever needed to share a specific part of a webpage with someone? We often resort to screenshots or vague instructions like "search for this text" or "scroll down to the third paragraph." 

Another approach is to add anchor points with IDs `<section id="section-1">` to enable direct navigation, but this requires modifying the HTML structure and only works for predefined sections. While these approaches work, they're not ideal for sharing specific text content.

[Browser support for `target-text` on webstatus.dev](https://webstatus.dev/features/target-text)

The `::target-text` pseudo-element offers a native solution for text highlighting. When someone visits a URL containing a text fragment (like `#:~:text=hello`), browsers that support this feature will automatically find and highlight that text. Let's look at how this works and where it might be useful.

The `::target-text` pseudo-element simplifies this entire process. When a user visits a URL with a text fragment (like `#:~:text=hello`), the browser automatically matches and scrolls to that text. `::target-text` lets us style this highlighted section without writing a single line of JavaScript.

The implementation is surprisingly straightforward. First, we define how highlighted text should appear:

```css title="CSS"
::target-text {
  background-color: yellow;
}
```

Then, we can create links that highlight specific text when clicked:

```html title="HTML"
<a 
  href="#:~:text=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog"
  >Click to highlight "quick brown fox"
</a>
```

Here's a simple example. Click the link below to see the text fragment highlighting in action. The browser will scroll to the matching text and apply the yellow background color.

`::target-text` can be particularly helpful in several scenarios:

1. Documentation websites where users need to reference specific sections
2. Educational platforms where teachers want to highlight particular passages
3. Support pages where staff need to guide users to specific information
4. Blog posts or articles where readers want to share specific quotes. *(via `#:~:text=` inside the URL)*

**Pro tip:** On most browsers, you can also right-click on selected text and choose *"Copy link to highlight"* for the same functionality without any custom code.

![Browser right-click menu on selected text with the 'Copy Link to Highlight' option underlined](https://res.cloudinary.com/dcxnnco0b/image/upload/w_auto,dpr_auto,f_auto,q_auto/v1734425760/Screenshot_2024-12-17_at_10.55.35_n8ds9f.webp)

The `::target-text` pseudo-element brings native text highlighting to the web, making content sharing more precise and user-friendly. Whether through custom implementations or the browser's built-in features, we can now share exact passages instead of vague directions - a small but significant step toward better web navigation.
