Works on this device
Syntax Highlighter
Paste your code and choose its language. The page shows it in colour at once, and gives you the HTML and CSS to put it on a web page. You can also copy it as formatted text for Word or an e-mail. The code is read as text on this device. It is never run and never sent anywhere.
Your code
A whole number from 1 to 1,000,000.
Paste or type up to 200,000 characters. The code is read as text and is not run or sent anywhere.
UTF-8 text up to 2 MB. The language is chosen from the end of the file name.
Ready. Paste some code or choose Try an example.
Highlighted code
Your code will show here in colour.
- Lines
- Characters
- Marked parts
| Kind | Parts |
|---|
Copy or download
For a document or an e-mail
Copy as formatted text writes the colours into the text itself, so they stay when you paste into Word, Google Docs or an e-mail.
How to use the syntax highlighter
- Choose the Language of your code. If you open a file from your device, the language is chosen from the end of its name (for example
.pyfor Python). - Paste or type your code in Your code. The coloured version under Highlighted code changes as you type.
- Choose a Colour theme. Tick Show line numbers to number the lines, and set the first number if your code starts later in a file.
- Copy the HTML and the CSS into your own page, or choose Download a whole page for one HTML file with both.
- For Word, Google Docs or an e-mail, choose Copy as formatted text, then paste. The colours are written into the text, so they stay.
Languages and the rules for each
The page has a small reader of its own for each of these languages: JavaScript, TypeScript, JSON, HTML, XML, CSS, SCSS, SQL, Python, PHP, Java, C, C++, C#, Go, Kotlin, Bash, YAML, Markdown, GraphQL. Each reader marks only the things in the table below. It does not check whether the code is right, and it does not run it.
| Language | Comments | Text in quotes | Other marks |
|---|---|---|---|
| JavaScript | // to the end of the line; /* to */ | "…", '…' and … (a backslash keeps the next character; … may run over lines, and ${…} inside it stays part of the text) |
regular expressions /…/ where a value may start; keywords, true false null undefined NaN Infinity, built-in objects such as Array and Promise; @decorators; a name before ( is a function |
| TypeScript | // to the end of the line; /* to */ | as in JavaScript | the JavaScript rules, plus TypeScript keywords such as interface, type and readonly, and the basic types such as string, number and any |
| JSON | none (JSON has no comments) | "…"; a text followed by : is a key | numbers; true, false and null |
| HTML | <!-- to --> | attribute values in "…" or '…', or without quotes | tags and attribute names; <!DOCTYPE …> and <?…?>; &…; character references; inside <script> the JavaScript rules (TypeScript with lang="ts", JSON when the type names json), inside <style> the CSS rules (SCSS with lang="scss") |
| XML (also SVG) | <!-- to --> | attribute values in "…" or '…'; <![CDATA[ … ]]> | tags, attribute names, <?xml …?> and <!DOCTYPE …>; &…; character references |
| CSS | /* to */ | "…" and '…' | selectors before {, property names before :, numbers with units, # colours, !important, @rules, names before ( as functions, --custom-properties as variables |
| SCSS | // to the end of the line; /* to */ | "…" and '…' | the CSS rules, plus $variables, #{…} and nested rules |
| SQL | -- to the end of the line; /* to */ | '…' (a doubled '' stays inside); names in "…" or … are shown as variables |
keywords and types in any mix of capital and small letters; :name, @name and $1 parameters |
| Python | # to the end of the line | '…', "…", '''…''' and """…""" with the prefixes r, b, u and f | keywords, True False None, built-in types; @decorators at the start of a line |
| PHP | // and # to the end of the line or to ?>; /* to */ | '…', "…" and … |
the code between <?php (or <?=) and ?>; outside it the HTML rules; $variables; keywords in any mix of capital and small letters; #[…] attributes |
| Java | // to the end of the line; /* to */ | "…", '…' and """…""" text blocks | keywords, true false null, primitive types, @annotations |
| C | // to the end of the line; /* to */ | "…" and '…' (also with u8, u, U and L in front) | a line that starts with # (such as #include) as a directive; keywords, NULL true false, basic types |
| C++ | // to the end of the line; /* to */ | "…" and '…' with their prefixes, and raw texts R"x(…)x" | the C rules, plus C++ keywords and nullptr |
| C# | // to the end of the line; /* to */ | "…", '…', $"…", @"…" (a doubled "" stays inside) and """…""" | lines that start with # (such as #region); keywords, true false null, built-in types |
| Go | // to the end of the line; /* to */ | "…", '…' and … raw text over lines |
keywords, true false nil iota, built-in types |
| Kotlin | // to the end of the line; /* to */ (these may be inside each other) | "…", '…' and """…""" | keywords, true false null, basic types, @annotations |
| Bash (shell) | # at the start of a word, to the end of the line (also the #! first line) | '…' (no escapes), "…" and $'…' | keywords (if, then, fi, for, do, done…) and common built-in commands (echo, cd, export…) as whole words; $name, ${…} and $1 as variables; numbers are not marked |
| YAML | # at the start of a line or after a space | '…' and "…" on one line; plain values; the lines of a | or > block | keys before ": ", numbers and true false null ~ of the YAML 1.2 core schema, &anchors, *aliases and !tags, --- and ... markers |
| Markdown | <!-- to --> | code and fenced code blocks (` or ~~~; the code inside is highlighted when the fence names a language on this list) |
# headings, list and quote marks, bold and *italic* (also with _), [links](…) and <autolinks>, --- lines; simplified rules, not the full CommonMark rules |
| GraphQL | # to the end of the line | "…" and """…""" | keywords, true false null, the built-in scalar types, $variables and @directives |
The HTML and CSS you get
- The HTML is
<pre class="hl hl-light"><code class="language-javascript">with your code inside. Each marked part is in a<span>whose class names its kind, such ashl-keywordorhl-comment. Other text is left as it is. - The characters
&,<and>are written as&,<and>, so your code shows as text and is not read as HTML. - With line numbers, each line is in a
<span class="hl-line">and the CSS writes the numbers. They are not part of the text, so a visitor who copies your code does not copy the numbers. - The CSS gives each class its colour for the theme you chose. Every colour of every theme has a contrast of at least 4.5 to 1 with its background, the WCAG 2.2 level AA value for normal text. Comments are also in italics, so they do not depend on colour alone.
- A final line break at the end of your code is left out of the HTML.
| Class | What it marks |
|---|---|
| hl-comment | Comment |
| hl-string | Text in quotes |
| hl-number | Number |
| hl-keyword | Keyword |
| hl-literal | Fixed value (true, false, null) |
| hl-type | Built-in type |
| hl-function | Name before a bracket |
| hl-meta | Directive, decorator or declaration |
| hl-tag | Tag |
| hl-attr | Attribute |
| hl-property | Property or key |
| hl-variable | Variable |
| hl-regex | Regular expression |
| hl-selector | Selector |
| hl-heading | Heading |
| hl-strong | Bold text |
| hl-emphasis | Italic text |
| hl-link | Link |
| hl-code | Code |
Worked example
The JavaScript line const n = 42; // answer has three marked parts: the keyword const, the number 42 and the comment // answer. The HTML is:
<pre class="hl hl-light"><code class="language-javascript"><span class="hl-keyword">const</span> n = <span class="hl-number">42</span>; <span class="hl-comment">// answer</span></code></pre>
With the Light theme the CSS colours .hl-light .hl-keyword in #a1166e and .hl-light .hl-comment in #5f6b78 with italics, on a white background.
Limits you should know
- The readers are small and follow the stated rules only. Unusual code can be marked in a way your editor would not mark it. JSX and TSX tags, PHP heredoc texts, C++ digit separators (
1'000) and Python soft keywords such asmatchare not read specially. - In JavaScript, a
/is read as the start of a regular expression where a value can start, and as division after a name, a number or a closing bracket. This is a common rule, but it can be wrong in rare code. - The Markdown reader uses simplified rules, not the full CommonMark rules.
- This page does not format, tidy or shorten code. Use the Code Formatter to lay out code and the Code Minifier to make it smaller. To check JSON, use the JSON Formatter & Validator.
- This page does not change the colours inside an editor such as VS Code or Sublime Text. Those programs have their own settings and themes.
- Up to 200,000 characters. Files up to 2 MB, read as UTF-8 text.
- For a picture of your code, use Code to Image.