Search

Font with Built-In Syntax Highlighting

Syntax highlighting code on the web happens like coloring any other text on the web happens. You wrap the bits of text you want colored uniquely in some element, probably a meaningless <span>, then select that in CSS and apply a color. Ideally, the span-wrapping happens server-side so client-side JavaScript isn’t tied up doing something so rote, but many of the libraries that help with it are client-side.

What if… you didn’t need to do span-wrapping at all?! That’s what Heikki Lotvonen has cooked up. The colorization happens at the font level, that is, via “color fonts”, the OpenType feature, and “contextual alternates”. This is loaded with advantages. No client-side JavaScript needed, no extra DOM weight, and no exotic processing of any kind. It works just about like using any other custom font. It’s almost certainly faster than whatever you’re doing now!

Adam made a demo that showcases how CSS can override the colors used, making theming and color modes doable.

Wanna be a better web typographer?

Frontend Masters logo

It was once famously said that the web is 95% typography. You can't be a web designer and ignore type! We have an in-depth course on web typography from Jason Pamental getting into things like responsive styles, variable fonts, font loading, and more.

One response to “Font with Built-In Syntax Highlighting”

Leave a Reply

Your email address will not be published. Required fields are marked *

Frontend Masters ❤️ Open Source

Did you know? Frontend Masters Donates to open source projects. $313,806 contributed to date.