3 min read

Syntax highlighting with prism.js

Last time I was searching for a good syntax highlighter for beautifully viewing the code snippets on my blog post. Among different syntax highlighter I found out PrismJS by Lea Verou is pretty awesome.

Yet not sure what I mean by a syntax highlighter or what it does? Here’s an example of JavaScript snippet highlighted with syntax highlighter:

<script language="javascript">
  var today= new Date()
<body onload=alert(today)>

PrismJS is a light weight and works on most of all the platform whether it is your own hosted site or any other popular CMS platform like WordPress, Blogger or Tumblr.

Installing on your page

To install PrismJS on your webpage you need to download the files prism.js and prism.css choosing your desire theme and required settings.

Here, JavaScript file prism.js is your settings and CSS file prism.css is your theme styles.

Now, upload the two files in your hosting site.

Then, you will need to link the prism.css and prism.js files in your index page. Example:

<!DOCTYPE html>
    <link href="themes/prism.css" rel="stylesheet" />
    <!-- after all your content -->
    <script src="prism.js"></script>

Remember that to correctly work, prism.css file should be include before the </head> tag and prism.js before </body> tag.

Installing on WordPress

Easily highlight your code on WordPress with WP PrismJS. Simply download & activate the plugin and a new icon on your TinyMCE editor will appear. Click on it and you will be able to enter your indented code; choose the language; specifying the maximum height of the code window and the filename.

Installing on Blogger/Tumblr

To install PrismJS on your Blogger/Tumblr blog you can upload prism.css and prism.js files on Dropbox because we cannot host file in the Blogger/Tumblr site. Then as same process above go to template edit and inculde prism.css file link before the </head> tag and prism.js file link before </body> tag.

See also: How to host assets with Dropbox on your website

If you don’t like to upload the file and want to use PrismJS default theme then you can include the following two files link to install PrismJS:

<link href="http://prismjs.com/themes/prism.css" rel="stylesheet" />

<script src="http://prismjs.com/prism.js"></script>

Using PrismJS

First thing you have to escape HTML character < to &lt; and > to &gt; to highlight syntax with PrismJS. For Example:

  <code class="language-javascript">
      var today= new Date()
    &lt;body onload=alert(today)&gt;

You can also use different tools to escape HTML character.


You have to wrap your code inside the <pre> and the <code> tag defining the necessary class="language-XXX".

For example to show HTML snippet define class="language-markup"

  <code class="language-markup">

For CSS snippet define class="language-css"

  <code class="language-css">

For JavaScript snippet define class="language-javascript"

  <code class="language-javascript">

With PrismJS by help of these few easy steps you get a nice highlighted code snippet in your webpage, isn’t it awesome?