::highlight() - CSS Selector

::highlight()

Summary of characteristics of the ::highlight() selector

Quick description
Pseudo-element that targets the text selected after a search with Javascript.
Status
Standard
W3C Module
CSS Pseudo-Elements Module
Document status: WD (Working Draft)

::highlight() pseudo-element - Description.

::highlight() targets the text highlighted by JavaScript using the CSS Custom Highlight API library. This library lets you select parts of text anywhere on the page.

The operation requires several steps:

  • Create one or more Range objects that each define a portion of text to highlight.
  • Group these objects into a Highlight object.
  • Link this object to a CSS-usable identifier.
  • Use ::highlight() with this ID to target the texts.

Few properties can apply to ::highlight():

  • The color of the text.
  • The background color.
  • text-decoration and the detailed properties. ⚠
  • Text shadow (text-shadow). ⚠

The example below searches for the entered word within the text of the description section of this page. It is first searched without considering case (highlighted in light blue), then with case sensitivity (highlighted in blue).
Compared to a classic search, this has the advantage of only looking in the desired spots, and not in the table of contents, for example. On the other hand, since the search is done in JavaScript, we have complete freedom to do whatever we want, here distinguishing texts that respect case from those that don’t.

::highlight() pseudo-element - Browser compatibility.

Relatively new, the pseudo-element ::highlight() is now well recognized by browsers, with a few exceptions.

Column 1
Support by browsers for the pseudo-element ::highlight which targets texts highlighted by Javascript.

Notes:

(1) The pseudo element ::highlight cannot be used with text-decoration and text-shadow.

(2) The pseudo-element ::highlight is ignored when combined with user-select:none.

1
::highlight
pseudo-element
Estimated overall support.
93%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Samsung Internet

Chrome

Edge

Opéra

Safari

Safari sur IOS

Firefox

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

KaiOS Browser

Opéra mini

::highlight() pseudo-element - Historic.

See also...

Some pseudo-elements target the result of a search. Here are the main ones:

::selection
A pseudo-element that targets user-selected content.
⚠
::search-text
Langue française
Target the text searched by the internet user (browser search zone CTRL+F).
::target-text
Langue française
Targets the text of the item designated by the url bookmark.
::highlight()
Pseudo-element that targets the text selected after a search with Javascript.