docs.rodeo

MDN Web Docs mirror

Highlight

{{APIRef("CSS Custom Highlight API")}} 

The Highlight interface of the CSS Custom Highlight API is used to represent a collection of {{domxref("Range")}}  instances to be styled using the API.

To style arbitrary ranges in a page, instantiate a new Highlight object, add one or more Range objects to it, and register it using the {{domxref("HighlightRegistry")}} .

A Highlight instance is a Set-like object that can hold one or more Range objects.

{{InheritanceDiagram}} 

Constructor

Instance properties

The Highlight interface doesn’t inherit any properties.

Instance methods

The Highlight interface doesn’t inherit any methods.

Examples

The following example demonstrates how to create ranges, instantiate a new Highlight object for them, and register it to be styled on the page:

const parentNode = document.getElementById("foo");

// Create a couple of ranges.
const range1 = new Range();
range1.setStart(parentNode, 10);
range1.setEnd(parentNode, 20);

const range2 = new Range();
range2.setStart(parentNode, 40);
range2.setEnd(parentNode, 60);

// Create a custom highlight for these ranges.
const highlight = new Highlight(range1, range2);

// Register the ranges in the HighlightRegistry.
CSS.highlights.set("my-custom-highlight", highlight);

The following CSS code snippet demonstrates how to style the registered custom highlight by using the {{cssxref("::highlight")}}  pseudo-element:

::highlight(my-custom-highlight) {
  background-color: peachpuff;
}

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN