Syntax Highlighter Compared: highlight.js, Prism, Shiki

November 11, 2026 · 10 min read

Syntax Highlighter Libraries Compared: highlight.js, Prism, and Shiki

A syntax highlighter splits source code into tokens and colours each one, so keywords, strings and comments stand apart. On the web, the three libraries worth comparing are highlight.js, Prism and Shiki. Pick highlight.js or Prism when the browser does the work, and Shiki when you can render at build time.

What a Code Syntax Highlighter Does to Your Markup

Every code syntax highlighter does the same two jobs. It reads the code as plain text, matches it against a grammar for the language, and wraps each token in a span. Then something supplies the colour: a theme stylesheet that targets class names, or inline style attributes written straight onto each span.

Here's what highlight.js 11.12.0 produced when we passed it a two-line Python function:

<span class="hljs-keyword">def</span> <span class="hljs-title function_">greet</span>(<span class="hljs-params">name</span>):
    <span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Hello, <span class="hljs-subst">{name}</span>&quot;</span>

No colours yet. The hljs-keyword and hljs-string classes only pick up colours, such as red keywords and dark blue strings in the GitHub theme, once a theme stylesheet loads.

The grammars come in three families. highlight.js and Prism use hand-written regular expressions. Shiki uses TextMate grammars, the same format VS Code reads. Tree-sitter, a parser that updates its syntax tree as you type, powers highlighting in several code editors but rarely runs in web pages.

Markdown's only part in this is the language tag after the opening fence, which becomes a language-python class on the code element. Which tags each library accepts is a separate question, answered in our list of code block language identifiers. One thing to know up front: our editor's preview shows fenced code in plain monospace and doesn't colour it. You see colours only after your own site or platform runs a highlighter.

How Do You Highlight Code in HTML With highlight.js?

You highlight code in HTML by loading a highlighter script and a theme, marking each block with pre and code, and calling the library once the page loads. With highlight.js from cdnjs, the whole page looks like this:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.12.0/styles/github.min.css">
</head>
<body>
  <pre><code class="language-python">def greet(name):
    return f"Hello, {name}"</code></pre>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.12.0/highlight.min.js"></script>
  <script>hljs.highlightAll();</script>
</body>
</html>

hljs.highlightAll() finds every pre code element, adds the hljs-* spans you saw above, and the GitHub theme paints them. The highlight.js site lists 11.12.0 as the current release, with 193 languages and zero dependencies. The CDN build bundles only a common subset (36 languages in our check), and you add others with one extra script tag each, such as languages/elixir.min.js.

Leave out the class and highlight.js tries to guess the language. That guess is weak on short snippets. In our test of the common build, SELECT * FROM users came back as CSS. The longer SELECT name FROM users WHERE id = 1; was correctly read as SQL. Always set the class if you know the language.

The same library runs in Node or a build script. hljs.highlight(code, { language: 'python' }).value returns the highlighted HTML string, so you can do the work once and ship static markup.

highlight.js Themes: One Stylesheet Swaps the Whole Look

A highlight.js theme is a single CSS file that assigns colours to the hljs-* classes. Changing the look means changing one link tag, and the markup stays the same, so your content never needs editing. The theme picker on highlightjs.org listed 258 themes as of November 2026, most of them Base16 variants.

To follow the reader's light or dark setting, load two themes. The media attribute picks one:

<link rel="stylesheet" media="(prefers-color-scheme: light)"
  href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.12.0/styles/github.min.css">
<link rel="stylesheet" media="(prefers-color-scheme: dark)"
  href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.12.0/styles/github-dark.min.css">

The browser applies only the stylesheet whose media query matches, so code turns dark the moment the operating system does. Remember that the theme colours only the tokens, not the rest of your rendered page. Our guide to Markdown CSS themes covers that second, document-level stylesheet.

Prism: A Small Core You Build Language by Language

Prism takes the opposite approach to bundling. Instead of one big file, you build your own on the Prism download page by ticking the languages, theme and plugins you need. Prism's site puts the core at 2KB minified and gzipped, with each language adding roughly 0.3 to 0.5KB and themes around 1KB.

Markup follows the same language-xxxx convention, and Prism also accepts the shorter lang-xxxx. The class is inherited, so a language-javascript class on body sets a page-wide default, and language-none opts a block out. Prism's plugins add features through more classes and attributes:

<link rel="stylesheet" href="prism.css">

<pre class="line-numbers" data-line="2"><code class="language-javascript">const total = items.length;
const label = `Items: ${total}`;
console.log(label);</code></pre>

<script src="prism.js"></script>

With the Line Numbers and Line Highlight plugins included in your download, that block shows numbers down the left edge and a highlighted band behind line 2. data-line also takes ranges such as 1-3, 5. Prism highlights on load by itself, so you don't call a function. Its docs are upfront about the trade-off, though: being regex-based, it will fail on certain edge cases, which they list on a known-failures page.

Shiki: VS Code Grammars Rendered at Build Time

Shiki uses TextMate grammars and VS Code themes, so code looks the way it does in VS Code. It's ESM-only and runs in any modern JavaScript runtime. Its main function, codeToHtml, returns a finished HTML string:

import { codeToHtml } from 'shiki'

const html = await codeToHtml('def greet(name):\n    return name', {
  lang: 'python',
  theme: 'github-light'
})

The output carries its colours inline, so no theme stylesheet is needed. Here's the first line from our run with Shiki 4.6.0:

<pre class="shiki github-light" style="background-color:#fff;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#D73A49">def</span><span style="color:#6F42C1"> greet</span>

For light and dark, replace theme with themes: { light: 'github-light', dark: 'github-dark' }. Shiki then writes the dark colours into CSS variables such as --shiki-dark on each span. A short prefers-color-scheme rule, given in Shiki's dual themes guide, swaps them in.

The catch is weight. Shiki's own measurement puts the full bundle, with every grammar and theme, at over a megabyte gzipped. That's why most sites run it at build time. In the browser, its docs recommend the fine-grained bundle, which loads only the languages and themes you pick. VitePress uses it by default, Astro ships built-in support for it, and our Laravel Markdown guide shows the Spatie package that uses it in PHP. If your docs follow a docs as code workflow, the site build already runs in CI on every merge, so highlighting happens there.

highlight.js vs Prism vs Shiki: Which Syntax Highlighter Fits?

The deciding question is where highlighting runs, then how you want themes to work. Language counts come from each project's site or package as of November 2026 (highlight.js 11.12.0, Shiki 4.6.0).

highlight.jsPrismShiki
Grammar engineRegex, per languageRegex, per languageTextMate grammars (VS Code)
Languages193297242 bundled
RunsBrowser, Node, DenoBrowser, NodeAny modern JS runtime, usually at build time
OutputSpans with hljs-* classesSpans with token classesSpans with inline styles
ThemesOne CSS file per themeOne CSS file per themeVS Code themes, light and dark via CSS variables
Auto-detects languageYesNoNo
Best forUser-pasted code, quick setupSmall custom bundles, line pluginsDocs and static sites

We prefer Shiki for docs and static sites, because the colours match VS Code and nothing extra ships to the reader. We pick highlight.js when we can't control which languages people paste, such as comments or a forum, since it can guess. Prism sits between them and suits sites that want line numbers without a build step.

For GitHub's exact look, starry-night is a fourth option: it uses the grammars on Linguist's grammar list, though its own docs warn it may be too heavy for browsers.

Should Highlighting Run in the Browser or at Build Time?

Run highlighting at build time when your content is known in advance, and in the browser when code arrives after the page loads. Each choice has knock-on effects:

  • JavaScript weight. Client-side highlighting ships a library and grammars to every reader. Build-time highlighting ships plain HTML.
  • Flash of plain code. In the browser, code appears uncoloured until the script runs. Pre-rendered code is coloured on first paint.
  • Live content. Chat messages, comments or a playground need client-side highlighting, because there's no build step to hook into.
  • Readers without JavaScript. Feed readers and other clients that don't run scripts never see client-side highlighting. They get colour only from pre-rendered spans, and usually only from inline-styled ones, since they skip your stylesheet.
  • Pasting elsewhere. Many email clients and CMS editors drop linked stylesheets, so inline-style output (Shiki's default) keeps its colours where class-based output goes grey.

If you render Markdown yourself, most parsers expose a hook for this. In markdown-it it's the highlight option, where you call highlight.js or Shiki during conversion. In React, the react-syntax-highlighter package wraps Prism (through refractor) or highlight.js (through lowlight) as a component, and inlines styles unless you turn that off. Our React Markdown tutorial shows it plugged into react-markdown.

One limitation no library fixes: a wrong or missing language tag. Tag a shell script as python and every highlighter colours it as Python, confidently and incorrectly.

Using a Syntax Highlighter Online for One-Off Snippets

An online syntax highlighter is the right tool when you need coloured code once, not on every page. Think of a CMS post, a newsletter or a single help page. You paste code, pick a language and theme, and copy the HTML out.

Our code block generator runs in your browser and does this for Markdown and HTML. It builds fenced blocks (with a diff mode and alert wrappers) and HTML output with line numbers and highlight.js themes. Paste the HTML into its destination and check it there once, because some editors and email clients strip styling on paste.

When the target is a tweet or a slide where HTML won't paste at all, an image is the safer format. The code to image tool renders code with highlight.js and exports PNG or SVG.

Choosing a syntax highlighter comes down to where it runs. Use Shiki when you render at build time and want VS Code accuracy. Use highlight.js when users paste unknown code in the browser, and Prism when a small bundle and line plugins matter most. For a one-off snippet, the code block generator gives you highlighted HTML in your browser, with nothing to install.