---
title: Syntax Highlighting
linktitle:
description: Hugo provides server-side syntax highlighting via Pygments and, like most static site generators, works very well with client-side (JavaScript) syntax highlighting libraries as well.
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2017-02-01
#tags: [highlighting,pygments,code blocks,syntax]
categories: [developer tools]
menu:
docs:
parent: "tools"
weight: 20
weight: 20
sections_weight: 20
draft: false
aliases: [/extras/highlighting/,/extras/highlight/]
toc: true
---
Hugo can highlight source code in _two different ways_—either pre-processed server side from your content or to defer the processing to the client side, using a JavaScript library.
## Server-side
For the pre-processed approach, highlighting is performed by an external Python-based program called [Pygments](http://pygments.org/) and is triggered via an embedded Hugo shortcode (see [example](#example-highlight-shortcode-input) below). If Pygments is absent from the path, it will silently simply pass the content along without highlighting.
### Server-side Advantages
The advantages of server-side syntax highlighting are that it doesn’t depend on a JavaScript library and, consequently, works very well when read from an RSS feed.
### Pygments
If you have never worked with Pygments before, here is a brief primer:
+ Install Python from [python.org](https://www.python.org/downloads/). Version 2.7.x is already sufficient.
+ Run `pip install Pygments` in order to install Pygments. Once installed, Pygments gives you a command `pygmentize`. Make sure it sits in your PATH; otherwise, Hugo will not be able to find and use it.
On Debian and Ubuntu systems, you may also install Pygments by running `sudo apt-get install python3-pygments`.
Hugo gives you two options that you can set with the variable `pygmentsuseclasses` (default `false`) in your [site configuration](/getting-started/configuration/).
1. Color codes for highlighting keywords are directly inserted if `pygmentsuseclasses = false` (default). The color codes depend on your choice of the `pygmentsstyle` (default = `"monokai"`). You can explore the different color styles on [pygments.org](http://pygments.org/) after inserting some example code.
2. If you choose `pygmentsuseclasses = true`, Hugo includes class names in your code instead of color codes. For class-names to be meaningful, you need to include a `.css` file in your website representing your color scheme. You can either generate this `.css` files according to the [description from the Pygments documentation](http://pygments.org/docs/cmdline/) or download the one of the many pre-built color schemes from [Pygment's GitHub css repository](https://github.com/richleland/pygments-css).
### Server-side Usage
Highlighting is carried out via the [built-in shortcode](/content-management/shortcodes/) `highlight`. `highlight` takes exactly one required parameter for the programming language to be highlighted and requires a closing shortcode. Note that `highlight` is *not* used for client-side javascript highlighting.
### Example `highlight` Shortcode Input
{{% code file="example-highlight-shortcode-input.md" %}}
```html
{{* highlight html */>}}
{{ .Title }}
{{ range .Data.Pages }}
{{ .Render "summary"}}
{{ end }}
{{ .Title }}
{{ range .Data.Pages }}
{{ .Render "summary"}}
{{ end }}
` elements with language-specific class attributes. For example, a code block for HTML would have `class="language-html"`.
The client-side highlighting script therefore looks for programming language classes according to this convention: `language-go`, `language-html`, `language-css`, `language-bash`, etc. If you look at the page's source, you might see something like the following:
```html
body {
font-family: "Noto Sans", sans-serif;
}
```
If you are using markdown, your content pages needs to use the following syntax, with the name of the language to be highlighted entered directly after the first "fence." A fenced code block can be noted by opening and closing triple tilde ~ or triple back ticks `:
{{< nohighlight >}}
~~~css
body {
font-family: "Noto Sans", sans-serif;
}
~~~
{{< /nohighlight >}}
Here is the same example but with triple back ticks to denote the fenced code block:
{{< nohighlight >}}
```css
body {
font-family: "Noto Sans", sans-serif;
}
```
{{< /nohighlight >}}
Passing the above examples through the highlighter script would yield the following markup:
{{< nohighlight >}}
<pre><code class="language-css hljs">;<span class="hljs-selector-tag">body</span> {
<span class="hljs-attribute">font-family</span>: <span class="hljs-string">"Noto Sans"</span>, sans-serif;
}
{{< /nohighlight >}}
In the case of the coding color scheme used by the Hugo docs, the resulting output would then look like the following to the website's end users:
```css
body {
font-family: "Noto Sans", sans-serif;
}
```
Please see individual libraries' documentation for how to implement each of the JavaScript-based libraries.
[Prism]: http://prismjs.com
[prismdownload]: http://prismjs.com/download.html
[Highlight.js]: http://highlightjs.org/
[Rainbow]: http://craig.is/making/rainbows
[Syntax Highlighter]: http://alexgorbatchev.com/SyntaxHighlighter/
[Google Prettify]: https://github.com/google/code-prettify
[Yandex]: http://yandex.ru/