Concepts and usage
Styling text ranges on a webpage can be very useful. For example, text editing web apps highlight spelling or grammar errors, and code editors highlight syntax errors.
The CSS Custom Highlight API extends the concept of other highlight pseudo-elements such as ::selection, ::spelling-error, ::grammar-error, and ::target-text by providing a way to create and style arbitrary Range objects, rather than being limited to browser-defined ranges.
Using the CSS Custom Highlight API, you can programmatically create text ranges and highlight them without affecting the DOM structure in the page.
There are four steps to style text ranges on a webpage using the CSS Custom Highlight API:
- Creating Range objects.
- Creating Highlight objects for these ranges.
- Registering the highlights using the HighlightRegistry.
- Styling the highlights using the ::highlight() pseudo-element.
Create ranges
The first step is to define the text ranges that you want to style by creating Range objects in JavaScript. For example:
Create highlights
The second step is to instantiate Highlight objects for your text ranges.
Multiple ranges can be associated to one highlight. If you want to highlight multiple pieces of text the same way, you need to create a single highlight and initialize it with the corresponding ranges.
But you can also create as many highlights as you need. For example, if you are building a collaborative text editor where each user gets a different text color, then you can create one highlight per user, as seen in the code snippet below:
Each highlight can be styled differently.
Register highlights
Once highlights have been created, register them by using the HighlightRegistry available as CSS.highlights.
The registry is a Map-like object used to register highlights by names, as seen below:
In the above code snippet, the user-1-highlight and user-2-highlight strings are custom identifiers that can be used in CSS to apply styles to the registered highlights.
You can register as many highlights as you need in the registry, as well as remove highlights and clear the entire registry.
Style highlights
The final step is to style the registered highlights. This is done by using the ::highlight() pseudo-element. For example, to style the user-1-highlight highlight registered in the previous step:
Interfaces
HighlightThis interface is used to represent a collection of ranges to be styled on a document.
HighlightRegistryAccessible via CSS.highlights, this Map-like object is used to register highlights with custom identifiers.
Examples
>Highlighting search results
This example shows how to use the CSS Custom Highlight API to highlight search results.
HTML
The HTML code snippet below defines a search field and an article with a few paragraphs of text:
JavaScript
JavaScript is used to listen to the input event on the search field. When the event is fired, the code locates matches for the input text in the article text. It then creates ranges for the matches, and uses the CSS Custom Highlight API to create and register a search-results highlight object:
CSS
Finally, the ::highlight() pseudo-element is used in CSS to style the highlights:
Result
The result is shown below. Type text within the search field to highlight matches in the article:
Specifications
Browser compatibility
>api.Highlight
Loading…
api.HighlightRegistry
Loading…
css.selectors.highlight
Loading…
.png)

