CSS Custom Highlight API lets you style text ranges without touching the DOM
The CSS Custom Highlight API allows developers to visually highlight arbitrary text ranges using JavaScript-registered identifiers, without inserting wrapper elements into the DOM. A walkthrough by Sunkanmi Fafowora on Piccalilli demonstrates the API by pairing it with a text-shadow fallback for browsers that have not yet implemented it. On the JavaScript side, developers create a Highlight instance, define a standard DOM Range, and register both in the CSS.highlights registry. The corresponding CSS targets the named highlight via the ::highlight() pseudo-element, applying color and text-shadow declarations identical to those used in the HTML fallback. An @supports selector() check determines whether the browser supports the API, enabling a clean progressive enhancement without user-agent sniffing.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in