Spec-Zone.ru › CSS

`::highlight()` CSS pseudo-element

Baseline Newly available

This feature works across the latest devices and browser versions. This feature might not work in older devices or browser versions. It's been available across browsers since March 2026.

The ::highlight() CSS pseudo-element applies styles to a custom highlight.

A custom highlight is a collection of AbstractRange objects and is registered on a webpage using the HighlightRegistry.

The ::highlight() pseudo-element follows a special inheritance model common to all highlight pseudo-elements. For more details on how this inheritance works, see the Highlight pseudo-elements inheritance section.

Allowable properties

Only certain CSS properties can be used with ::highlight():

  • color
  • background-color
  • text-decoration and its associated properties
  • text-shadow
  • -webkit-text-stroke-color, -webkit-text-fill-color and -webkit-text-stroke-width

In particular, background-image is ignored.

Syntax

::highlight(custom-highlight-name)

Examples

Highlighting characters

HTML

<p id="rainbow-text">CSS Custom Highlight API rainbow</p>

CSS

#rainbow-text {
  font-family: monospace;
  font-size: 1.5rem;
}

::highlight(rainbow-color-1) {
  color: violet;
  text-decoration: underline;
}
::highlight(rainbow-color-2) {
  color: purple;
  text-decoration: underline;
}
::highlight(rainbow-color-3) {
  color: blue;
  text-decoration: underline;
}
::highlight(rainbow-color-4) {
  color: green;
  text-decoration: underline;
}
::highlight(rainbow-color-5) {
  color: yellow;
  text-decoration: underline;
}
::highlight(rainbow-color-6) {
  color: orange;
  text-decoration: underline;
}
::highlight(rainbow-color-7) {
  color: red;
  text-decoration: underline;
}

JavaScript

const textNode = document.getElementById("rainbow-text").firstChild;

if (!CSS.highlights) {
  textNode.textContent =
    "The CSS Custom Highlight API is not supported in this browser!";
}

// Create and register highlights for each color in the rainbow.
const highlights = [];
for (let i = 0; i < 7; i++) {
  // Create a new highlight for this color.
  const colorHighlight = new Highlight();
  highlights.push(colorHighlight);

  // Register this highlight under a custom name.
  CSS.highlights.set(`rainbow-color-${i + 1}`, colorHighlight);
}

// Iterate over the text, character by character.
for (let i = 0; i < textNode.textContent.length; i++) {
  // Create a new range just for this character.
  const range = new Range();
  range.setStart(textNode, i);
  range.setEnd(textNode, i + 1);

  // Add the range to the next available highlight,
  // looping back to the first one once we've reached the 7th.
  highlights[i % 7].add(range);
}

Result

Specifications

Specification
CSS Custom Highlight API Module Level 1
# custom-highlight-pseudo
CSS Pseudo-Elements Module Level 4
# selectordef-highlight-custom-ident

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet WebView Android WebView on iOS
highlight
105
105
149
140–149Before Firefox 149, cannot be used with text-shadow. See bug 1845447.
Before Firefox 146, cannot be used with text-decoration. See bug 1845446.
91
17.2The style is ignored when combined with user-select: none. See bug 278455.
105
149
140–149Before Firefox for Android 149, cannot be used with text-shadow. See bug 1845447.
Before Firefox for Android 146, cannot be used with text-decoration. See bug 1845446.
72
17.2The style is ignored when combined with user-select: none. See bug 278455.
20.0
105
17.2The style is ignored when combined with user-select: none. See bug 278455.

See also

  • CSS custom highlight API module
  • CSS custom highlight API
  • CSS pseudo-elements module

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::highlight

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API