{"id":3564,"date":"2024-08-20T12:41:45","date_gmt":"2024-08-20T17:41:45","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=3564"},"modified":"2024-08-20T17:04:06","modified_gmt":"2024-08-20T22:04:06","slug":"font-with-built-in-syntax-highlighting","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/font-with-built-in-syntax-highlighting\/","title":{"rendered":"Font with Built-In Syntax Highlighting"},"content":{"rendered":"\n<p>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 <code>&lt;span><\/code>, then select that in CSS and apply a color. Ideally, the span-wrapping happens server-side so client-side JavaScript isn&#8217;t tied up doing something so rote, but many of the libraries that help with it are client-side.<\/p>\n\n\n\n<p>What if&#8230; you didn&#8217;t need to do span-wrapping at all?! <a href=\"https:\/\/blog.glyphdrawing.club\/font-with-built-in-syntax-highlighting\/\">That&#8217;s what Heikki Lotvonen has cooked up.<\/a> The colorization happens <em>at the font level<\/em>, that is, via <a href=\"https:\/\/www.colorfonts.wtf\/\">&#8220;color fonts&#8221;<\/a>, the OpenType feature, and &#8220;contextual alternates&#8221;. 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&#8217;s almost certainly faster than whatever you&#8217;re doing now!<\/p>\n\n\n\n<p>Adam <a href=\"https:\/\/codepen.io\/argyleink\/pen\/GRbyNNv\">made a demo<\/a> that showcases how CSS can override the colors used, making theming and color modes doable.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_GRbyNNv\" src=\"\/\/codepen.io\/anon\/embed\/GRbyNNv?height=450&amp;theme-id=47434&amp;slug-hash=GRbyNNv&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed GRbyNNv\" title=\"CodePen Embed GRbyNNv\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &lt;span>, then select that in CSS and apply a color. Ideally, the span-wrapping happens server-side so client-side JavaScript isn&#8217;t tied up doing something [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3567,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29],"tags":[81,7,39],"class_list":["post-3564","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-beat","tag-color","tag-css","tag-typography"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-20-at-10.40.51%E2%80%AFAM.png?fit=1178%2C490&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=3564"}],"version-history":[{"count":4,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3564\/revisions"}],"predecessor-version":[{"id":3572,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3564\/revisions\/3572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/3567"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=3564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=3564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=3564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}