Highlight.js allows you to have custom styled and properly formatted code on your website.
Want your code to look similar to the code you see here on this page? Let me introduce you to Highlight.js.
Highlight.js is a JavaScript library with the sole purpose of making code look better on websites. It supports more languages than you'd likely ever need, and has hundreds of different styles and colors to choose from.
How to use this code in Webflow:
There's a few steps to setting up Highlight.js on your website.
- Copy the code you see above and paste it into an HTML embed at the top of your website navigator in Webflow.
- Next you'll need to read the comments I've made in the code. There's a portion of this code that needs to be copied and pasted in a separate HTML embed at the footer of your website. Once you've copied those particular scripts to and pasted them in the footer embed, you can remove them from the header embed.
- After that, you'll need to create a third HTML embed that has the actual code you want to show off on your webpage, so create that embed where ever you need it on the page.
- Paste the code you want to show off inside the HTML embed you just created.
- If your code includes HTML tags in it (like <script>, <link>, <style> etc...) you'll need to use the HTML equivalents as a substitute so the code doesn't actually activate. For example: the "<" and ">" need to be replaced with "<" and ">" respectively, giving you this: " <script> ".
- You'll want to surround the code in two different tags, the <pre> and <code> tags. The order would look like this:
<pre>
<code>
The code you want to display goes here
</code>
</pre>
- Finally, you'll want to make sure you add the class 'highlight' and 'pre-shadow' to your <pre> tag. You'll also want to add a class to your <code> tag. The code tag class depends on what language you're showing off. If you're using CSS, then add the class of 'css'. If you're using JavaScript, write 'js'. If you're using more than one language in your code, give it all the classes for those languages. It should look something like this:
<pre class="highlight pre-shadow">
<code class="css">
The code you want to display goes here
</code>
</pre>
- Save and publish to see the result.
If you're having trouble implementing Highlight.js, I've left links to some helpful resources below with more in-depth explanations.
Find out each language's class name for the <code> tag here.
Demo all of the different styles here.
Here's a helpful video of someone implementing Highlight.js
Here's the GitHub link to all the styles you can choose from.
This is a Webflow Cloneable where someone implemented Highlight.js.