Code Syntax Highlighting with highlight.js
Do you publish code to the web that is not supposed to be executed? Are you like me and like to publish coding tutorials and need to make sure that the code is legible, formatted, and ✨beautiful ✨? This article will explain in six easy steps how to do it with two HTML tags and the highlight.js library.
6 Easy Steps:
- Add an embed to your site or whatever allows you to write HTML within your text editor.
- Add <pre> and <code> tags as shown in the code below.
- Import the highlight.js library from CDN and execute the hljs.highlightAll() function (see usage) . Copy/paste it into the before </body> tag section of Webflow.
- Choose your theme from the demos
- Import the CSS for your theme by adding the <link> tag to the <head> section. Don’t forget to add .min.css to the end of the theme name. In the example below, change the text ‘default’ to the name of the theme you want to use as it appears on the github repository in src > styles
- Publish and win!
If you want to display HTML, be sure to escape your HTML. I use freeformatter.
If you want more help, here's a video where I show you how I did it on this website: