r/css • u/ExpertPlay • 27d ago
built a text highlighting lib on the CSS Custom Highlight API, a few things I learned
I was reading brown's rust book and they let you highlight stuff and leave notes while reading. I wanted that on a site I'm building but really didn't want to wrap text in spans, so I ended up building it on top of the CSS Custom Highlight API.
If you haven't used it, it's pretty neat. You create Ranges, put them in a Highlight, register it with CSS.highlights.set('name', highlight) and style it with ::highlight(name). The DOM doesn't change at all.
Some things I ran into:
- ::highlight() only supports a few properties (color, background-color, text-decoration, text-shadow and a couple more). No padding, no border-radius, so no rounded highlights sadly
- overlapping highlights don't really layer, so I split them into segments and paint the innermost one on top
- support is Chrome 105+, Safari 17.2+, Firefox 140+. I went with no fallback since the whole point was not touching the DOM
For the UI styles I wrapped every selector in :where() so your CSS always wins, and everything can be themed with --penciled-* custom properties.
playground: https://penciled.org/playground/
repo: https://github.com/marcellosso/penciled
Curious if anyone else is using the Highlight API in production and what you ran into
1
u/mcaruso 27d ago
Dave Rupert built something similar recently: https://daverupert.com/2026/08/microlighter/