highlight.js

Highlight.js (22.2k ⭐) is an open-source library to syntax highlight code snippets on web pages. Prism is the main alternative.

  • πŸ’Έ Free and Open-Source
  • 🌍 Supports many languages (192+)
  • 🎨 Supports many themes (496+)

It's the library used by the popular Stack Overflow and Discord.


Load the library πŸ“š

You can find the tag to load it on the cdnjs website. You should add tags for all languages you want to use, which is more secure than letting the script automatically import them ☠️.

<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/X.Y.Z/highlight.min.js" [...]></script>

Write Code πŸ€–

<pre><code class="language-c"
>int main() {
    exit 0;
}
</code></pre>

Highlight Code πŸš€

Once the content to highlight is rendered, you need to call hljs:

<script>
hljs.highlightAll();
</script>