::target-text CSS Pseudo Element
Description
The ::target-text pseudo-element in CSS is used to style the portion of text within an element that has been targeted by a fragment identifier in the URL. In simpler terms, when a user navigates to a URL with a hash (e.g., https://example.com/page#section1), the element with the matching id becomes the target, and ::target-text allows you to apply styles specifically to the text content of that targeted element. This pseudo-element provides a precise way to emphasize text that the user is navigating to, rather than styling the entire element.
Unlike the :target pseudo-class, which styles the entire element being targeted, ::target-text focuses solely on the textual content. This distinction is particularly useful when you want subtle highlighting or animation effects on the text itself without altering the element's box model, borders, or background. It can also be combined with other pseudo-elements like ::first-letter or ::first-line for more advanced typographic effects.
You can style ::target-text using standard text-related CSS properties such as color, font-weight, text-decoration, and background-color. This enables dynamic highlighting or visual feedback when a user jumps to a specific section via an anchor link.
Example:
<p id="intro">Welcome to our website! Click <a href="#intro">here</a> to highlight this text.</p>
p:target::target-text {
background-color: yellow;
font-weight: bold;
color: darkblue;
}
In this example, when the user clicks the link to #intro, only the text inside the paragraph with id="intro" will be highlighted and styled, leaving the rest of the paragraph’s container unaffected.
Another useful scenario is animating the appearance of the target text for a smoother reading experience, for instance using transitions on color or background-color. This makes ::target-text a powerful tool for enhancing navigation and readability on content-heavy pages.
Syntax
::target-text {
/* ... */
}
Example
Browser Support
The following information will show you the current browser support for the CSS ::target-text pseudo element. Hover over a browser icon to see the version that first introduced support for this CSS psuedo element.
This psuedo element is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 31st December 2025
