<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo" -->

---
title: css custom highlight api javascript Tutorial | daily.dev
description: A step-by-step guide demonstrates building a DOM-free syntax highlighter using the CSS Custom Highlight API, which styles arbitrary text ranges via Range and...
canonical: https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: css custom highlight api javascript Tutorial | daily.dev
og:description: A step-by-step guide demonstrates building a DOM-free syntax highlighter using the CSS Custom Highlight API, which styles arbitrary text ranges via Range and...
og:url: https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo
og:image: https://api.daily.dev/og/posts/Zer4jPPPO.png
og:image:alt: css custom highlight api javascript Tutorial
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# css custom highlight api javascript Tutorial

**[SitePoint](https://daily.dev/sources/sitepoint)** · 17 min read · 5 upvotes · 1 comments

## Summary

A step-by-step guide demonstrates building a DOM-free syntax highlighter using the CSS Custom Highlight API, which styles arbitrary text ranges via Range and Highlight objects without inserting wrapper elements like span. It covers tokenizing source code, creating Range objects, registering highlights via CSS.highlights, updating on content change in contenteditable blocks, and performance advantages over span-based wrapping (zero DOM node insertion, no layout recalculation). Browser support is noted: Chromium 105+, Safari 17.2+, with Firefox still lacking full support. Limitations include a restricted set of styleable CSS properties (no font-weight, font-size, display, padding) and the need for normalize() to avoid text node fragmentation bugs. Includes a working interactive demo with performance stats.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sitepoint.com/css-custom-highlight-api-javascript-guide>

## Questions this post answers

### Which browsers support the CSS Custom Highlight API and from what version?

Chromium-based browsers including Chrome and Edge support the CSS Custom Highlight API starting from version 105, while Safari added support in version 17.2. Firefox has been tracking the specification but had not shipped full support at time of writing. Developers should verify exact feature completeness against the MDN compatibility tables before targeting a minimum version.

_Track browser support milestones like this on daily.dev before committing to the custom highlight api._

### Which CSS properties can be styled inside a ::highlight() pseudo-element?

Only a limited, paint-level set of properties is allowed: color, background-color, text-decoration, text-shadow, font-style, caret-color, and text-emphasis-color. Layout-affecting properties such as font-weight, font-size, display, padding, and margin are excluded, because highlights must not alter document layout and operate purely at the paint level.

_Developers weighing highlight-based styling against span wrapping can follow such css capability details on daily.dev._

### How do overlapping Highlight objects get resolved when they cover the same text range?

The Highlight.priority property resolves overlaps: when two Highlights cover the same text, the one with the higher priority value paints on top. Without an explicit priority set, the browser paints equal-priority highlights in registration order, so the last highlight registered in CSS.highlights renders on top of earlier ones.

_Implementers layering search highlights over syntax coloring can dig into edge cases like this on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@yaireo** · 0 upvotes

> This is why I created [https://github.com/yairEO/react-css-highlight](https://github.com/yairEO/react-css-highlight)
>
> Please go take a look 👀
>
> it's extremely **fast**, lightweight &easy-to-use

## Similar posts on daily.dev

- [Working with ::highlight\(\) using progressive enhancement](https://daily.dev/posts/working-with-highlight-using-progressive-enhancement-svttq0eh5) · Piccalilli · 1 upvotes · 0 comments
- [What’s \!important \#17: Custom Highlight API, CSS Navigation Matching, Fixing text-stroke, and More](https://daily.dev/posts/what-s-important-17-custom-highlight-api-css-navigation-matching-fixing-text-stroke-and-more-alg764vfx) · CSS-Tricks · 7 upvotes · 0 comments
- [Fine, I’ll build my own text editor](https://daily.dev/posts/fine-i-ll-build-my-own-text-editor-mlxtdf0rd) · Lobsters · 0 upvotes · 0 comments
- [Custom Highlight Universal Selector ::highlight\(\*\)](https://daily.dev/posts/custom-highlight-universal-selector-highlight--f416uv98z) · Planet Igalia · 0 upvotes · 0 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#css](https://daily.dev/tags/css), [#dom](https://daily.dev/tags/dom)

[View this post on daily.dev](https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"css custom highlight api javascript Tutorial","url":"https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo"},"datePublished":"2026-09-06T00:42:11.478Z","dateModified":"2026-09-13T19:35:08.361Z","description":"A step-by-step guide demonstrates building a DOM-free syntax highlighter using the CSS Custom Highlight API, which styles arbitrary text ranges via Range and...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1310337c6cbc33a712a189fd490098f0?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1310337c6cbc33a712a189fd490098f0?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"SitePoint","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SitePoint","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/sitepoint","url":"https://daily.dev/sources/sitepoint"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"javascript,css,dom","timeRequired":"PT17M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SitePoint","item":"https://daily.dev/sources/sitepoint"},{"@type":"ListItem","position":3,"name":"css custom highlight api javascript Tutorial"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo","comment":[{"@type":"Comment","text":"This is why I created https://github.com/yairEO/react-css-highlight\nPlease go take a look 👀\nit’s extremely fast, lightweight \u0026amp;easy-to-use","datePublished":"2026-09-06T15:22:12.123Z","url":"https://daily.dev/posts/Zer4jPPPO#c-NIenVpHtj","author":{"@type":"Person","name":"Yair Even Or","url":"https://daily.dev/yaireo","image":"https://avatars.githubusercontent.com/u/845031?v=4"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/css-custom-highlight-api-javascript-tutorial-zer4jpppo#faq","mainEntity":[{"@type":"Question","name":"Which browsers support the CSS Custom Highlight API and from what version?","acceptedAnswer":{"@type":"Answer","text":"Chromium-based browsers including Chrome and Edge support the CSS Custom Highlight API starting from version 105, while Safari added support in version 17.2. Firefox has been tracking the specification but had not shipped full support at time of writing. Developers should verify exact feature completeness against the MDN compatibility tables before targeting a minimum version. Track browser support milestones like this on daily.dev before committing to the custom highlight api."}},{"@type":"Question","name":"Which CSS properties can be styled inside a ::highlight() pseudo-element?","acceptedAnswer":{"@type":"Answer","text":"Only a limited, paint-level set of properties is allowed: color, background-color, text-decoration, text-shadow, font-style, caret-color, and text-emphasis-color. Layout-affecting properties such as font-weight, font-size, display, padding, and margin are excluded, because highlights must not alter document layout and operate purely at the paint level. Developers weighing highlight-based styling against span wrapping can follow such css capability details on daily.dev."}},{"@type":"Question","name":"How do overlapping Highlight objects get resolved when they cover the same text range?","acceptedAnswer":{"@type":"Answer","text":"The Highlight.priority property resolves overlaps: when two Highlights cover the same text, the one with the higher priority value paints on top. Without an explicit priority set, the browser paints equal-priority highlights in registration order, so the last highlight registered in CSS.highlights renders on top of earlier ones. Implementers layering search highlights over syntax coloring can dig into edge cases like this on daily.dev."}}]}
```

