Adding Syntax Highlighting to Code Snippets in a Website

This article was last updated on 2022-04-10, the content may be out of date.

All rights and credits reserved to Tania Rascia. This is a rewrite (kind of) just for sake of simplicity.

Here we will use the JavaScript library to automatically highlight our code. There are various libraries but the most favorited are:

It’s your choice to pick one but in this guide, we will use Prism.js. If you want to local hosting your file. Download it here,

Or we can simply link the css in our <head>:



And link to the JS before the closing tag:


<script src=""></script>

Finally, wrap your desired code in <pre><code class="language-whatever">. You’re defining the language, and the <pre> ensures that all your indentation will be properly preserved.


<pre><code class="language-css">body{