I noticed that sometimes my code gets highlighted in different colors when rendered.

  • What is syntax highlighting?
  • How does it work?
  • Why isn't my code being highlighted correctly?
  • How do I report a bug or request a new language?
  • How do I use syntax highlighting?
  • What languages are currently available on Stack Exchange?

Return to FAQ index

  • I have question: why SE stuff (one person) will not report further such fundamental bugs? This person will once read all issue list of highlight.js and can easily decide it is bug or not. This looks really non-profesional when you see such highlight bugs... The SE chose that library so I think - it is SE responsibility to report such bugs further to highligt.js - especially In situation when this process is complicated (everybody need to get know all issue list). Am I wrong? Dec 3 '20 at 11:45
  • 1
    I need to check for existing reports on github.com/highlightjs/highlight.js/issues and eventualy post new bug reports. So I have few question not mentioned here. Which version of highlight.js is used on SE sites ? How colors are predifined ? Basicly I know/suppose that this is maintainged by CSS file, I ask do SE Sites use standard colors from https://github.com/highlightjs/ project or SE Sites use own sets of colors ? Could we change color and tone of colors ? Feb 4 at 21:46
  • I look into highlight.js demo then I select Language Categories=All and Themes=Stackoverflow Light are this theme exactly the same used on SE Sites ? Which language is used for lang-default ? Feb 4 at 22:42
  • 1
    how to find a list of languages supported by Stack Exchange? (Not the list supported by highlight.js)
    – Cadoiz
    Sep 20 at 6:36

What is syntax highlighting?

Syntax highlighting allows code in posts to be highlighted based on the language it's written in, to make it easier to read.

How does it work?

Stack Exchange does not have its own syntax highlighting engine. It uses highlight.js, and may not necessarily be using the latest release of that library. Therefore, any bugs and feature requests regarding syntax highlighting cannot be handled by Stack Exchange.

When creating or editing posts, syntax highlighting is assigned to the preview based on tags as soon as you stop typing for 5 seconds.

Why isn't my code being highlighted correctly?

1. Check that the site you're using has syntax highlighting enabled

The full list is available on Meta. If syntax highlighting is not enabled on a site, follow the instructions at the bottom of the answer there.

2. Make sure you're specifying a language code or the tag exists on a site

In case of a tag name make sure to also specify it in lower case. See How do I use syntax highlighting? section below for more info.

2a. Check that the language is ...

  • ... supported by highlight.js

    Look at the list of highlight.js supported languages. If your language is not on the list, it needs to be created within the highlight.js project before it can be deployed by Stack Exchange.

  • ... used by Stack Exchange

    Look at the highlight-loader.js file on the dev CDN, open in your favourite editor, and search for the language hint. For example, searching for "dart" brings you to a line consisting of hljs.registerLanguage('dart', .... This means that lang-dart is an available language hint. Searching for "psh" or "powershell" doesn't return any results, meaning that PowerShell is not supported by the Stack Exchange version of highlight.js.

2b. Check that the tag has a language code associated with it

Each tag may have a highlighting language specified in its properties. Visit the tag's wiki page to see it: click on the tag you've found at step 2 (such as ), then click "Learn more...". The language code that is currently being used for the tag (if any) will be displayed at the very bottom, below the wiki buttons:

Code Language (used for syntax highlighting): lang-js

If the tag doesn't have a highlighting language specified when it should, create a on your site's per-site meta to request it. (Only moderators can change the highlighting language for a tag.)

3. Check that the tags on a question have an associated language codes

By default, Stack Exchange uses the tags on a question to determine the language you are using. If there's more than one tag that has a highlighting language specified, or if none of the tags have one, it uses lang-default and lets highlight.js infer what's the best language to use. The same highlighting language that is used on a question is also used on its answers.

How do I report a bug or request a new language?

Reporting a bug or requesting a feature for a language supported by Stack Exchange

Go to the highlight.js GitHub repository's list of existing issues and check to see if your request is already filed there, and if not, post it there.

If your bug fix or feature request has already been fixed or implemented by highlight.js but isn't working here, please wait, as new versions of highlight.js are deployed on the sites on a scheduled basis, and the latest version hasn't been deployed yet. (If it hasn't been deployed after a long time, you can post a feature request here on Meta asking that Stack Exchange update to the latest version.)

Keep in mind that Stack Exchange does not maintain this syntax highlighter (aside from installing newer versions of it), and posting bug reports or feature requests concerning it here on Meta will not get them fixed or implemented.

Before you do anything, make sure that you've got the correct highlighting turned on.

Requesting that Stack Exchange support a new language which is already supported by highlight.js

If a language is already on the list of highlight.js supported languages, but is not supported on Stack Exchange (see Language codes currently available on Stack Exchange below), you can raise a feature request here on Meta to ask for it to be deployed on the network.

While it's fine to request for the SE team to add additional supported languages, these requests are extremely likely to be considered low-priority and as such deferred. The current response for such feature requests is that the team finds that the added trade-offs from supporting more languages outweigh their potential benefit, though they're looking for future ways to lighten it.

Requesting that a new language which is not currently supported by highlight.js be added

Follow the instructions in Reporting a bug or requesting a feature for a language supported by Stack Exchange above to request that highlight.js support your new language. Once the language is supported by highlight.js, follow Requesting that Stack Exchange support a new language which is already supported by highlight.js to have it implemented here on Stack Exchange.

How do I use syntax highlighting?

As long as all the conditions in the Why isn't my code being highlighted correctly? section above are satisfied, highlighting will kick in automatically on code blocks based on the tags on a question. (See 3. Check that the tags on a question have an associated language codes section above for how this works.) Inline code (i.e. this) is not highlighted.

It is possible to explicitly override the default highlighting language in use on the post with your language of choice in a specific code block, by specifying a language hint above the code block. Note that this is only supported when using the code fence (```) method of code formatting; as of the implementation of CommonMark, doing so on code blocks using the four-space indent method is no longer supported*:

code goes here

You may use either a language code or a tag name in the language hint to activate syntax highlighting. See below for the complete list of highlight.js-supported language codes.

For example:

  • Here is a code block with language code (having the "lang-" prefix) as a hint:

    function greet(person) {
        return "Hello " + person;
    var user = "Jon Skeet";
  • Here is a code block with tag name (without "lang-" prefix) as a hint:

    var arr = [0, 1, 2];  // will highlight if [typescript] tag exists
  • If you don't want to have any syntax highlighting, you can use the lang-none language:

    [code here]

You can also apply a language hint to all code blocks in your post, so you don't have to add a hint before each one, by adding an HTML comment at the top of your post. The hint will then be applied to all code blocks within your post, including those which use four-space indent or HTML <pre><code>:

<!-- language-all: lang-or-tag-here -->

Note that when used on a question, it does not override the highlighting language on its answers; those will still be highlighted by default as per the question's tags.

Language codes currently available on Stack Exchange

This is a complete list of every identifier that you can use in the language hint for syntax highlighting. All those language hints in each group point to the same highlighter. Other language aliases set by highlight.js might work, but only the following identifiers are officially supported by Stack Exchange.

  • Default: interpret the code and guess lang-default
  • None: explicitly do not use any syntax highlighting lang-none, lang-plaintext, lang-text, lang-txt
  • Bash lang-bash, lang-sh, lang-zsh
  • C, C++, and other C-likes lang-c, lang-h, lang-cpp, lang-hpp, lang-c++, lang-h++, lang-cc, lang-hh, lang-cxx, lang-hxx, lang-c-like
  • C# lang-cs, lang-csharp, lang-c#
  • Clojure lang-clojure, lang-clj
  • CoffeeScript lang-coffee, lang-coffeescript, lang-cson, lang-iced
  • CSS lang-css
  • Dart lang-dart
  • Erlang lang-erl, lang-erlang
  • Go lang-go, lang-golang
  • Haskell lang-hs, lang-haskell
  • HTML, XML, XSL, et al. lang-html, lang-xml, lang-xsl, lang-xhtml, lang-rss, lang-atom, lang-xjb, lang-xsd, lang-plist, lang-wsf, lang-svg
  • HTTP Headers lang-http, lang-https
  • Ini and TOML lang-ini, lang-toml
  • Java lang-java, lang-jsp
  • JavaScript lang-js, lang-javascript, lang-jsx, lang-mjs, lang-cjs
  • JSON lang-json
  • Kotlin lang-kotlin, lang-kt
  • LaTeX, TeX lang-latex, lang-tex
  • Less lang-less
  • Lisp lang-lisp
  • Lua lang-lua
  • Makefile lang-makefile, lang-mk, lang-mak
  • Markdown lang-markdown, lang-md, lang-mkdown, lang-mkd
  • MATLAB lang-matlab
  • Objective-C lang-objectivec, lang-mm, lang-objc, lang-obj-c
  • OCaml lang-ocaml, lang-ml
  • Pascal, Delphi lang-pascal, lang-delphi, lang-dpr, lang-dfm, lang-pas, lang-freepascal, lang-lazarus, lang-lpr, lang-lfm
  • Perl lang-pl, lang-perl, lang-pm
  • PHP lang-php, lang-php3, lang-php4, lang-php5, lang-php6, lang-php7
  • PHP Template lang-php-template
  • Protocol Buffers lang-protobuf
  • Python lang-py, lang-python, lang-gyp, lang-ipython
  • R lang-r
  • Ruby lang-rb, lang-ruby, lang-gemspec, lang-podspec, lang-thor, lang-irb
  • Rust lang-rs, lang-rust
  • Scala lang-scala
  • Scheme lang-scheme
  • SCSS lang-scss
  • Shell lang-shell, lang-console
  • SQL lang-sql
  • Swift lang-swift
  • TypeScript lang-typescript, lang-ts
  • VHDL lang-vhdl
  • VB.NET lang-vbnet, lang-vb
  • YAML lang-yaml, lang-yml

Any language identifiers used in a post that go unrecognized by highlight.js will functionally default to lang-default.

Hinting: Tags

You can specify any tag that exists on the site, and it will use whatever language code is currently associated with that tag (which can be either null (no hint), default, or a specific language-code).

Keep in mind that by default all tags start off with none as their language code. Tags with none specified as their language code will be ignored and revert to lang-default.

You can also use the plain none keyword to manually specify no syntax highlighting, similar to using the lang-none code above.

* The former method of specifying a highlighting language can still be used for HTML code blocks: place an HTML comment <!-- language: lang-or-tag-here --> before the <pre><code> tags and it will work. This:

<!-- language: c -->
<pre><code>#include &lt;stdio.h>
int main() {
  printf("Hello World");

will render as

#include <stdio.h>
int main() {
  printf("Hello World");

Also, this former method hasn't been completely removed for four-space indented code blocks, but merely deprecated. While it will still work for the time being on four-space indented code blocks, it may/will be removed in the future.

Note to editors:

Please do not add to the above list unless you are 100% sure it exists.

  • Just because you type something in and it looks like it's highlighted correctly does not mean the identifier actually exists in the system. Keep in mind that invalid identifiers revert back to default.

  • Additionally, several languages that are indicated as supported in highlight.js's documentation may not actually be supported by the Stack Exchange version of highlight.js, as they may be in separate modules which may not be deployed on Stack Exchange.

Because of these points, please link to the Meta question which confirms a hint's existence in your edit summary when adding a new hint to the list.

Note to commenters:

The comments on this FAQ are for requesting clarification of something you might not understand in the FAQ so that it can be fixed. Please DO NOT ask if certain languages will be supported in the future. That is not a question we can answer because Stack Exchange does not maintain this highlighter. Visit highlight.js for language support.

  • 3
    Shouldn't there be an indicated preference (not rule) here in this answer for using either a language code or a tag name (possibly with some qualifiers)? E.g. "Prefer to use X over Y, unless Z." (Somewhat related question on MSO (in comments): What's the difference between the two code formatting styles (code-fences vs indented blocks) regarding syntax highlighting?) Oct 16 '20 at 16:00
  • 1
    and it will use whatever language code is currently associated with that tag Is there an official list of these mappings available anywhere? Oct 29 '20 at 1:05
  • Section 4, "4. Check that the tags on the question have syntax highlighting enabled", has all the info to find out @JoshGoebel Oct 29 '20 at 5:41
  • 1
    @Josh To your direct question, I don't believe there is a list anywhere currently containing all tags with associated language, however. It also doesn't look like this info is accessible using SEDE either, it's not in the tags table.
    – zcoop98
    Oct 29 '20 at 14:32
  • 6
    @Wolf The lang- prefix isn't redundant. As mentioned in the post above, ```lang-javascript tells the highlighter to use the JS highlighter directly, while ```javascript alone tells it to look at the highlighter set for the tag [javascript]. They aren't always the same (maybe most notably with [typescript] on Stack Overflow).
    – zcoop98
    Mar 11 at 16:09
  • 2
    Thanks a lot for clarifying. The post has so much text that it's hard to find this information. The tags I use obviously have the right highlighting connected to them, so it's easy to get the impression of redundancy for most people.
    – Wolf
    Mar 11 at 16:56
  • Is there a way to determine what syntax highlighting code is being applied to a given code block? For example, in instances where none of the tags are associated with language codes and so lang-default is used, I'd like to know which language highlight.js ultimately settled on based on the auto-recognition (since if I find one I like, I can manually specify it in future, as lang-default gives different results question to question even if I use the same language).
    – Greedo
    Mar 21 at 15:47
  • I don't know @Greedo - perhaps worth a separate question. Mar 22 at 13:54
  • 1
    @Greedo You can sometimes tell by inspecting the code block on page using browser dev tools- I've found it somewhat inconsistent in the past, but most of the time the selected/ detected language itself is listed as a class on the code block. (Example)
    – zcoop98
    Mar 22 at 15:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .